Saturday, 7 December 2013

Inheritance in Java


 Inheritance in simple language means to have something in common in child with parent. Inheritance in java also has the same concept like a child inherit some characteristics and surname from parent. It is one of the basic OOP concepts used in java along with Encapsulation, Polymorphism and Abstraction.

            Inheritance is a way to define IS-A relationship between classes. Inheritance is used when we have to design a new class and this new class needs some code which another class already has. In implementing this, the fields and the methods can be reused without writing them again.

        In Java, inheritance is achieved through extends and impelements keywords. By using these keywords properties of one object can be acquired in another object. The class from which n new class is derived is known as subclass and the class from which subclass is derived is called superclass.

For example:
Consider the Animal as a base class and Cat as the derived class from it and follow the code written below:


Base Class : Animal

Child class : Cat

Main Class
The output for this program would be :

                                          Legs :4
                                          sounds as meow

Diagrammatically, inheritance is represented as below:

Inheritance is represented using an arrow
Types Of Inheritance:
There are three types of inheritance in java.
1. Single
2. Multilevel
3. Hierarchical

As shown in below diagram:

Types of Inheritance in Java



No comments:

Post a Comment