10

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?

Community
  • 1
  • 1
Gravitate
  • 2,885
  • 2
  • 21
  • 37
  • 2
    You shouldn't edit the answer into the question. Just upvote/accept the answer(s) that most helped you. If none of them truly answer your question then answer it yourself with your own answer. – Servy Oct 23 '12 at 19:24
  • @Servy Sorry, have removed the update. I have already up-voted helpful answers and I will be marking an answer. – Gravitate Oct 23 '12 at 19:31

8 Answers8

8

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Although, Paul Sasik and D Stanley's answers are possibly more helpful (in a general sense.) You have answered the question as it was asked (even if I didn't fully understand what I was asking at the time.) – Gravitate Oct 23 '12 at 21:46
5

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)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 1
    Thank you for this, I particularly like your last sentence. That is a good way to think of the difference.... and I find I have been using them quite wrongly.. :S – Gravitate Oct 23 '12 at 19:13
  • @Gravitate - well, I wouldn't say you've been using them "wrongly". As I and others have said, the terms are used to mean both things, so you just need to be clear what you _mean_ when you say parent/child. – D Stanley Oct 23 '12 at 19:31
  • No, I mean I have been using the wrongly in my code. I have used encapsulation where I should have used inheritance. – Gravitate Oct 23 '12 at 19:36
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.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • That's not necessarily true, one can use "parent" as a synonym for "base" (ditto for child/derived). But like I said in my answer parent/child are not technical terms so one can use them in different senses. – user1610015 Oct 23 '12 at 18:50
  • 3
    @PaulSasik: You can't truly disambiguate the issue, as the nature of the problem is that usage of those terms *is ambiguous*. What you've pointed out is one (of several) other possible uses of the terms "parent" and "child". The answer is not that the OP's choice of terms was wrong, but rather that his choice of terms was ambiguous and he should use terms that are *un*ambiguous. – Adam Robinson Oct 23 '12 at 18:55
  • @AdamRobinson: Agreed. parent/child/base are ambiguous. The key, I think, is to specify whether or not you're talking in the context of inheritance or encapsulation. Then the terms become more concrete. – Paul Sasik Oct 23 '12 at 19:00
  • @PaulSasik: Yes, but the terms are not limited to those two ideas. For instance, the OP inferred that parent/child related to nested classes, which it certainly could. – Adam Robinson Oct 23 '12 at 19:02
  • Quote: _A child class cannot access its parent unless it has a reference to its parent and even then, it can only access the public members of the parent._ That's misleading in my opinion. In C#, a class `Inner` which is nested inside another class `Outer` has access to the private members of `Outer`. – Jeppe Stig Nielsen Oct 23 '12 at 19:05
  • @JeppeStigNielsen: You're both half correct. A nested class DOES need a reference to the outer class (unlike a nested [non-static] class in Java), but you are right that it can access private members of the containing class. – Adam Robinson Oct 23 '12 at 19:08
  • @JeppeStigNielsen: Good catch. I updated my answer to make those statements more specific. – Paul Sasik Oct 23 '12 at 19:08
  • @AdamRobinson Correct. If class `Inner` is nested inside a class `Outer`, then, considered as a member of `Outer`, the `Inner` is a static member of `Outer`. This means that you can say `Outer.Inner` to reference the type. But if you have `var outerInstance = new Outer();`, then there's nothing called `outerInstance.Inner`. Even if `Outer` is marked `static`, it can contain a nested class, and this also convinces that nested types are static members of their containing type. So for a nested class to use **non-static** members of the containing class, it surely needs a reference to an instance. – Jeppe Stig Nielsen Oct 23 '12 at 19:29
  • @PaulSasik You still claim that the nested class can only access public members of the containing class. That's wrong. It can access all members of the containing class. But it needs an instance of the outer class to access those members that are non-static, because the nested class does not belong to any specific instance of the outer class. (I'm talking C# here.) – Jeppe Stig Nielsen Oct 23 '12 at 19:32
  • @JeppeStigNielsen: I'm not talking about nested classes here. I think the OP is trying to figure out the difference between inheriting and plain old encapsulation, eg. parent class has a reference to one or more "child" classes. Yes, there are several other ways of relating classes, including nested classes but those are more rare in practice (my experience.) – Paul Sasik Oct 23 '12 at 19:50
  • I simplified the answer. Originally it contained notes about how a child class might access a parent in encapsulation and inheritance scenarios. This was an attempt to keep with the spirit of the question since the OP specifically asked about children accessing parents. But that made for less clarity... it's gone. – Paul Sasik Oct 23 '12 at 19:55
2

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

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

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.

Maess
  • 4,118
  • 20
  • 29
  • To the person who gave a -1, care to comment? – Maess Oct 23 '12 at 18:56
  • 1
    -1 Because your answer is incorrect. See the answers by Tigran and CodeInChaos for the correct answer. Parent/child does not specifically refer to encapsulation any more than it refers to inheritance. – Adam Robinson Oct 23 '12 at 19:00
  • That's a matter of interpretation. – Maess Oct 23 '12 at 19:06
  • In a sense, I suppose the correctness of any question is a matter of interpretation, but your statement that "Parent and child have to do with the OO principle of encapsulation", the statement upon which your whole answer is based, is incorrect. Yes, those terms *can* refer to that idea, but they can also refer to other ideas. – Adam Robinson Oct 23 '12 at 19:09
0

"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".

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • No. Sorry but they are definitely different. At least in .net they are. – Gravitate Oct 23 '12 at 18:48
  • 2
    @Gravitate: .NET does not have its own object-oriented nomenclature. – Adam Robinson Oct 23 '12 at 18:48
  • @Gravitate Where in .NET are the words parent/child used? I've heard of parent/child forms, but not of parent/child classes. – user1610015 Oct 23 '12 at 18:55
  • Sorry, am I using Parent/Child as specific terms when in fact they can be used to describe any relationship ie. inheritance OR encapsulation? I assumed Parent/Child was used to describe encapsulation whereas Base and Derived was used to describe inheritance? Is that incorrect? – Gravitate Oct 23 '12 at 18:58
  • 1
    @Gravitate: That is incorrect; see Tigran's or CodeInChaos' answer. Parent/child is ambiguous and simply means there is some sort of hierarchical relationship between two things, whatever the nature of that relationship may be (inheritance, encapsulation, nested classes, etc.). – Adam Robinson Oct 23 '12 at 19:01
0

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.

cadrell0
  • 17,109
  • 5
  • 51
  • 69
0

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

  // ...
}
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181