Sunday, 3 May 2020

Inheritance in Java

By using the inheritance in Java, you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Benefits of inheritance in java?

  • Method Overriding (so runtime polymorphism can be achieved).
  • Code Reusability.

Terms used in Inheritance:

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.

Syntax of Java Inheritance

class Subclass_name extends Superclass_name 

{ 

   //methods and fields 

} 

Example:

class Person{ 

 float salary=50000; 

} 

class Programmer extends Person{ 

 int bonus=5000; 

 public static void main(String args[]){ 

   Programmer p=new Programmer(); 

   System.out.println("Programmer salary is:"+p.salary); 

   System.out.println("Bonus of Programmer is:"+p.bonus); 

} 

} 


Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.

Single Inheritance


When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Example

class Animal{ 

void eat(){System.out.println("eating...");} 

} 

class Dog extends Animal{ 

void bark(){System.out.println("barking...");} 

} 

class Test{ 

public static void main(String args[]){ 

Dog d=new Dog(); 

d.bark(); 

d.eat(); 

}} 

Output:

 

barking...

eating...


Multilevel Inheritance

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

Example:

class Animal{ 

void eat(){System.out.println("eating...");} 

} 

class Dog extends Animal{ 

void bark(){System.out.println("barking...");} 

} 

class BabyDog extends Dog{ 

void weep(){System.out.println("weeping...");} 

} 

class TestInheritance2{ 

public static void main(String args[]){ 

BabyDog d=new BabyDog(); 

d.weep(); 

d.bark(); 

d.eat(); 

}} 

Output:

 

weeping...

barking...

eating...

 

 

Hierarchical Inheritance

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

Example

class Animal{ 

void eat(){System.out.println("eating...");} 

} 

class Dog extends Animal{ 

void bark(){System.out.println("barking...");} 

} 

class Cat extends Dog{ 

void meow(){System.out.println("meowing...");} 

} 

class Test{ 

public static void main(String args[]){ 

Cat c=new Cat(); 

c.meow(); 

c.eat(); 

c.bark();

}} 

Output:

 

meowing...

eating...

barking…






Why multiple inheritance is not supported in java?

In order to decrease the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.

 

class A{ 

void msg(){System.out.println("Hello");} 

} 

class B{ 

void msg(){System.out.println("Welcome");} 

} 

class C extends A,B{//suppose if it were 

  

 public static void main(String args[]){ 

   C obj=new C(); 

   obj.msg();//Now which msg() method would be invoked? 

} 

} 

Test it Now

 Compile Time Error


No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...