Inheritance

Inheritance is the ability of a subclass to take on the general properties of superclasses in the inheritance chain. The properties then form part of the subclass’ definition. Inheritance enables superclasses’ properties to be propagated downward to the subclasses in a class hierarchy, and makes the properties available as part of the subclasses’ definition. These properties are said to be inherited (or taken on) by the subclasses.

Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.

Inheritance allows us to reuse of code, it improves reusability in your java application.

Syntax
class XYZ extends ABC
	{
	}
Example
class Developer {
	   String designation = "Developer";
	   String companyName = "Ducat";
	   void does(){
		System.out.println("IT Sector");
	   }
	}
	public class PhpDeveloper extends Developer{
	   String mainSector = "Php";
	   public static void main(String args[]){
		PhpDeveloper obj = new PhpDeveloper();
		System.out.println(obj.companyName);
		System.out.println(obj.designation);
		obj.does();
	   }
	}
Output

Ducat

Developer

Php

IT sector

In above example, we have a base class Developer and a sub class PhpDeveloper. Since class PhpDeveloper extends the designation and company properties and work() method from base class, we need not to declare these properties and method in sub class.

Here we have companyName, designation and work() method which are common to all the Developers so we have declared them in the base class, this way the child classes like JavaDeveloper, WordPressDeveloper and HtmlDeveloper do not need to write this code and can be used directly from base class.

Example 2
class Developer {
	   private String designation = "Developer";
	   private String companyName = "Ducat";
	   public String getDesignation() {
		return designation;
	   }
	   protected void setDesignation(String designation) {
		this.designation = designation;
	   }
	   protected String getCompanyName() {
		return companyName;
	   }
	   protected void setCompanyName(String companyName) {
		this.companyName = companyName;
	   }
	   void does(){
		System.out.println("IT sector");
	   }
	}
	public class JavaExample extends Developer{
	   String mainSector = "Php";
	   public static void main(String args[]){
		JavaExample obj = new JavaExample();
		/* Note: we are not accessing the data members
		 * directly we are using public getter method
		 * to access the private members of parent class
		 */
		System.out.println(obj.getCompanyName());
		System.out.println(obj.getDesignation());
		System.out.println(obj.mainSector);
		obj.does();
	   }
	}
Output

Ducat

Developer

Php

IT sector

In Example 2, the important point to note in the above example is that the child class is able to access the private members of parent class through protected methods of parent class. When we make a instance variable (data member) or method protected, this means that they are accessible only in the class itself and in child class.

Types of Inheritance

Types of Inheritance in Java

  • Single Inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple Inheritance
  • Hybrid inheritance
Single Inheritance

Single inheritance refers to child and parent class relationship. In Single inheritance class extends another class.

Multilevel Inheritance

Multilevel inheritance refers to child and parent class relationship. In Multilevel inheritance class extends the child class. Like class C extends class B extends class A.

Hierarchical Inheritance

Hierarchical inheritance refers to child and parent class relationship. In Hierarchical inheritance, more than one classes extends the same class. Like classes B, C, D extends the same class A.

Multiple Inheritance

In Multiple Inheritance refers to one class extends more than one classes means one child class has two parent class. Like class C extends both class A and B. But in Java it does not support multiple inheritance.

Hybrid Inheritance

Combination of more than one types of inheritance in a single program. For example class A & B extends class C and another class D extends class A then this is a hybrid inheritance example because it is a combination of single and hierarchical inheritance.

Copyright 1999- Ducat Creative, All rights reserved.