Why java Don't Support Multiple interitence
Answers were Sorted based on User's Feedback
Answer / g ch vishnu vardhan reddy
Multiple Inheritance is not supported by java in order to
avoid ambiguity between variables of different classes and
to rudece memory overloading.
Is This Answer Correct ? | 42 Yes | 1 No |
There is a big reason behind that(java does not support
multiple inheritance). Please go through the following
example.
1. Assume that java is supporting multiple inheritance
class A {
void m1() {
// implement method
}
}
class B {
void m1() {
// implement method
}
}
//As for the assumption (1) the following code will compile
class C extends A,B {
public static void main( String s[]) {
C c = new C();
c.m1();
}
}
Note : In main method i am calling c.m1() method In this
situation which super class m1 method has to call (from A
or B) JVM will confuse.
So our assumtion(1) is wrong .
This is the reason why java does not support multple
inheritance through classes.
Note : This same cocept is applicable for classes.
Is This Answer Correct ? | 29 Yes | 4 No |
Answer / midhula kadiyala
If you extend more than one class JVM does n't understand
for which object it has to create object first.so ambiguios
occurs.That's y java does n't support multiple inheritance.
We can implement multiple inheritance by using interfaces.
Interface A
{
public void X();
}
class B implements A
{
public void X()
{
s.o.p("this is function x");
}
public void Y()
{
s.o.p("this is function y");
}
}
class C extends B
{
public void X(){...}
public void j(){...}
P S V M()
{
A a =new C();
a.X();
}
}
if a class wants to extend 1 or more classes define those
function definitions in one interface and in another class
provide bodies by implementing those interface.
if the class can extend the class which is implementing that
interface then u can access all functions present in that
interface.
here my interface A
i provide bodies in class B by implementing interface A
class C extend that class B
and class c is able to access all the methods present in the
interface by creating the reference of an interface with the
object of class C
(remember 1 thing to the reference of interface we can
assign object of class only if it is implementing that
interface)
here class c extends class B and again class B implements
interface A
so i can write
A a= new C();
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / sanjeev
Why cant we face same ambiguity when we declare variables?
interface A{
int i = 10;
}
interface B{
int i = 20;
}
Class C implement A, B{
what is the value of C.i?
}
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / srinu
Java Does not support Multiple inhertiance through class
because ambiguity between variables of different classes
and to rudece memory overloading.and another reason java
extended only one class at time
java support Multiple inhertiance through interfaces.because
java class implements any number of interface at time
Is This Answer Correct ? | 0 Yes | 2 No |
Answer / kiranksj
ya java don't support multiple interitence but by achieving
it the interface are came
Is This Answer Correct ? | 2 Yes | 6 No |
Answer / sid
The famous "diamond shaped problem " is the reason behind NOT using multiple inheritance in java.
check out this link for full details
http://en.wikipedia.org/wiki/Diamond_problem
Is This Answer Correct ? | 0 Yes | 4 No |
Answer / samir rana
java support multiple inheritance not through the use of
class, but through the implementation of interface we can
achieve multiple inheritance.
By using interface we can implement multiple interfaces for
a class which can solve the problem of our multiple inheritance.
Is This Answer Correct ? | 0 Yes | 4 No |
A person says that he compiled a java class successfully without even having a main method in it? Is it possible?
How two different class threads communicate with each other?. send example code.
How do you invoke a method?
What about interrupt() method of thread class ?
What are the 3 types of control structures?
If there are two notepad opened and working on it . is this a process or thread? computer based Live Example to differentiate between Thread , Process , and Multi tasking ?
what is difference between equals and ==?
Which programming language is best in future?
How does finally block differ from finalize() method?
What is singleton math?
What is the difference between the program and the process?
What is the difference between Synchronizing mehtod & Synchronizing block?