#50 Multiple Inheritance in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)what is multiple inheritance?
2)Why Java does not support Multiple Inheritance?
3)What is ambiguity and not allowed in java?

#1
Multiple inheritance
If a particular class inheriting multiple class then this type of inheritance is called multiple inheritance.
like c++ language there are multiple inheritance like that :
class A
{
... .. ...
};
class B
{
... .. ...
};
class C: public A,public B
{
... ... ...
};

But Java not allowed we have alternate option for that is implementing multiple interface not we will discussing in upcoming lecture.
#2
why java not support multiple inheritance?
=Because of the Ambiguity problem, Java does not support multiple inheritances directly.

#3
Why it is ambiguous?
suppose we have some class A, B and c
class A
{
... .. ...
show(){

}
};
class B
{
... .. ...
show(){

}
};
class C extends A,B //assume for some instance java support multiple inheritence
{
... ... ...
show();
//here we get ambiguity since if we allowed multiple inheritance and same two property or method belong to class A and Class B
//then how C class use show() method there is ambiguity of choice...
//that’s why java exclude the concept of multiple inheritance

};

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

please keep making such courses... so glad that i discovered a creator like you...

abhineshwarjaiswar
Автор

You should have put these on the very beginning. The video of packages also. This helps while making and arranging files while making notes according to the topic. In java I find very difficult how to arrange and make notes in Java for different topics. Do each class we have to make different java file then why multiple class concept is used.

sohampatel-bmgt
Автор

I think multiple inheritance is supported by interfaces in and why??

jagrutiacharya
Автор

You should have talked about how C++ solved this ambiguity problem,
For anyone who is curious, C++ and other languages which support multiple parents in inheritence, uses function overriding to call the derived class method and then inside there you can use the method of A, B, or both, and include your derived class logic too inside it. This will look like,
class AB extends A, B{
func(){
A::func();
B::func();
// your logic here
}
}

ooogabooga
Автор

public class Main{
public static void main(String[] args) {
SciCalc obj = new SciCalc();
System.out.println(obj.add(8, 3));
System.out.println(obj.mult(8, 3));
System.out.println(obj.sub(4, 9));
System.out.println(obj.div(4, 9));
System.out.println(obj.powered(7, 2));

C letter = new C();
letter.toString();
new B();

}
}

class A{
public A(){
System.out.println("Hello from A");
}
}

class B extends A{
public B(){
super();
System.out.println("Hello from B");
}
}

class C extends B{
public C(){
super();
System.out.println("Hello from C");
}
}

class Calc{

public int add(int n1, int n2){
return n1 + n2;
}

public int sub(int n1, int n2){
return n1 - n2;
}

}

class Advanced extends Calc{

public int mult(int n1, int n2){
return n1 * n2;
}

public int div(int n1, int n2){
return n1 / n2;
}
}

class SciCalc extends Advanced{

public double powered(int n1, int n2){
return Math.pow(n1, n2);
}
}

kvelez
Автор

What about using default method of interfaces from java8 and calling them using interface.super.method..

saicharanguggilam
Автор

Is this also known as Diamond Problem?

vishnusatheeshbabu
Автор

Hii, I have a doubt plz someone reply and resolve

If there is a same method in both the parent classes then it will create ambiguity. Even if there are no any same methods in both the parent classes then also it is giving error?

WHAT IS THE REASON BEHIND IT?

ganeshpatil
Автор

My Question: Can method overloading solve this ambiguity problem?
Example

class Mobile
{
brand;
price;
network;

public void call() {
print("Calling...")
}

}

class SmartPhone
{

schoolContacts; // ← a database of my networks at school
churchContacts;


public void call(String schoolContacts) { // ← method overloading
print("Calling..." + name of the person I am calling)
}

public void call(String churchContacts) {
print("Calling..." + name of the person I am calling)
}

}

// multiple inheritance

public class Apple extends to SmartPhone, Mobile // assuming this syntax works
{
public static void main(String a[]) {

// assuming I create the objects using appropriate classes

person1.call()



}
}

davidking
Автор

gadde multiple inheritances nahi hota hai java me

futuristictechnology..