• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending interface

 
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear David,

I think it should be



Thanks for the reply,

Regards,
Vijay
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry - my mistake - edited it above

Thanks
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic