aspose file tools*
The moose likes Beginning Java and the fly likes Extending interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending interface" Watch "Extending interface" New topic
Author

Extending interface

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear friends,

We can't extend more than one class, but why it is allowed to extend more than one interface?

class A extends B,C // compiler error

interface C extends A,B // DOESN'T GIVE COMPILER ERROR
{
public void cMethod();
}

Please provide your explanation

Regards,
Vijay
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by vijaychandran rajagopalan:
Dear friends,

We can't extend more than one class, but why it is allowed to extend more than one interface?

class A extends B,C // compiler error

interface C extends A,B // DOESN'T GIVE COMPILER ERROR
{
public void cMethod();
}

Please provide your explanation

Regards,
Vijay


One thing for sure is that we cannot extend interface.We can only implement interface.

You cannot have multiple inheritance in Java.You can navigate here to know why?

Whereas multiple interface is allowed.
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Balasubramanian Chandrasekaran wrote
One thing for sure is that we cannot extend interface.We can only implement interface.
You cannot have multiple inheritance in Java.You can navigate here to know why?

Whereas multiple interface is allowed.


I beg to defer. We can extend an interface. A class cannot extend an interface, it can only implement it, but an interface can extend another interface. An interface cannot implement another interface, but it surely can extend one or more interfaces
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Thanks for the reply.

Excuse me for not posting clear question.

interface C extends A,B {}
in which, i should have mentioned A and B are interfaces.

But still, is there any specific reason why classes are not allowed to extend more than one but interfaces are allowed to do so?

Kindly provide your help.
Regards,
Vijay
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
I guess it's because a class implementing an interface that extends multiple interfaces is only the same as one class implementing multiple interfaces.

That is:

has the same effect as:


[EDIT: Sorry - I just realised my horrible lower-case interface/class names. I have punished myself suitably]
[ January 16, 2008: Message edited by: David Payne ]
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Dear David,

I think it should be



Thanks for the reply,

Regards,
Vijay
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Sorry - my mistake - edited it above

Thanks
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
It's a pain for the compiler to implement multiple inheritance.
It almost never buys you something that containment couldn't do.
It can lead to a lot of obscurity in code maintenance.

These lessons were learned in C++.
Java syntactically started basically as C++ with the cruft removed.

(There's also the issue of portable byte-code+JVM that distinguish it.)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
Don't get me started about those stupid light bulbs.
 
subject: Extending interface