You must Sign In to post a response.
  • Category: General

    Solve the OOP scenario problem of creating a Class in c#

    Hi,

    I've a common class Employee with members Id, Name, DeptId, ManagerId, List<Projects>, List<People>(people who work under him) and has the public methods GetEmployee(), GetProjects().

    i. How to map this employee to multiple departments?

    ii. Can we make this Employee class Static? If yes, Why?
    If No, can we make class methods Static?

    iii. If we make class methods Static, what is the output of below:
    Employee emp = new Employee({Id=1,…});
    Employee.GetEmployee();
    Employee.GetProjects();

    Employee emp1 = new Employee({Id=2,…});
    Employee.GetEmployee();
    Employee.GetProjects();
    Which value will we get finally EmpId=1's data or Id=2's data?

    iv. Can we make this Employee class Abstract so that this be a base class for all its
    related subclasses to provide common functionality? or a normal class which is
    inherited by all its related subclasses?

    v. Why should we go for Abstract class? and difference between making Employee class
    Abstract to inherit methods and making Employee class public to get inherited by
    subclasses?

    Please explain me this scenario in OOP environment...
  • #754372
    Hai Arul,
    Please find below the answers for your questions:
    A common class Employee with members Id, Name, DeptId, ManagerId, List<Projects>, List<People>(people who work under him) and has the public methods GetEmployee(), GetProjects().

    i. How to map this employee to multiple departments?
    Ans. If you want to map the employee with multiple departments, you can have the List for the employee as:

    List<Employee> employees= new List<Employee>();
    employee.Add("ram");

    To add the department, you need to create the relation between the tables.
    ii. Can we make this Employee class Static? If yes, Why?
    If No, can we make class methods Static?
    Ans. It depends, if the method or class is shared by many other classes, we can make the class as static class. if only few methods are shared, then we can make those methods as static method.
    iii. If we make class methods Static, what is the output of below:
    Employee emp = new Employee({Id=1,…});
    Employee.GetEmployee();
    Employee.GetProjects();

    Employee emp1 = new Employee({Id=2,…});
    Employee.GetEmployee();
    Employee.GetProjects();
    Which value will we get finally EmpId=1's data or Id=2's data?
    Ans. Here in this case, you are having the Employee class non static and you made the other methods as static. Also you are initializing the class object by passing the
    iv. Can we make this Employee class Abstract so that this be a base class for all its
    related subclasses to provide common functionality? or a normal class which is
    inherited by all its related subclasses?
    Ans: yes, You can make it as Abstract super class.
    v. Why should we go for Abstract class? and difference between making Employee class
    Abstract to inherit methods and making Employee class public to get inherited by
    subclasses?
    Ans. For this answer, you can go through the below link Q.26:

    http://pawantechit.blogspot.in/2013/03/interview-question-for-3-6-year.html

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments