Inheritance is a way to implement an IS-A relationship i.e. parent-child relationship. A subclass inherits the superclass properties like data member, methods. Inheritance is the way of re-usability of code. Let us take the example of parent and child. A child inherits the properties of its parent.
Why inheritance is used?
- Code re-usability.
- Run-time polymorphism.
Syntax:
class subclass extends superclass{ Â Â //subclass code } |
Types of inheritance:
1. Single inheritance:
When a derived class inherits the properties and behavior from a single parent class. It is known as single inheritance.
Example:
/** * This program is used for single inheritance example. * @author W3spoint */ class Student { String name = "Pooja"; } public class CollegeStudent extends Student { String className = "MCA"; /** * This method is used to show details of a student. * @author W3spoint */ public void showDetail(){ System.out.println("Student name = " + name); System.out.println("Student class name = " + className); } public static void main(String args[]){ //creating subclass object CollegeStudent obj = new CollegeStudent(); //method call obj.showDetail(); } } |
Output:
Student name = Pooja Student class name = MCA |
2. Multilevel inheritance:
When a derived class inherits the properties and behavior from a derived class. It is known as multilevel inheritance.
Example:
/** * This program is used for multilevel inheritance example. * @author W3spoint */ class Student { String name = "jai"; } class CollegeStudent extends Student { String className = "MCA"; } class McaStudent extends CollegeStudent{ String semester = "3rd sem."; /** * This method is used to show details of a student. * @author W3spoint */ public void showDetail(){ System.out.println("Student name = " + name); System.out.println("Student class name = " + className); System.out.println("Student semester = " + semester); } } public class StudentTest { public static void main(String args[]){ //creating subclass object McaStudent obj = new McaStudent(); //method call obj.showDetail(); } } |
Output:
Student name = jai Student class name = MCA Student semester = 3rd sem. |
3. Hierarchical inheritance:
When two or more derived class inherits the properties and behavior from same parent class. It is known as hierarchical inheritance.
Example:
/** * This program is used for Hierarchical inheritance example. * @author W3spoint */ class Numbers { int a = 10; int b = 20; } class AddNumbers extends Numbers { /** * This method is used to add. * @author W3spoint */ public void showAdd(){ System.out.println(a + b); } } class MultiplyNumbers extends Numbers { /** * This method is used to multiply. * @author W3spoint */ public void showMultiplication(){ System.out.println(a * b); } } public class Test { public static void main(String args[]){ //creating base classes objects AddNumbers obj1 = new AddNumbers(); MultiplyNumbers obj2 = new MultiplyNumbers(); //method calls obj1.showAdd(); obj2.showMultiplication(); } } |
Output:
30 200 |
Why multiple inheritance is not supported in java?
Multiple inheritance is not supported by Java because of ambiguity problem. Let us consider the below example. We have two classes Test1 and Test2 which have same method show(). If multiple inheritance is possible than Test class can inherit properties and behaviour of both Test1 and Test2 classes. Now Test class have two show() methods inherited from Test1 and Test2. Problem occurs now in method call, when show() method is called with Test class object which method will be called, of Test1 class or Test2 class. That is why multiple inheritance is not supported in java.
Example:
/** * This is used to show that multiple inheritance * is not supported in java in case of classes. * @author W3spoint */ class Test1{ public void show(){ System.out.println("show details."); } } class Test2{ public void show(){ System.out.println("show details."); } } //let multiple inheritance is possible. public class Test extends Test1, Test2 { public static void main(String args[]){ Test obj = new Test(); //Ambiguity problem in method call //which class show() method will be called. obj.show(); } } |
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at com.w3spoint.business.Test.main(Test.java:19) |
Interview Questions on Inheritance
- Why multiple inheritance is not supported in java?
- How to implement multiple inheritance in java?
- Are interfaces also inherited from Object class?
- Why an interface cannot have constructor in java?
- How do you restrict a member of a class from inheriting to it’s sub classes?
- Can a class extend itself in java?
- Are constructors inherited in java?
- What happens if both superclass and subclass have a field with same name?
- Are static members inherited to subclasses in java?
Next Topic: Aggregation in java with example.
Previous Topic: Association in java with example.