0

I am learning java by following two books, I would appreciate it if anyone can help me understand couple of lines which I have seen and am unable to understand.

Lets say I have classes:

  1. Employee (Abstract class)
  2. Manager (Employee Subclass)
  3. Server (Employee Subclass)
  4. Restaurant

So pretty much "Restaurant" will have a need for "Manager" and "Server(s)". I have seen code written this way and would like to know if this valid. I have also written the same code the way I have learned. Please see that code at the bottom.

public Class Restaurant{

   private Employee Manager;   //I DON'T UNDERSTAND THIS
   private Employee Server;    //I DON'T UNDERSTAND THIS


   public Restaurant(){

        //I DON"T UNDERSTAND THIS
        Manager = new Manager();
        Server = new Server();
   }
}

This is how I have learned to code:

public Class Restaurant{

   private Manager _mgr;
   private Server _server;


   public Restaurant(){

        _mgr = new Manager();
        _server = new Server();
   }
}
Combustion007
  • 474
  • 1
  • 13
  • 30

4 Answers4

2

I guess that your Manager and Server class have the classes defined like below,

class Manager extends Employee {

}

class Server extends Employee {

}

In that case class Manager is a type of Manager and Employee both and the same with Server as well. It is an inheritance and polymorphism of OOP. Read about that here and here. You second code and first code both are correct. The way we use it depends.

Vinay
  • 6,891
  • 4
  • 32
  • 50
0

I can understand the confusion as what you're actually looking at in your first example is an object of type Employee that is actually named Manager (which is the literal name of the subclass).

So to answer your question (though I notice a distinct lack of questions and/or question marks in your question), yes, it is valid. Since Manager is a Employee and Server is a Employee, they simply have two Employee objects whose names clarify what they ought to be.

It actually would be syntactically correct in that example to say

Manager = new Server();

but that's obviously counter intuitive to the variables' names.

captainroxors
  • 718
  • 7
  • 18
0
public Class Restaurant{

   private Employee Manager;   //
   private Employee Server;    //


   public Restaurant(){

        //I DON"T UNDERSTAND THIS
        Manager = new Manager();
        Server = new Server();
   }
}

class Manager extends Employee{

}

class Server extends Employee{
}

Now, since Employee is the super class of Manager and server, we can say that "Every Employee is a Manager, every Server is a Manager".

Employee e = new Manager(); // is like "Pointing to a manager and saying that he is an employee. Which is correct. 

The actual object is determined at runtime. Remember, Not all methods/attributes of the "Service/manager" class can be accessed using an Employee reference.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Structure of the code has been discussed in detail through answers. But one thing that code elaborate is part does not exist without whole which is called composition. It is has a relationship with strong association. Other is aggregation in which part can exist without whole .

Composition

public Class Restaurant{

   private Employee Manager;   //
   private Employee Server;    //


   public Restaurant(){

        //I DON"T UNDERSTAND THIS
        Manager = new Manager();
        Server = new Server();
   }
}

Aggregation

public Class Restaurant{

   private Employee Manager;   //
   private Employee Server;    //


   public Restaurant(){
   }

   public setManager (){

        Manager = new Manager();
   }
   public setServer (){

    Server = new Server();
   }

}
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15