After asking the question Call a method that requires a derived class instance typed as base class in VB.NET or C# on Stack Overflow, I was informed that I had used the wrong terms when asking the question. I had used "parent" and "child" where I should have used "base" and "derived" instead.
I have been unable to find a good description of the difference.
This is what I know (or think I know) so far:
A parent class contains the child class. Where as a derived class inherits from a base class.
They are similar because the child (or derived) can access the parents (or base) properties and methods (where allowed).
They are different because you can refer to a property of the child class in the form of Parent.Child.Property
. Whereas you cannot do that with a derived class.
What is the difference and in what situation should one be used over the other?
Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class.
In C#, it is possible to inherit fields and methods from one class to another. We group the "inheritance concept" into two categories: Derived Class (child) - the class that inherits from another class. Base Class (parent) - the class being inherited from.
A parent-child relationship is a very strong relationship between two classes. Think of it as a "has a" or "containment" relationship. In a parent-child relationship: Every child class has a parent class.
parent and child are more abstract relations. They are used to describe hierarchy, and thus can be used in all kinds of trees (or sometimes DAGs).
The tree of class inheritance is one such tree, so calling them parent and child is not wrong.
This terminology is often used with other kinds of trees, such as nested GUI controls, directory structures,...
base and derived is used only for inheritance, and thus more precise. This terminology is preferred, since it's less ambiguous.
Parent/Child is used in both contexts. It can be used to describes a "contains" relationship as you mentioned (Parent.Child.Property
) or it can mean a derived class (also called a subclass).
Bottom line is - to understand what is meant by Parent/Child you have to know the context.
In any case, the difference between the two concepts (inheritance vs. encapsulation) can be thought of as a "is-a" and "has-a" relationship.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With