What is Inheritance?
Inheritance is the process by which one class acquires the properties(instance variables) and methods of another class. The class whose properties are inherited is known as superclass or parent class and the class which inherits the properties of other is known as subclass or derived class.
extends Keyword
extends is the keyword used to inherit the properties of a class.
Types of inheritance
Java provides following types of inheritance.
1) Single Inheritance
Single inheritance In a class hierarchy when a child has one and only one parent and parent has one only child, that inheritance is said to be single inheritance.
Here Student is a parent class of Marks and Marks would be a child class of Student. Using extends Keyword, the Marks class inherits the method putRoll() of Student class.
Example of Single inheritance
class Student { int rollno; void getNo(int no) { rollno=no; } void putNo() { System.out.println("Rollno= "+rollno); } } class Marks extends Student { float marks; void getMarks(float m) { marks=m; } void putMarks() { System.out.println("Marks= "+marks); } } class Display { public static void main(String ar[]) { Marks ob=new Marks(); ob.getNo(44); ob.putNo(); ob.getMarks(66); ob.putMarks(); } }
Output
Rollno= 44
Marks= 66.0
2) Multi-level Inheritance
In a class hierarchy, when a class is derived from already derived class then that inheritance is said to be multi-level inheritance.
As you can see in below Sports is subclass or child class of Marks class and Marks is a child class of Student.
Example of Multi-level Inheritance
class Student { int rollno; void getNo(int no) { rollno=no; } void putNo() { System.out.println("Rollno= "+rollno); } } class Marks extends Student { float marks; void getMarks(float m) { marks=m; } void putMarks() { System.out.println("Marks= "+marks); } } class Sports extends Marks { float score; void getScore(float scr) { score=scr; } void putScore() { System.out.println("Score= "+score); } } class Display { public static void main(String ar[]) { Sports ob=new Sports(); ob.getNo(44); ob.putNo(); ob.getMarks(66); ob.putMarks(); ob.getScore(85); ob.putScore(); } }
Output
Rollno= 44
Marks= 66.0
Score= 85.0
3) Multiple Inheritance
then that inheritance is said to be multiple inheritance. Multiple Inheritance is very rarely used in software projects because problems in the hierarchy. Multiple Inheritance is supported in C++.
4) Hierarchical inheritance:
In a class hierarchy, when a parent class has two or more than two child classes then, the inheritance is said to be hierarchical inheritance.
Example
import java.util.Scanner; class Student { int rollno; void getNo(int no) { rollno=no; } void putNo() { System.out.println("Rollno= "+rollno); } } class Marks extends Student { float marks; void getMarks(float m) { marks=m; } void putMarks() { System.out.println("Marks= "+marks); } } class Sports extends Student { float score; void getScore(float scr) { score=scr; } void putScore() { System.out.println("Score= "+score); } } class Display { public static void main(String ar[]) { Marks s1=new Marks(); Sports s2=new Sports(); Scanner obj=new Scanner(System.in); System.out.println("Enter Roll and Marks"); int r=obj.nextInt(); int m=obj.nextInt(); s1.getNo(r); s1.putNo(); s1.getMarks(m); s1.putMarks(); System.out.println("Enter Roll and Score"); r=obj.nextInt(); float s=obj.nextFloat(); s2.getNo(r); s2.putNo(); s2.getScore(s); s2.putScore(); } }
Output
Enter Roll and Marks
101
560
Rollno= 101
Marks= 560
Enter Roll and Score
102
425
Rollno= 102
Score= 425
0 Comments