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

Why extends and then implements?

Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

Kindly tell me while we're working with classes and interfaces and we see like this;

class class1 extends class2 implements interface1 { }. //it is valid

I want to know that why we cannot implement an interface before extending a class like this;

class class1 implements interface1 extends class2 { }. //it is invalid

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Start by going through the formal grammar and see whether there is an order to implements clause and extends clause.
Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

there is only syntactical description on topics. I am looking for the conceptual theory behind this.
Thank you for replying
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Well, one answer would be, why allow both? Standardising on just one is clearer.

And if you're going to standardise on one order, the fact that you can only extend one class but you can implement many interfaces suggests to me that the class inheritance is somehow a "stronger" relationship, and so it makes sense to put that first. When you're trying to understand a class, knowing what its immediate superclass is is probably the most important thing to know about it.
Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

thank you Brown.
thank you Ritchie.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
You're welcome
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

The "extends" clause specifies class inheritance. Java does not support multiple inheritance, so you can extend one and only one base class. If you don't explicitly define an "extends", then the compiler will act as though you'd stated "extends java.lang.Object".

However, multiple inheritance does have its advantages, so Java takes a page from Microsoft's OLE and presents the "implements" clause to reference interfaces. Unlike a normal class, which defines actual data and method body, an interface is simply a contract that states that the class that implements it will define the methods whose prototypes are declared in the interface definition. So a method which accepts an object implementing interface xyz can safely expect to invoke any of the methods in interface xyz, no matter what the actual class of that object is. That includes classes that implement a subclass (extension) of that interface.


Customer surveys are for companies who didn't pay proper attention to begin with.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40


I think that the simple answer is "that is how the grammar is defined in the Java Language Specification". Any more than that, unless we have one of the Java designers posting here, would only be speculation.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Having written more than one compiler myself (including a Java compiler), I can state that there's no technical reason why the ordering of those 2 clauses should matter.

In psychological terms, however, there can be many interfaces implemented (0-n), but like I said earlier exactly one and only one base class, which will assumed to be java.lang.Object when not explicitly given.

So it makes sense to order them that way so that people are presented with a consistent organization of the class header. This isn't the only clause whose ordering is ordained. Consider the "throws" clause.

In performance terms, it's a teeny bit faster to check for the presence or absence of a single clause-defining keyword at a time instead of running them through a "switch statement" to permit arbitrary ordering. But I doubt that was a major consideration.
Nilay Mitash
Greenhorn

Joined: Mar 30, 2014
Posts: 19

When we write:

class Class1 extends Class2 implements Interface1{}

it means that Class1 is inheriting properties of Class2 and Class 2 is implementing Interface1(which. we know, is possible)

but when we write:

class Class1 implements Interface1 extends Class2{}

it means Class1 is implementing Interface1 and Interface1 is extending Class2 (which is illegal)

An Interface can never extend any class. It can only extend other interfaces.

If an interface could extend other classes, it would not make any sense because interfaces cannot have method bodies and also objects of interfaces cannot be created. But when an interface extends another interface, the class which implements the first interface, will have to have all the methods of both the interfaces overridden.

for example:

class Class1 implements Interface1 extends Interface2{}

would be a legal declaration and Class1 will have to override all the abstract methods of both the interfaces.

But on the other hand, an Interface cannot implement another Interface because an interface cannot have method bodies.

for example:

interface Interface1 implements Interface2{}
or
class Class1 implements Interface1 implements Interface2{}

would be wrong/illegal declaration.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Nilay Mitash wrote:When we write:

class Class1 extends Class2 implements Interface1{}

it means that Class1 is inheriting properties of Class2 and Class 2 is implementing Interface1(which. we know, is possible)

Is that a typo ? It's not possible to tell from that line alone whether Class2 implements Interface1 or not. The only thing you can say for certain is that Class1 implements Interface1. This could be directly or via inheritance from Class2.


Joanne
Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

Thank you Nilay for your post towards this topic. This is really useful
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Shivi Kashyap wrote:Thank you Nilay for your post towards this topic. This is really useful

However, you should also read Joanne's post, because Nilay's, as posted, is incorrect.

In the declaration:
class Class1 extends Class2 implements Interface1 { ...
BOTH the 'extends' AND the 'implements' refer to properties of Class1; nothing else.

Specifically, it does NOT mean that "Class 2 is implementing Interface1" - in fact, more likely, the exact opposite.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Vivek Thakur
Greenhorn

Joined: May 08, 2014
Posts: 3
This is impossible because an Interface can never extend any class. It can only extend other interfaces.
Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

Hi Winston,

You're too not wrong in fact you've given me the exact description about this thread. But simply logic is that ; an interface can never extend a class. The java Grammer specification by oracle also described this issue in certain syntactical way. However, thank you for your post. You know it really matters
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to the Ranch Vivek Thakur
Vivek Thakur
Greenhorn

Joined: May 08, 2014
Posts: 3
Thank you Ritchie
Shivi Kashyap
Greenhorn

Joined: Mar 28, 2014
Posts: 16

I forgot to welcome you Vivek. Welcome to coderanch society.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Shivi Kashyap wrote: . . . The java Grammer specification . . .
I presume you mean the Java® Language Specification. Which section?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why extends and then implements?