File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending interface

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear David,

I think it should be



Thanks for the reply,

Regards,
Vijay
 
Mark Newton
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - my mistake - edited it above

Thanks
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic