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

After asking a question. Call a method that requires an instance of a derived class, introduced as a base class in VB.NET or C # in Stack Overflow, I was informed that I used the wrong terms when asking the question. I used “parent” and “child”, where instead I would use “base” and “derived”.

I could not find a good description of the difference.

This is what I know (or think I know):

The parent class contains the child class. Where, as a derived class, the base class is inherited.

They are similar because the child (or derivative) can access the parent (or base) properties and methods (where allowed).

They are different because you can refer to a property of a child class in the form Parent.Child.Property . If you cannot do this with a derived class.

What is the difference and in which situation should I use another?

+8
c # parent-child derived-class
source share
8 answers

parent and child are more abstract relationships. They are used to describe the hierarchy and, therefore, can be used in all kinds of trees (or sometimes in DAG groups).
The class inheritance tree is one such tree, so calling them parent and child is not an error.
This terminology is often used with other types of trees, such as nested GUI controls, directory structures, ...

base and derivatives are used only for inheritance and therefore more accurate. This terminology is preferable because it is less ambiguous.

+6
source share

When a class is derived from a base class, it is called inheritance . You inherit when you want to add functionality to an existing component, or extend the component.

When a class refers to / contained in the parent class, it is called encapsulation . You encapsulate when your (usually parent) object 'uses' components.

From Ext - Inheritance vs. Encapsulation :

When do you inherit and when do you encapsulate? You inherit when you want to add functionality to an existing component. You encapsulate when your objects "use" components. You inherit if your new class is “Ext Component.” You encapsulate if your new class has an “Ext” Component.

Here is a link that details inheritance and encapsulation in object-oriented programming and discusses which concept is better in this situation.

+3
source share

Parent / Child is used in both contexts. It can be used to describe the "contains" relationship, as you mentioned ( Parent.Child.Property ), or it can mean a derived class (also called subclass ).

The bottom line is to understand what is understood by the parent / child, you need to know the context.

In any case, the difference between the two concepts (inheritance and encapsulation) can be considered as the relationship "is-a" and "has-a".

  • A dog is an animal (inheritance).
  • The car has an engine (encapsulation)
+3
source share

Produced in OOP implicitly determines the polymorphic relationship between types:

 public class A{ } public class AB : A{ } 

class AB is a derived class from A

Parent and child are the definition of abstract relationships, which in programming can take different forms, such as:

 public class A{ } public class ParentA{ List<A> children = ... } 

commonly used in Graph like relationships

+2
source share

Parent and child are associated with the principle of encapsulation of OO, while basic and derivatives are related to the principle of inheritance.

A child class in an encapsulated parent class that displays only the public methods of the child and does not have direct access to the parent.

A derived class has access to all properties, methods, and members of the base class that are displayed as protected or higher access modifiers.

0
source share

“Parent” is a synonym for “base,” and “child” is a synonym for “derivative,” but “parent” and “child” are unusual and not very technical (and not very good IMOs). The two other terms are “superclass” and “subclass”.

0
source share

Based on how you use Parent / Child, I think you mean nested classes.

 class Container { class Nested { } } 

A nested class is private by default. The goal here, as a rule, is that Nested will be the helper class used by Container . For example, Nested can be used by a method in a container that must return multiple values. A Nested or Container instance requires a reference to another to access any of its non-static elements.

Where as a derived class the base class is inherited

 class Base { } class Derived : Base { } 

A derived class has all the functionality of its base class and can be used wherever a base class can be used. The Derived instance has access to all public and protected non-stationary members. Base does not have access to any member of Derived . In addition, Derived can override the behavior of Base virtual members.

0
source share

Avoiding the parent-parent terminology, I just want to mention that a nested class can have its own containing ("external") class as a base class. I don't think this is a template that people use a lot, but it is allowed in the language (C #).

Example:

 class MyClass { // we choose to make the instance constructor private MyClass() { } // nested type, private to MyClass, deriving from MyClass class InnerMyClass : MyClass { // ... } public static MyClass GetMyClassInstance() { return new InnerMyClass(); } // ... } 
0
source share

All Articles