what is different between base class and child class?

A base class (or parent class, or superclass) is a class which one or more child classes inherits from; a child class (or sub-class) is one which inherits from a base class. Depending on the language, you may have some programmer-defined classes which have no base classes, or (more often) all programmer-defined classes may inherit implicitly from a global, abstract root class (often called Object).

When a class inherits from another class, it is as if the properties and methods of the base class are copied into the child class; the child class 'inherits' all the properties and behaviors of the parent, but may add to them, and can override (replace) some or all of the methods of the parent class.

An object reference (i.e., a variable) of the parent class type may refer to members of its child classes, in which case any message (method call) can be sent to the object that the parent class declares, but the method used will be the one of the child class. That is to say, if I have

class Foo 
    method a()
    method b()

class Bar of Foo
    method b()  // overrides Foo's b()
    method c()  // adds a new method c()

variable baz of Foo
variable quux of Foo

baz := new Foo()
quux := new Bar()

baz.a()    // this uses the Foo method a()
baz.b()    // this uses the Foo method b()

quux.a()    // this uses the Foo method a()
quux.b()    // this uses the Bar method b()
quux.c()    // in most languages this would be an error!

This is known as polymorphism.

In C++, where there is an explicit difference between an object variable and an object reference, you need to explicitly use pointers to objects to get polymorphic behavior; furthermore, the polymorphic methods have to be explicitly declared as virtual in order to get full polymorphism. In C#, variables of class types are always references, but you still need to declare polymorphic methods as virtual in order for it to be in the virtual method table. Most other languages automatically allow any method to be polymorphic.

In most languages, there are provisions for abstract classes, ones which cannot be directly instantiated. In those cases, an object of the abstract class type can only refer to objects of a child class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.