Inheritance

Home   »    Java   »    Inheritance


Inheritance means using the Pre-defined Code This is very Main Feature of OOP With the advantage of Inheritance we can use any code that is previously created With the help of inheritance we uses the code that is previously Defined but always Remember We are only using that code but not Changing that code With the Advent of inheritance we able to use pre-defined code and also able to add new code . All the pre-defined code is reside into the form of classes if we wants to use that code then we have to inherit or extend that class The Class that is Pre-defined is called as Base or super Class and the class which uses that code is called as derived or sub class There many types of Inheritance provided by Java But Multiple Inheritance is not Provided by java. Various types of inheritance provided by java is as follows:

1) Single Inheritance (only one super class)
2) Multilevel Inheritance (Derived from a derived class)
3) Hierarchical Inheritance (One super class, many subclasses)

Inheritance - Extending a Class




Output of "ExtendClass.java"


Download Complete Program

  -  





Single Inheritance


Single Inheritance is that in which only one super or base class and only one Sub or derived Class and in Multilevel Inheritance the derived class is again inherited by another class and in the hierarchical inheritance one super class have many sub classes.






Output of "SingleInheritance"


Download Complete Program

  -  





Multilevel Inheritance


When we use the derived class as a super class then this is known as multilevel inheritance. Java supports this conecept and uses it extensively in building its class library. This concept allows us to build a chain of classes.

In multilevel hierarchy, each subclass inherits all of the traits found in all of its superclasses. It is perfectly acceptable to use a subclasses of another.







Output of "MultiLevelInheritance.java"


Download Complete Program

  -  





Hierarchical Inheritance

Hierarchical design of a program is a very feature of java for developing any kind of project. Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below the level.









Method Overriding

Method Overriding occurs in inheritance when there is Same Method in both the classes means when a super class and sub class a same method then the Sub class Which inherits the methods of super class will Execute his own Method Means it will overrides Method that is taken from super or base Class. This is Called Method Overriding In this Situation Super Keyword is used if we wants to Execute the Method of Base Class.





Output of "MethodOverriding.java"


Download Complete Program

  -  





<< Previous Topic
Next Topic >>

1 comment: