Inheritance | OOps |
Inheritance Overview:
- Inheritance is a fundamental concept in object-oriented programming (OOP).
Inheritance in java is a mechanism by which one class is allowed to inherit the features(fields and methods) of another class.
- It allows a class to inherit the properties and methods of another class, known as the superclass or base class.
- The class that inherits these properties and methods is called the subclass or derived class.
- Inheritance promotes code reuse, enhances modularity, and enables polymorphism.
Real time Ex : Parents & Children
To establish inheritance in Java, you use the extends keyword.
The subclass declaration follows the superclass declaration, and the extends keyword is used to specify the superclass.
Example: class SubClass extends SuperClass { }
- How to implement inheritance in Java ?
-Java provides extends keyword to inherit the features of a class.
- Important terminologies in Java inheritance
- Subclass/Child class : The class that inherits the features of other class is known as subclass, also known as child class, derived class or extended class.
- Superclass/Parent class : The class whose features are being inherited is known as super class, also known as parent class or base class.
- re-usability : mechanism that facilitates to reuse fields and methods of the existing class into the new class is known as reusability.
When you create a new class, you can use the same fields and methods already defined in the previous class. This is called reusability.
- Is-A relationship in java
Is-A relationship in java represents Inheritance. It is implemented in Java through keywords extends (for class inheritance) and implements (for interface implementation).
For example:
class A { // statements } The Java compiler will treat the above code like this: class A extends Object{ // statements. }
types of inheritance
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritance(Supported using interfaces, not by classes)
- Hybrid inheritance(Mix of two or more of above inheritance types)
Single level Inheritance in Java
class
A {void
add(int
a,int
b) { System.out.println(a +"+"
+b+" = "
+(a+b)); } }class
Bextends
A {void
subtract(int
a,int
b) { System.out.println(a +"-"
+b+" = "
+(a-b)); }public static void
main(String args []) { B obj =new
B(); obj.add(5,10); obj.subtract(20,10); } }
Multilevel Inheritance in Java
class
A {void
add(int
a,int
b) { System.out.println(a +"+"
+b+" = "
+(a+b)); } }class
Bextends
A {void
subtract(int
a,int
b) { System.out.println(a +"-"
+b+" = "
+(a-b)); } }class
Cextends
B {void
multiply(int
a,int
b) { System.out.println(a +"*"
+b+" = "
+(a*b)); }public static void
main(String args []) { C obj =new
C(); obj.add(5,10); obj.subtract(20,10); obj.multiply(10,20); } }
Hierarchical Inheritance in Java
When two or more than two classes inherits a single class, we call it hierarchical inheritance. In java a class can be inherited by any number of classes, so in hierarchical inheritance, there can be any number of child classes, it's not limited to two classes only. The program below shows an example of hierarchical inheritance.class
A {void
add(int
a,int
b) { System.out.println(a +"+"
+b+" = "
+(a+b)); } }class
Bextends
A {void
multiply(int
a,int
b) { System.out.println(a +"*"
+b+" = "
+(a*b)); } }class
Cextends
A {void
divide(int
a,int
b) { System.out.println(a +"/"
+b+" = "
+(a/b)); } }class
Test {public static void
main(String args []) { B obj =new
B(); obj.add(5,10); obj.multiply(20,10); C obj2 =new
C(); obj2.add(20,30); obj2.divide(20,10); } }
Multiple Inheritance in Java Using support to interface
Multiple inheritance in java is supported through interfaces only, which means a class can inherit the features(specially default and static methods from java 8 onwards) from more than one interfaces. Java allows a class to implement any number of interfaces. The program below shows an example of multiple inheritance.
interface
A {public void
print();default void
add(int
a,int
b) { System.out.println(a +"+"
+b+" = "
+(a+b)); } }interface
B {public void
print();default void
subtract(int
a,int
b) { System.out.println(a +"-"
+b+" = "
+(a-b)); } }class
Cimplements
A, B {public void
print() { System.out.println("Testing multiple inheritance in Java"
); }public static void
main(String args []) { C obj =new
C(); obj.print(); obj.add(20,10); obj.subtract(20,10); } }
Why Java does not support multiple inheritance through classes ?
Hybrid Inheritance in Java
hybrid inheritance is a mix of hierarchical and multiple inheritance but it's not limited to that only, it may contain the mix of any other inheritance types as well.
Comments
Post a Comment