What is the difference between a child of a parent

2020-07-06 02:04发布

问题:

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?

回答1:

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.



回答2:

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)


回答3:

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 is referenced by/contained in a 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 object 'uses' components. You inherit if your new class "is a" Ext Component. You encapsulate if your new class "has a" Ext Component.

Here is a link that takes a look at inheritance and encapsulation in object oriented programming in detail and discusses which concept is better in which situation.



回答4:

Derived in OOP esplicitly defines polymorphic relationship between types:

public class A{
}

public class AB : A{
}

class AB is derived class from A.

Parent and Child is a definiton of abstract relationship, that in programming can get different shapes like:

public class A{
}

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

usually used in Graph like relationships



回答5:

Parent and child have to do with the OO principle of encapsulation, whereas base and derived have to do with the principle of inheritance.

A child class in encapsulated by the parent class, exposing only public methods of the child and having no direct access to the parent.

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



回答6:

"Parent" is a synonym for "base" and "child" is a synonym for "derived", but "parent" and "child" are uncommon and not very technical (and don't sound very good IMO). Two other terms are "superclass" and "subclass".



回答7:

Based on the way you are using Parent / Child, I think you mean nested classes.

class Container
{
    class Nested
    {
    }
}

A nested class is private by default. The purpose here is typically that Nested will be a helper class used by Container. For example, Nested could be used by a method in Container that needs to return multiple values. An instance of Nested or Container would require a reference to the other to access any of its non-static members.

Where as a derived class inherits from a base class

class Base
{
}

class Derived : Base
{
}

A derived class has all of the functionality of its base class and can be used anywhere the base class can be used. An instance of Derived has access to all of the public and protected non-static members. Base does not have access to any members of Derived. Additionally, Derived can override the behavior of virtual members of Base.



回答8:

Avoiding the terminology parent/child I just want to mention that it is possible for a nested class to have its own containing ("outer") class as its base class. I don't think it's a pattern people use very much, but it's allowed in the language (C#).

An 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();
  }

  // ...
}