Inheritance in Java - Javastudypoint

Saturday, December 29, 2018

Inheritance in Java


Inheritance is one of the key features of object-oriented programming (OOPs). Inheritance is a process of inheriting the properties and behavior of the existing class into a new class. When we inherit the class from an existing class, we can reuse the methods and fields of the parent class. Inheritance can be defined as 
Is-A relationship, which is also known as the parent-child relationship.


Inheritance in Java
Inheritance

Why we use Inheritance?

  • The main purpose of inheritance is code reusability. We can reuse the code when we inherit the properties and behavior of the existing class into a new class.
  • The runtime polymorphism (method overriding)  can be achieved by inheritance.


Important terminologies in Inheritance:

Class: Class is used to describe the properties and behavior of an object. It is a blueprint of an object.

Super Class: Super Class is also known as a parent class or a base class. The super class is the class whose features are inherited from a subclass.

Sub Class: Sub Class is also known as a child class or a derived class or an extended class. Sub Class is the class which inherits the other class.

The syntax of Inheritance in Java:

class superclass
{
    // methods and fields
}
class subclass extends superclass
{
      // methods and fields
}

The extends is a keyword which is used to inherit the properties and behavior of a class.

Inheritance Example in Java:

File: Person.java
public class Person
{
private String name;
private int age;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public String getAge()
{
return age;
}
}
File: Student.java
public class Student extends Person
{
private int rollno;
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public int getRollno()
{
return rollno;
}
}
File: InheritanceExample.java
public class InheritanceExample
{
public static void main( String args [] )
{
Student st = new Student();
st.setRollno(100);
st.setName("Rahul");
st.setAge(21);
System.out.println("Student rollno: is: " +st.getRollno());
System.out.println("Student name is: " +st.getName());
System.out.println("Student age is: " +st.getAge());
}
}

Output:

Student rollno: is: 100
Student name is:  Rahul
Student age is:    21



Types of Inheritance in Java:

  • Java supports only three types of inheritance Single Inheritance, Multilevel Inheritance, and Hierarchical Inheritance.
  • Multiple and Hybrid Inheritance in java can be supported through interface only.

1. Single Inheritance: 

In single inheritance, one class can extend the functionality of another one class. In single inheritance only one parent and one child class is present.
Inheritance in Java
Single Inheritance

Syntax:

public class SuperClass
{
    // methods and fields
}
public class SubClass extends SuperClass
{
    // methods and fields
}


Example of Single Inheritance:

class Birds
{
public void birdsEat
{
System.out.println(" Birds are eating! ");
}

class Crow extends Birds
{
public void crowEat
{
System.out.println(" Crow is  eating! ");
}
}
class SingleInheritance
{
public static void main(String args [])
{
Crow obj = new Crow();
obj.birdsEat();   // Calling super class method
obj.crowEat();     // Calling sub class method
}
}


Output:

Birds are eating!
Crow is eating!



2. Multiple Inheritance:

In multiple inheritance, there is more than one parent class and one child class is present. The child class inherits from more than one parent class. Java doesn't support multiple inheritance. It can be supported through interface only.

Inheritance in Java
Multiple Inheritance


3. Multilevel Inheritance: 

In multilevel inheritance, there is more than one level. If one class can inherit from a derived class and derived class becomes the base class of the new class then it is called multilevel inheritance.


Inheritance in Java
Multilevel Inheritance

Syntax:

class A
{
  // methods and fields;
}
class B extends A
{
  // methods and fields
}
class C extends B
{
   // methods and fields
}


Example of Multilevel Inheritance:

class Birds
{
public void birdsEat
{
System.out.println(" Birds are eating! ");
}

class Crow extends Birds
{
public void crowEat
{
System.out.println(" Crow is  eating! ");
}
}
class BabyCrow extends Crow
{
public void babyCrowEat
{
System.out.println(" Baby Crow is  eating! ");
}

}
class MultilevelInheritance
{
public static void main(String args [])
{
BabyCrow obj = new BabyCrow();
obj.birdsEat();   
obj.crowEat();
obj.babyCrowEat();     
}
}

Output:

Birds are eating!
Crow is eating!
Baby Crow is eating!



4. Hierarchical Inheritance:

In Hierarchical inheritance, from a single parent class, we are inheriting multiple child class. In the below diagram, class A inherits the class B, class C, and class D.


Inheritance in Java
Hierarchical Inheritance

Syntax:

class A
{
  // methods and fields;
}
class B extends A
{
  // methods and fields
}
class extends A
{
   // methods and fields

}
class extends A
{
   // methods and fields
}



Example of Hierarchical Inheritance:

class Birds
{
public void birdsEat
{
System.out.println(" Birds are eating! ");
}

class Crow extends Birds
{
public void crowEat
{
System.out.println(" Crow is  eating! ");
}
}
class Sparrow extends Birds
{
public void sparrowEat
{
System.out.println(" Sparrow is  eating! ");
}

}
class MultilevelInheritance
{
public static void main(String args [])
{
BabyCrow obj = new BabyCrow();
obj.birdsEat();   
obj.crowEat();
obj.sparrowEat();     
}
}

Output:

Birds are eating!
Crow is eating!
Sparrow is eating!


5. Hybrid Inheritance: 

If we are using multiple types of inheritance simultaneously such types of inheritance are called Hybrid Inheritance. But java does not support Hybrid Inheritance. Hybrid Inheritance can be supported through interface only.


Inheritance in Java
Hybrid Inheritance

Why Java doesn't support multiple inheritance?

Java doesn't support multiple inheritance due to the ambiguity problem.

Consider a scenario where X, Y, and Z are the three classes. The Z class inherits both the class i.e. class X and class Y. If class X and class Y have the same method and we call it from child class object ( class Z object), there will be ambiguity to call the method of class X or a class Y. Therefore compiler show compile time error if we inherit 2 classes.


Inheritance in Java



class X
{  
public void show()
{
System.out.println("Hello");
}  
}
class Y
{  
public void show()
{
System.out.println("Welcome");
}  
}
class Z extends X,Y
{
 public static void main(String args[])
{  
  Z obj=new Z();  
  obj.show();      //Now which show() method would be invoked?  
}  
}  


You may also like:

Encapsulation in Java OOPs with Example.
Java Authentication and Authorization Service (JAAS)
MVC Architecture in JSP with Example
JSP Standard Tag Library - JSTL Tutorial 
Session Tracking in Servlet and its techniques
JSP Life Cycle. What are Java Beans?







No comments:

Post a Comment