Inheritance in Java: Definition and Examples
Table of Content:
What is Inheritance?
In object-oriented programming, inheritance allows a subclass (child class) to inherit attributes (properties) and methods (behaviors) from a superclass (parent class).
Inheritance in Java is a mechanism in which a subclass acquires all the properties (attributes) and behaviors (methods) of its parent class.
Explanation:
-
Properties: In this case, properties typically refer to attributes or fields that define the state of an object. For example, if a parent class has fields like
name
,age
, andcolor
, these fields are the properties of the parent class. -
Behaviors: This refers to the methods (functions) defined in the parent class. The child class inherits the methods of the parent, allowing it to perform actions or behaviors defined by the parent class.
Using inheritance, we can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.
The logic behind inheritance in java is that we can create new classes that from existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
No | Term | Definition |
---|---|---|
1 | Inheritance | Inheritance is a process where one object acquires the properties of another object |
2 | Subclass | Class which inherits the properties of another object is called as subclass |
3 | Superclass | Class whose properties are inherited by subclass is called as superclass |
4 | Keywords Used | extends and implements |
Types of Inheritance in Java
In OOP, there are majorly following types of Inheritance which are:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Purpose of Inheritance
- code reusability: the same methods and variables which are defined in a parent/super/base class can be used in the child/sub/derived class.
- It promotes polymorphism by allowing method overriding.
Disadvantages of Inheritance
- The main disadvantage of using inheritance is that the two classes (parent and child class) get tightly coupled.
- This means that if we change the code of parent class, it will affect to all the child classes which are inheriting/deriving the parent class, and hence, it cannot be independent of each other.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name { //methods and fields }
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. It indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
In the terminology of Java, a class which is inherited is called parent or super class and the new class is called child or subclass.
Let us see how extends keyword is used to achieve Inheritance.class Vehicle. { ...... } class Car extends Vehicle { ....... //extends the property of vehicle class. }
Now based on above example. In OOPs term we can say that,
- Vehicle is super class of Car.
- Car is sub class of Vehicle.
- Car IS-A Vehicle.
Simple example of Inheritance
Program:class Parent { public void pMethod() { System.out.println("Parent method"); } } public class Child extends Parent { public void cMethod() { System.out.println("Child method"); } public static void main(String[] args) { Child cobj = new Child(); cobj.cMethod(); //method of Child class cobj.pMethod(); //method of Parent class } }Output:
Child method Parent method Press any key to continue . . .
Another example of Inheritance
This program will show you how to access parent class member or instance variable
Program:class Vehicle { String vehicleType; } public class bike extends Vehicle { String modelType; public void showDetail() { vehicleType = "bike"; //accessing Vehicle class member modelType = "sports"; System.out.println(modelType+" "+vehicleType); } public static void main(String[] args) { bike car =new bike(); car.showDetail(); } }Output:
sports bike Press any key to continue . . .
Another example of Inheritance
As displayed in the above figure, Developer is the subclass and Employee is the superclass. Relationship between two classes is Developer IS-A Employee.It means that Developer is a type of Employee.
Program:class Employee{ float salary=40000; } class Developer extends Employee{ int bonus=5000; public static void main(String args[]){ Developer p=new Developer(); System.out.println("Developer salary is:"+p.salary); System.out.println("Bonus of Developer is:"+p.bonus); } }Output:
In the above example, Developer object can access the field of own class as well as of Employee class i.e. code reusability.
Developer salary is:40000.0 Bonus of Developer is:5000 Press any key to continue . . .
Important example of Inheritance
Following is an example demonstrating Java inheritance. In this example,
you can observe two classes namely Calculation
and calculate
.
Using extends keyword, the calculate
inherits the methods addition()
and
Subtraction()
of Calculation class.
class Calculation { int z; public void addition(int x, int y) { z = x + y; System.out.println("The sum of the given numbers:"+z); } public void Subtraction(int x, int y) { z = x - y; System.out.println("The difference between the given numbers:"+z); } } public class calculate extends Calculation { public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given numbers:"+z); } public static void main(String args[]) { int x = 10, y = 20; calculate obj = new calculate(); obj.addition(x, y); obj.Subtraction(x, y); obj.multiplication(x, y); } }Output:
In the above example, Developer object can access the field of own class as well as of Employee class i.e. code reusability.
The sum of the given numbers:30 The difference between the given numbers:-10 The product of the given numbers:200 Press any key to continue . . .
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
NOTE :Multiple inheritance is not supported in java
Single Inheritance Example
Save File As: MainClass.java Program:class Animal{ void eat(){ System.out.println("eating grass..."); } } class Cow extends Animal{ void age(){ System.out.println("age 21"); } } class MainClass{ public static void main(String args[]){ Cow d=new Cow(); d.age(); d.eat(); } }Program:
age 21 eating grass... Press any key to continue . . .
Multilevel Inheritance Example
Save File As: MainClass.java Program:class Animal{ void eat(){ System.out.println("eating...Animal class...eat method"); } } class Dog extends Animal{ void bark(){ System.out.println("barking...Dog class...bark method"); } } class BabyDog extends Dog{ void weep(){ System.out.println("weeping...BabyDog class...weep method"); } } class MainClass{ public static void main(String args[]){ BabyDog obj=new BabyDog(); obj.weep(); obj.bark(); obj.eat(); } }Program:
weeping...BabyDog class...weep method barking...Dog class...bark method eating...Animal class...eat method Press any key to continue . . .
Hierarchical Inheritance Example
Save File As: MainClass.java Program:class Animal{ void animalMethod(){ System.out.println("iam inside Animal class..."); } } class Dog extends Animal{ void sms(){ System.out.println("Hi You are Dog..."); } } class Cat extends Animal{ void message(){ System.out.println("Hi You are cat..."); } } class MainClass{ public static void main(String args[]){ Cat c=new Cat(); c.message(); c.animalMethod(); //c.sms(); error } }Program:
Hi You are cat... iam inside Animal class... Press any key to continue . . .
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.
Why multiple inheritance is not supported in Java
- To remove ambiguity.
- To provide more maintainable and clean design.
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 same method and you call it from child class object, there will be ambiguity to call method of A or B class.
Save File As: C.java Program:class A{ void message(){ System.out.println("Inside message"); } } class B{ void message(){ System.out.println("Inside message ... B Class"); } } class C extends A,B{//suppose if it were Public Static void main(String args[]){ C obj=new C(); obj.msg();//Now which message() method would be invoked? } }Output:
compile time error