Multiple Inheritance in Java | Java Tutorials
Автор: SG Tutorial
Загружено: 2023-03-15
Просмотров: 1174
Описание:
Multiple Inheritance in Java | Java Tutorials for beginners
About Video:
This video will show What is Multiple Inheritance with real life example as well as practical example?Why Java doesn't support Multiple Inheritance? And How we can achieve Multiple Inheritance in Java?
Links:
To learn about basics of Java click here 👇
Java Basics: • Java Tutorials for Beginners | Java course...
Java Inheritance and Types:
• Java Inheritance | Java for Beginners
Notes:
1)What is multiple inheritance?
2)Why Java doesn't support multiple inheritance?
3)How we can achieve multiple inheritance in java?
To learn about Multiple Inheritance you should know basic knowledge about Inheritance
1)What is multiple inheritance?
To receive or inherits properties and behaviour from one class to another class is nothing but the inheritance.
For example,
class A extends class B and class C so here class A is child class and class B and class C are child classes so if we notice here,single child class extend more than one parent class and it's nothing but the Multiple Inheritance.
2)Why Java doesn't support multiple inheritance?
Let's take one example to understand why Java doesn't support multiple inheritance?
class B{
void m1(){
System.out.println("class B m1 method");
}
}
class C{
void m1(){
System.out.println("class C m1 method");
}
}
class A extends B,C{
void m2(){
System.out.println("class A m2 method");
}
}
class Test{
public static void main (String [] args){
A a = new A();
a.m1();
a.m2();
}
}
If we execute above code then compiler will raise an error because in above example class A extends class B and C and both the classes have same m1() method of same signature and hence compiler get confused which method has to be call and it's create ambiguity and this problem is also called as diamond problem.Basically due to diamond problem Java doesn't support multiple Inheritance.
3)How we can achieve multiple inheritance in java?
We can overcome diamond problem by using Interface..
For example,
interface B {
void m1();
}
interface C {
void m1();
}
class A implements B,C {
public void m1(){
System.out.println(" m1 method");
}
void m2(){
System.out.println(" m2 method");
}
}
class Test{
public static void main (String [] args){
A a = new A();
a.m1();
a.m2();
}
}
Output: m1 method
m2 method
So if you execute above code then it's compiles fine without any error even though in above example class A implements more than one interface so it's proves that we can achieve multiple inheritance using interface.
Thank You!
#multipleinheritanceinjava #multipleinheritanceusinginterfaceinjava #multipleinheritanceinjavaeclipse #sgtutorialjava #java #whyjavadoesntsupportmultipleinheritance #achievemultipleinbeeitanceinjavausinginterface
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: