Inheritance
Inheritance
Brajesh Raj
Index
Inheritance
Types of Inheritance
Why multiple inheritance is not possible in
Java in case of class?
Introduction
Inheritance in Java is a mechanism in which one
object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object
Oriented programming system).
TestInheritance3.java
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 the same method and you call
it from child class object, there will be ambiguity
to call the 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.
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