12 Replies - 657 Views - Last Post: 05 August 2010 - 04:00 PM Rate Topic: -----

#1 BigDelicious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Calling the constructor of a base class... Why?

Posted 05 August 2010 - 01:49 PM

Hey all,

I basically have a question that I haven't been able to figure out (At least not its usefulness). Calling a constructor of the base class within a derived class. Take this code below for example (Obtained from MSDN):

public class Manager : Employee
{
public Manager(int annualSalary)
: base(annualSalary)
{

}
}

Why would the derived class "Manager" that already has an annualSalary variable use the base class "Employee"'s annualSalary instead?

Any insight would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling the constructor of a base class... Why?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 01:52 PM

Usually the derived class would inherit that data from the base class. In that particular snippet, Manager doesn't have "its own", it is copying the value of the parameter to its private data member by means of the base class constructor.


I'm assuming it would like this:


public class Employee{
     private annualSalary;
     public Employee(int salary){
          annualSalary = salary;
     }
}


//...
public class Manager : Employee{
     public Manager(int annualSalary)
       : base(annualSalary){

     }
}


Was This Post Helpful? 1
  • +
  • -

#3 BigDelicious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 01:57 PM

Wouldn't it make more sense to have the derived class handle it without the base class? Or am I getting it all wrong?

View PostKYA, on 05 August 2010 - 12:52 PM, said:

Usually the derived class would inherit that data from the base class. In that particular snippet, Manager doesn't have "its own", it is copying the value of the parameter to its private data member by means of the base class constructor.


I'm assuming it would like this:


public class Employee{
     private annualSalary;
     public Employee(int salary){
          annualSalary = salary;
     }
}


//...
public class Manager : Employee{
     public Manager(int annualSalary)
       : base(annualSalary){

     }
}


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 02:01 PM

That would depend on what else is going on. One of the main concepts of OOP is code reusability. Why go through inheritance if you're going to have each class do its own thing?

The 'IS A' relationship indicates that the derived class has some properties of the base.
Was This Post Helpful? 0
  • +
  • -

#5 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 02:02 PM

View PostKYA, on 05 August 2010 - 07:52 PM, said:


public class Employee{
     private annualSalary;
     public Employee(int salary){
          annualSalary = salary;
     }
}


//...
public class Manager : Employee{
     public Manager(int annualSalary)
       : base(annualSalary){

     }
}



I thought private variables of superclass can't be inherited down to its subclass?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 02:05 PM

The general reason to call a base class's constructor is to have the derived constructor perform the same tasks (and possibly more) as the base constructor does, without recoding it all.

In this simplistic example, it wouldn't matter if you called the base constructor. You could recreate that functionality very simply. However, if you are deriving from a very complex class, you may not want to re-write a ton of code.

A better reason, though, is if you're dealing with a class that you don't have the source to. Let's say you need to write a derivative of System.IO.Stream. You don't know the exact process the constructor goes through for setup, you just know the end result. It's better to allow the stream to do it's normal setup, then run your custom code, instead of trying to figure it out and re-create the wheel.

View PostCuzzie, on 05 August 2010 - 08:02 PM, said:

View PostKYA, on 05 August 2010 - 07:52 PM, said:


public class Employee{
     private annualSalary;
     public Employee(int salary){
          annualSalary = salary;
     }
}


//...
public class Manager : Employee{
     public Manager(int annualSalary)
       : base(annualSalary){

     }
}



I thought private variables of superclass can't be inherited down to its subclass?


No, but public properties can.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 02:39 PM

You could keep it private:

class Base{
private:
	int data;
public: 
	Base(int d):data(d)		{};
	int getData()			{return data;};
};

class Derived : public Base{
public:
	Derived(int d):Base(d)		{};
};


Do access and modification through accessors and mutators respectively.

If the derived class has to play with those variables you could make them protected.
Was This Post Helpful? 0
  • +
  • -

#8 BigDelicious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 03:17 PM

Alright, Say I am in the main part of the program and instead of creating a Employee object I create a Manager Object. How would the mechanics work since the Manager constructor invokes the Employee base class first but I never created one.

I appreciate everyones help but I am still lost. Maybe the example I used is a bad one.

If you can use a better example to help me figure it out I would be in heaven. Kind of in a infinte loop in my brain at the moment lol.


View PostBigDelicious, on 05 August 2010 - 12:57 PM, said:

Wouldn't it make more sense to have the derived class handle it without the base class? Or am I getting it all wrong?

View PostKYA, on 05 August 2010 - 12:52 PM, said:

Usually the derived class would inherit that data from the base class. In that particular snippet, Manager doesn't have "its own", it is copying the value of the parameter to its private data member by means of the base class constructor.


I'm assuming it would like this:


public class Employee{
     private annualSalary;
     public Employee(int salary){
          annualSalary = salary;
     }
}


//...
public class Manager : Employee{
     public Manager(int annualSalary)
       : base(annualSalary){

     }
}


Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 03:22 PM

Manager 'IS A' Employee. The base constructor is implicitly called. Explicitly calling it means you want certain things to happen.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4316
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 03:39 PM

