Inheritance doubts
I'm relatively new to OOP, so wanted to clear a few things,
I have the following piece of code
class Parent
{
public Parent()
{
Console.WriteLine("Parent Class constructor");
}
public void Print()
{
Console.WriteLine("Parent->Print()");
}
}
class Child : Parent
{
public Child()
{
Console.WriteLine("Child class constructor");
}
public static void Main()
{
Child ChildObject = new Child();
开发者_如何转开发 Parent ParentObject = new Child();
ChildObject.Print();
ParentObject.Print();
}
}
Output :
Parent Class Constructor
Child Class constructor
Parent Class Constructor
Child Class constructor
Parent->Print()
Parent->Print()
My questions are as follows :
1) Why is the base class constructor called when I instantiate objects with the ChildClass
constructor? without explicitly specifying the base
keyword. Is there any way to avoid calling the base class constructor?
2) why is ParentClass ParentObj = new ChildClass();
possible? and not the other way round.
All base class constructors all the way up inheritance tree will get called. Constructors are always chained starting with the parent class on down to the most derived class.
Instances of
ChildClass
"are" instances ofParentClass
. They model an "is-a" relationship, but not the other way around.
In a word, polymorphism.
By Child
inheriting Parent
, the child object takes on the characteristics of the parent. If you think of it in genetic terms, it may make more sense.
1) Why is the base class constructor called when I instantiate objects with the ChildClass constructor? without explicitly specifying the base keyword. Is there any way to avoid calling the base class constructor?
There is no way to get around the base class constructor being called (that I am aware of). The point of having the base class constructor called is to instantiate the base class (pass parameters, initialize other objects, assign values, etc.)
2) why is ParentClass ParentObj = new ChildClass(); possible? and not the other way round.
Because of polymorhism, Child
looks like Parent
and therefore may be instantiated as Parent
. Since Parent does not inherit Child, Parent does not look like Child and therefore may not be instantiated as Child
.
For what it's worth, using Parent and Child have different meanings. Typically, when referring to inheritance, Parent
is the base class, where Child
would be the derived or sub-type.
- Because the compilator inserts
: base()
if you don't specify otherwise.
You can explicitly call: base("foo")
if you have a constructorParent(string s)
, thenParent()
is not called. You can not skip the base class constructor calling all together. - Because
Animal animal = new Dog();
makes sense and notDog dog = new Animal();
A dog is always an animal, but not necessarily the other way around.
1) Probably confusing because you are using Child/Parent nomenclature. Because Child/Parent implies two different instances (an owner relationship). Inheritance is an Is-A relationship. A dog is an animal. So a dog class could inherit from animal.
2) Again, you can have an animal which happens to be a dog, but you only have an animal handle on it. Like you could be holding a leash and it could have an elephant or a flea on the other end, but a leash can hold any animal.
1) There are (or could be) private members in the base class which the child class knows nothing about, but are necessary. Hence the parent constructor is called first to cover this eventuality.
2) ChildClass
is a type of ParentClass
as Square
is a type of Rectangle
. That's the theory, anyway. That's why it doesn't make sense to have it the other way around.
1) In C#, if you don't specify a call to a different constructor using something like base(blah)
, it will call the default constructor. Which you've defined above.
2) Every ChildClass
will always be a ParentClass
. However, there may be objects that are a ParentClass
but are not a ChildClass
. For example, if you had an OtherChildClass : ParentClass
, it would be a ParentClass
, but it would not be a ChildClass
.
精彩评论