Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor Parameters and Inheritance

Tags:

New to OOP and I'm confused by how derived-class constructors work when inheriting from a base class in C#.

First the base class:

class BaseClass {     private string BaseOutput = null;      public BaseClass(string BaseString)     {         BaseOutput = BaseString;     }      public virtual void PrintLine()     {         Console.WriteLine(BaseOutput);     } } 

Here is the derived class:

class SubClass : BaseClass {     private string SubOutput = null;      public SubClass(string BaseString, string SubString) : base(BaseString)     {         SubOutput = SubString;     }      public override void PrintLine()     {         Console.WriteLine(SubOutput);     } } 

Finally, the main part of the program:

class Program {     static void Main(string[] args)     {         BaseClass theBase = new BaseClass("Text for BaseClass");         SubClass theSub = new SubClass("2nd param", "Text for SubClass");          theBase.PrintLine();         theSub.PrintLine();          Console.ReadKey();     } } 

What I don't get is why, when calling the constructor for the derived class, I have to also pass the parameter that the base class needs. Shouldn't the BaseOutput field in the derived class just stay set to null if no value is assigned to it? Why can't something like this work:

public SubClass(string SubString) : base(BaseString) 

Furthermore, when calling the constructor in this derived class, the first parameter has to be named the same as the one in the base class or else it throws an error. If I were to define a new string variable called AnotherString in the derived class, why wouldn't this work:

public SubClass(string AnotherString, string SubString) : base(BaseString) 

Lastly, when you do this the right way and type out this...

public SubClass(string BaseString, string SubString) : base(BaseString) 

...what is the first parameter in the SubClass constructor used for? It's not being assigned or used in any methods for my derived class. Why do I have to even put it there at all?

like image 972
Frank Avatar asked Nov 23 '12 23:11

Frank


People also ask

How do constructors work with inheritance?

Constructor is automatically called when the object is created. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can derive from several(two or more) base classes. The constructors of inherited classes are called in the same order in which they are inherited.

What is difference between constructor and inheritance?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Is constructor an inheritance?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

Are constructors inherited in C#?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.


1 Answers

As to why you can't do:

public SubClass(string SubString) : base(BaseString) 

What would BaseString be?

You could do:

public SubClass(string SubString) : base("SomeFixedString") 

or

public SubClass(string SubString) : base(SubString) 

But if you want to pass one string to the base class constructor's parameter and have an additional one, you'll need to accept two parameters.

As to keeping the same name, you don't. You could do:

public SubClass(string AnotherString, string SubString) : base(AnotherString) 

As to the last question, the first parameter isn't doing nothing, it's being passed to the base class constructor. You could use it for something else if you wanted to.

like image 80
Servy Avatar answered Sep 24 '22 13:09

Servy