Does Java support multiple Inheritance?
Answers were Sorted based on User's Feedback
Answer / debapriya patra
Java does not support multiple inheritance.Why?
If anybody ask u why java does not support multiple
inveritance then answer is:"Whenever we create an instance
of a child class and we extend more then one class like
C++, then if these two classes contain same variable then
which will print if we try to print the variable."
Example:
class a
{
int i = 10;
}
class b
{
int i = 10;
}
class c extends a,b
{
public static void main(String[] args)
{
c c1 = new c();
System.out.printn(c1.i);//Here an ambiguous situation
will occur
}
}
Is This Answer Correct ? | 29 Yes | 14 No |
Answer / ujas doshi
No...Actually java does not support multiple inheritance.
You can say bcoz of reason mentioned by debapriya.
But if you want a class to be inherited from 2 classes you
can use a concept in java called interfaces..
class C extends A, extends B ---wrong, error, only one
class can be inherited
class C implements A, implements B ---fine, here A and B
are not classes but they are interfaces.
Now what are interfaces?
interfaces are similar to classes except a few things
1)Interfaces must contain static methods only
Is This Answer Correct ? | 17 Yes | 10 No |
Answer / tarun
I want to make sure that The answer provided by Tewodros
Tesema is an example of multilevel inheritence not multiple
inheritence which is supporte by java and it is true.
But Java does not supports multiple inheritence. In the case
of Interface we can inherit multiple interfaces by
sepertaing them with comm(,). But a class can inherit a
single class at a time.
Is This Answer Correct ? | 6 Yes | 0 No |
Answer / ujas
for any more doubts mail me to ujas.doshi@atosorigin.com
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / gyana
No actually java doesn't support multiple inheritance.But
we can use this by using Interface in java.
for example Class x,implements y, implements z.
here x is the main class and the two interface y and z
implements it in its program.y and z are two interfaces not
classes.
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / janardhan
In java single inheritance is possible, not multiple
inferitance.
in java one class extends only one class not more than one.
But in java one class implements multiple interfaces. some
time we will call multiple inheritance possible by
interfaces.
jana
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / ramakrishna
JAVA omits many rarely used, poorly understood,
confusing features of C++ that in our experience bring more
grief than benefit. This primarily consists of operator
overloading (although it does have method overloading),
multiple inheritance, and extensive automatic coercions.
Who better than Dr. James Gosling is qualified to make a
comment on this. This paragraph gives us an overview and he
touches this topic of not supporting multiple-inheritance.
Java does not support multiple inheritance
First lets nail this point. This itself is a point of
discussion, whether java supports multiple inheritance or
not. Some say, it supports using interface. No. There is no
support for multiple inheritance in java. If you do not
believe my words, read the above paragraph again and those
are words of the father of Java.
This story of supporting multiple inheritance using
interface is what we developers cooked up. Interface gives
flexibility than concrete classes and we have option to
implement multiple interface using single class. This is by
agreement we are adhering to two blueprints to create a class.
This is trying to get closer to multiple inheritance. What
we do is implement multiple interface, here we are not
extending (inheriting) anything. The implementing class is
the one that is going to add the properties and behavior. It
is not getting the implementation free from the parent
classes. I would simply say, there is no support for
multiple inheritance in java.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sadikhasan,meta
Java does not support multiple inheritance because it
creates a dimond problem.
if we want to implement multiple inheritance in java then we
use interfaces which support multiple inheritance.
e.g.
interface B
{
}
interface C
{
}
class A implements B,C
{
}
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / divyesh chauhan
yes java do not support multiple inheritance but we can achieve multiple inheritance using concept of interface implementation in class
for e:g
interface my
{
public void show();
}
interface my1 extends my
{
void disp();
}
class child implements my1
{
public void show()
{
System.out.println("hello java");
}
public void disp()
{
System.out.println("hello disp");
}
public static void main(String...a)
{
my m1=new child();
m1.disp();
m1.show();
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / ershad
Bcoz of DIAMOND PROBLEM (this term we hav 2
used )..diamond problem is one which does not supports
Multiple inheritance
Is This Answer Correct ? | 3 Yes | 5 No |
What is difference between module and function?
What do you mean by chromounits in java8?
What does null mean in java?
Why we do exception handling in java and how many types of exceptions are there?
Can we define private and protected modifiers for the members in interfaces?
What is the format of Inner Class after it compiled?
Can each java object keep track of all the threads that want to exclusively access it?
When do you create an index?
What is a ternary operator in java?
What is a default constraint?
When should you make a function static?
What is nan inf?