Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a child of a parent class and the derived of a base class in VB.NET or C#?

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?

like image 374
Gravitate Avatar asked Oct 23 '12 18:10

Gravitate


People also ask

What is the difference between child class and parent class?

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.

What is the difference between base class and 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.

What is parent class and child class in C#?

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.

What is the relationship between a parent and a child class?

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.


2 Answers

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.

like image 89
CodesInChaos Avatar answered Oct 21 '22 11:10

CodesInChaos


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.

  • A dog is an animal (inheritance)
  • A car has an engine (encapsulation)
like image 20
D Stanley Avatar answered Oct 21 '22 11:10

D Stanley