Maybe I can explain this a bit better. A Manager is both an Employee and a Manager. The blueprints for a Manager should contain all the instructions to create an Employee, and then extra instructions to create a Manager. Calling the base constructor is telling the instance of the manager to create itself as an employee. Then proceed with your extra instructions.

For instance, lets assume Manager contains an Employee[] of all the Employees that work with him. Employee contains a double Salary.

Every employee, even managers will have salaries. So we define setting that logic in the Employee constructor. Not everyone will have a list of employees, only Managers. So we define that logic in Manager. But Manager is also an Employee, so we need to set up Manager's salary. Well, creating an Employee already sets the salary, so we call the base constructor to do this. Then we go about our extra steps to create a manager.

As an aside, if your base class doesn't have a default constructor (one that is parameterless), you have to call the base constructor explicitly. If it has a default, and you don't explicitly call the constructor, it is called for you.
Was This Post Helpful? 0
  • +
  • -

#11 BigDelicious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 03:44 PM

I get the 'IS A' & 'HAS A' parts of OOP. What I dont get is my original question. Since Manager is already a derived class of Employee, why is it calling the base constructor for? Doesnt the Manager object have access to all the Employee methods?




View PostKYA, on 05 August 2010 - 02:22 PM, said:

Manager 'IS A' Employee. The base constructor is implicitly called. Explicitly calling it means you want certain things to happen.

Was This Post Helpful? 0
  • +
  • -

#12 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,409
  • Joined: 04-October 09

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 03:59 PM

View PostBigDelicious, on 05 August 2010 - 01:44 PM, said:

I get the 'IS A' & 'HAS A' parts of OOP. What I dont get is my original question. Since Manager is already a derived class of Employee, why is it calling the base constructor for? Doesnt the Manager object have access to all the Employee methods?

It's to stop duplication of code. Each constructor does exactly the same thing with the passed parameter, why write new code to do it when you already have perfectly good code written?

When you create an object that is derived from another object, that objects base constructor is automatically called. When you explicitly tell it to call a different base constructor (for example, one that takes parameters) then that one is called instead. For example, take these classes:
    class A {
        public A() {
            Console.WriteLine("This is constructor A()");
        }

        public A(int i) {
            Console.WriteLine("This is constructor A(int) and you passed {0}", i);
        }
    }

    class B : A {
        public B() {
            Console.WriteLine("This is constructor B()");
        }

        public B(int i) {
            Console.WriteLine("This is constructor B(int) and you passed {0}", i);
        }
    }

    class C : A {
        public C() {
            Console.WriteLine("This is constructor C()");
        }

        public C(int i)
            : base(i) {
            Console.WriteLine("This is constructor C(int) and you passed {0}", i);
        }
    }


And then this program using these classes:
    class Program {
         static void Main(string[] args) {
             Console.WriteLine("Lets do the A's");
             A a = new A();
             a = new A(1);

             Console.WriteLine();
             Console.WriteLine("Lets do the B's");
             B b = new B();
             b = new B(2);

             Console.WriteLine();
             Console.WriteLine("Lets do the C's");
             C c = new C();
             c = new C(3);

            Console.ReadLine();
        }
    }


The output you get is:
Lets do the A's
This is constructor A()
This is constructor A(int) and you passed 1

Lets do the B's
This is constructor A()
This is constructor B()
This is constructor A()
This is constructor B(int) and you passed 2

Lets do the C's
This is constructor A()
This is constructor C()
This is constructor A(int) and you passed 3
This is constructor C(int) and you passed 3


Notice when we called either of the B class constructors it automatically called the A class constructor. In the case of the class C, it called the default when we didn't specify, and the 2nd one when we did.
Was This Post Helpful? 1
  • +
  • -

#13 BigDelicious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-August 10

Re: Calling the constructor of a base class... Why?

Posted 05 August 2010 - 04:00 PM

Whoa Thanks!

I know have a clearer picture. KYA thanks to you as well but I think you are a bit advanced for my current skillset and now that I read them both I have a better grasp then I did before.

So basically it calls the Employee contructor that would contain the Managers Name, Address and other data and then the Manger class will add like a Team Name, EmployeeInTeam[] type of thing to the Manager?


View PostinsertAlias, on 05 August 2010 - 02:39 PM, said:

Maybe I can explain this a bit better. A Manager is both an Employee and a Manager. The blueprints for a Manager should contain all the instructions to create an Employee, and then extra instructions to create a Manager. Calling the base constructor is telling the instance of the manager to create itself as an employee. Then proceed with your extra instructions.

For instance, lets assume Manager contains an Employee[] of all the Employees that work with him. Employee contains a double Salary.

Every employee, even managers will have salaries. So we define setting that logic in the Employee constructor. Not everyone will have a list of employees, only Managers. So we define that logic in Manager. But Manager is also an Employee, so we need to set up Manager's salary. Well, creating an Employee already sets the salary, so we call the base constructor to do this. Then we go about our extra steps to create a manager.

As an aside, if your base class doesn't have a default constructor (one that is parameterless), you have to call the base constructor explicitly. If it has a default, and you don't explicitly call the constructor, it is called for you.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1