2/21/2019

Inheritance in Java- Types and Applications

The Java development services have been progressing for many years and we can definitely see a huge rise in the future. There is plenty of Java uses in your application; among them, inheritance is a process in which one object gains all the properties and behaviors of a parent object. Types of Inheritance in Java and the Applications of Inheritance in Java Language. We Are Going to Discuss Each and Single Details with the Help of Syntax, Examples, and Images. Inheritance is considered as a crucial part of the Object Oriented programming system. You may think about the application of inheritance in Java. Not to worry as we have got some quick summary of the Java inheritance and how to utilize it in your next project.

Things to Remember: Parent or Super class for all classes are Object Class that is present in java.lang Package.

The main idea behind the inheritance in Java is that you are allowed to create new classes upon the existing classes. By reusing the methods or fields of the parent class, you can inherit from an existing class and also add new methods or fields in your current class. Generally, the inheritance is known as a parent-child relationship that is represented by the IS-A relationship. There are duly two main reasons why you should opt for inheritance in Java.


Let us look at the basic Java Inheritance.

Syntax:

class Subclass-name extends Superclass-name
{
// methods and fields
}

The Java inheritance also consists of few terminologies which is mostly used while inheriting the classes. Go through this given terms to understand the concept in a better way.

Class -A group of objects that has common properties is defined as a class. Objects are created from a class.

Child Class -A class which inherits the other class. Also known as a subclass, derived class or extended class.

Parent Class -A class from where a subclass inherits the features. Also known as superclass or base class.

Reusability -It is a mechanism that allows you to reuse the fields and methods of the existing class when creating a new class. You can also use the same fields and methods that are already defined in the previous class.

Inheritance in Java

Now, it's the time to dive in different types of Java inheritance along with its applications.

Types and Applications of Inheritance in Java


  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance
Note: Java does not support Hybrid and Multiple Inheritance at class level because of the ambiguity problem.

1. Single Inheritance in Java


It is one of the easy inheritance to understand and implement as well. When a parent class gets extended by a child class then we call it a single inheritance. It is direct to direct connection defined as in the figure.

Single Inheritance in Java

Example of Single Inheritance:

Class A
{
public void method A()
{
System.out.println("Parent class");
}
}


Class B extends A
{
public void method B()
{
System.out.println("Child class");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class
obj.methodB(); //calling sub class
}
}

2. Multilevel Inheritance in Java


A mechanism in an object-oriented language where you can inherit from a derived class thereby making this derived class the parent class for the new class is defined as a multilevel inheritance. Hence, in Java, a class cannot directly access the members of the grandparents.

Multilevel Inheritance in Java

Example of Multilevel Inheritance:

Class X
{
public void method X()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void method Y()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class
obj.methodY(); //calling base class
obj.methodZ(); //calling local method
}
}

3. Hierarchical Inheritance in Java


When one class is inherited by many subclasses, it is said to have a hierarchical inheritance. A better pictorial representation helps you to understand better.

Hierarchical Inheritance in Java


Example of Hierarchical Inheritance:

class A
{
public void method A()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void method B()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void method C()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void method D()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();


obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}

4. Hybrid Inheritance in Java


A hybrid inheritance is a combination of single and multiple inheritances as depicted in the below-given figure. It can be achieved in the same way as the multiple inheritances in java. With the help of interfaces, you can have multiple as well as a hybrid inheritance in java.

Hybrid Inheritance in Java

Example of Hybrid Inheritance:

class C
{
public void display()
{
System.out.println("C");
}
}


class A extends C
{
public void display()
{
System.out.println("A");
}
}

class B extends C
{
public void display()
{
System.out.println("B");
}

}

class D extends A
{
public void display()
{
System.out.println("D");
}
public static void main(String args[]){

D obj = new D();
obj.display();
}
}

5. Multiple Inheritance in Java


When a child class inherits or extends more than one parent class, it is said to have multiple inheritances. There is an issue when it comes with multiple inheritances as the derived class will have to manage the dependency on two base class. It is mandatory to know that multiple inheritances are used very rarely in software projects as it can often lead to problems in the hierarchy. This might end up in unwanted complexity when further you want to extend the class. All of the new object-oriented languages like Small Talk, Java, and C# does not support multiple inheritances while it is fully supported in C++.

Multiple Inheritance Example

Author Bio:


Ava Barker working as a Technology Consultant at Tatvasoft UK which provides java development services in London. Coming from a technology background she likes to share her insights about development, design and more. She has also published her author bylines on many different publications online.

1 comment:

  1. GTM Plus will help you with this, because they have skilled people in the field.

    ReplyDelete