OOPS in JAVA 11
Inheritance
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
The class whose properties are inherited is known as superclass (base class, parent class) and the class which inherits the properties of other is known as subclass (derived class, child class).
Inheritance represents the IS-A relationship, also known as parent-child relationship.
extends Keyword
extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.
class Super
{
String name;
}
class Sub extends Super
{
//methods and fields
String name;
void show()
{
super.name = "Super Class"; //name of Super class
name = "Sub Class"; //name of Sub class
}
}
super Keyword
In Java, super keyword is used by a subclass whenever it need to refer to its immediate super class.
1. It is used to differentiate the members of superclass from the members of subclass, if they have same names.
2. It is used to invoke the superclass constructor from subclass.
super.variable
super.method();
Purpose of Inheritance
1. To promote Code Reusability.
2. To use Polymorphism.
Types of Inheritance
1. Single Inheritance
When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
class A {
}
class B extends A {
}
2. Multilevel Inheritance
When a class extends only one class then we call it a single inheritance. The below examples shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.
class A {
}
class B extends A {
}
class C extends B {
}
3. Heirarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.
class A {
}
class B extends A {
}
class C extends A {
}
class D extends A {
}
NOTE : Multiple inheritance is not supported in java.
Q. Why multiple inheritance is not supported in java???
To reduce the complexity and simplify the language, 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.
Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.
Example:
class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B //suppose if it were
{
Public Static void main(String args[])
{
C obj=new C();
obj.msg();
//Now which msg() method would be invoked?
}
}
Output:
Compile Time Error
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )