Inheritance in Java





Inheritance is one of the key feature of Object-oriented programming in Java. It allows user to create a new class (derived class) from an existing class(base class).

  • The mechanism of creating a new class from an old one is called inheritance (derivation).

  • The old class is referred as PARENT orBASE or SUPER class and the new class is called CHILD orDERIVED or SUB class.

  • The derive class inherits some or all of the members( properties ) from the base class.

  • The most important feature of inheritance is code re-usability .

  • Inheritance supports re-usability of code and is able to simulate the transitive nature of real life objects.

  • Inheritance implements the IS-A relationship.

  • A super class defines a general set of functionality, whereas subclasses define functionalities specific to them.

  • In other sense, inheritance in Java builds a hierarchy of classes & thus supports the concept of classification.

  • The keyword extends is used for inheritance.

Syntax:

class ChildClass extends SuperClass-name  
    {  
      //methods and fields 
    }

Example :

//super class
class Parent{
 public int x;
 void show ( ){
  System.out.println("show from Parent");
 }
}
//sub class
class Child extends Parent{
void display ( ){
  System.out.println("disp. from Child");
 }
}
class Test{
 public static void main(String []arg){
  Child ob = new Child();
/*Here, Child's object can 
  access all the member of Parent */
  ob.x = 50;
  ob.show( );
  ob.display( );
 }
}
OUTPUT:
show from Parent
disp. from Child

Types of Inheritance:
  • There are following types of inheritance ...
    1. Single Inheritance
    2. Hierarchical Inheritance
    3. Multilevel Inheritance
  • The above inheritance types are just organize the classes and establishes the relationship among classes to form the class hierarchy.
Note : Multiple inheritance is not supported in java through class.

1. Single Inheritance :

Single inheritance represents a form of inheritance when there is only one base class and one derived class. prowessapps.in

Example :

class Parent
 {
   //methods and fields 
 }
class Child extends Parent
 {
   //methods and fields 
 }          

2. Hierarchical Inheritance :

A class inherited by more than one class. prowessapps.in

Example :

class Parent
 {
   //methods and fields 
 }
class Child1 extends Parent
 {
   //methods and fields 
 }
class Child2 extends Parent
 {
   //methods and fields 
 }            

3. Multilevel Inheritance :

Derived class further inherited by another class.

In that case Derived class is become a base class for another class.

prowessapps.in

Example :

class Parent
 {
   //methods and fields 
 }
class Child extends Parent
 {
   //methods and fields 
 }
class GrandChild extends Child
 {
   //methods and fields 
 }

Points to Remember :

  • Private members of the super class are not accessible by the sub class and can only be indirectly accessed.

  • Constructors and Initializer blocks are not inherited by a subclass.

  • When you construct an object of child, the default base class constructor is called implicitly, before the body of the derived class constructor is executed. So, objects are constructed top-down under inheritance.

Example :

//base class
class Parent{
 private int x;
//base class constructor
  Parent ( ){
  System.out.println("Parent's Constructor");
 }
}
//derived class
class Child extends Parent{
//derived class constructor
 Child ( ){
  System.out.println("Child's Constructor");
 }
}
class Test{
 public static void main(String []arg){
  Child ob = new Child();
  ob.x = 50; //Compile ERROR
 }
}
OUTPUT : if remove the error
Parent's Constructor
Child's Constructor

Calling base class constructor from derived class constructor:
Implicitly every time default constructor gets executed whenever call the derived class constructor. But, you can explicitly call the other constructor of base class.

Syntax:

DerivedClassConstructor 
  {
    super(parameter); 
  }
super() must be very first statement in derived class constructor.
Example:
//base class
class Parent{
 private int x;
//base class constructor
  Parent (String s){
  System.out.println("Parent's Constructor");
  System.out.println(s);
 }
}
//derived class
class Child extends Parent{
//derived class constructor
Child ( ){
    super("Java Prowess");
  System.out.println("Child's Constructor");
 }
}
class Test{
 public static void main(String []arg){
  Child ob = new Child();
 }
}                       
OUTPUT :
Parent's Constructor
Java Prowess
Child's Constructor

Next topic is polymorphism




 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved