Saturday 23 March 2013

What is Inheritance ?



The mechanism of deriving a new class from an old one is called inheritance.  A class that is inherited (old class) is called a super class or base class or parent class.  The class that does the inheriting (new class) is called a subclass or derived class or child class.  Inheritance allows subclasses to inherit all the variables and methods of their parent classes.
          

  Inheritance is classified into different forms.


a) Single Inheritance: Derivation a subclass from only one super class is called Single Inheritance.

b) Hierarchical Inheritance: Derivation of several classes from a single super class is called Hierarchical Inheritance.


c) Multilevel Inheritance: Derivation of a class from another derived class s called Multilevel Inheritance.



 

d) Multiple Inheritance: Derivation of one class from two or more super classes is called Multiple Inheritance.  But Java does not support Multiple Inheritance directly.  It can be implemented by using Interface concept.


e) Hybrid Inheritance: Derivation of a class involving more than one form of Inheritance is called Hybrid Inheritance.




Defining a Subclass:  

A subclass is defined as
            Syntax:            class subclass-name extends superclass-name
                                    {
                                                      variable declaration;
                                                      method declaration;
                                    }
            The keyword extends signifies that the properties of the super class name are extended to the subclass name.  The subclass will now contain its own variables and methods as well as those of the super class.  But it is not vice-versa.

No comments:

Post a Comment