• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple inheritance  RSS feed

 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public XXXX extends something1, something2

1. XXX should be an interface,something1 and something2 need not, for the expression to be legal
2. XXX should be a class, something1 and something2 must be interfaces for the expression to be legal.
3. XXX, something1 and something2 must be interfaces for the expression to be legal.
4. The above statement is alway illegal in Java as multiple inheritance is not supported.

it says answer is 3, but i think answer should be 4. because multiple inheritance is not allowed.
vivek
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although multiple inhertince is not allowed you can implement as many interfaces as you like
 
Ram Nath
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 3.

Multiple inheritance is if you are inheriting from more than one class and not for interfaces. You can extend n number of interfaces.
public xxx extends interface1, interface2
is correct.
Ram
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't seem to agree .... am I missing something here ...

When I compile InterTest.java:
InterTest.java:1: '{' expected.
public interface InterTest extends Inter1 Inter2
^
1 error
I (or my JVM on Win 98) don't get it ?
I would have answered 4.
Regds.
ps: the ^ is shown between Inter1 and Inter2 below the Error line. I can't put the spaces correct here ....
- satya

[This message has been edited by satya5 (edited June 06, 2000).]
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya,
I dont see a comma(,) seperating Inter1 and Inter2 in your code.May be that is why you are getting the error.Otherwise an interface, as it has turned out to be, can extend as many other interfaces as it can. The following compiled quite oky for me:
----------------------------------------------------------------
import java.io.*;
public interface MyInterface extends Runnable,Serializable{}
-----------------------------------------------------------------

Herbert.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aha .... .......
So, now my answer to the qstn will be 3.
In the above code, this line should be used:
public interface InterTest extends Inter1, Inter2
Thanks.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class can extend only 1 class though it can implement any no of interfaces.
Original ques: class A extends s1,s2
should throw compilation error(assuming s1,s2 are classes)
Had it been : class A implements something1,something2
then its fine(assuming s1,s2 are interfaces)
Am i right??
Ganesh
 
Suma Narayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nganesh,

The original question is
public XXXX extends something1, something2
Please note, it says XXXX and not class XXXX. Had it been class XXXX, then what you are saying would have been right. It is legal for interface to extend as many interfaces as needed. So the given answer is right.
Suma.
 
Hari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.,
Is It correct An interface can inhert multiple interface.
Please reply any.
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post which mock exam that you got the question from. Thanks!
Paul
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify:
If you think about what is going on it will make sense why multiple inheritance of interfaces is legal. First, why isn't multiple inheritance of classes supported? Because of the problem of dealing with how to handle multiple inheritance when classes have the same method.
Example:

So which doStuff() method gets executed? Mother or Father?
Now lets rewrite them with interfaces:

In this case Child must define doStuff(). Mother and Father don't have code to support doStuff() so there is no issue with Mother and Father sharing the same method definition since they don't implement the method.
So the only thing left to realize is that an interface does not implement other interfaces, it extends them. So as long as XXX, something1 and something2 are all interfaces there is no problem with multiple inheritance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!