• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why extends and then implements?

 
Shivi Kashyap
Greenhorn
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by going through the formal grammar and see whether there is an order to implements clause and extends clause.
 
Shivi Kashyap
Greenhorn
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is only syntactical description on topics. I am looking for the conceptual theory behind this.
Thank you for replying
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Brown.
thank you Ritchie.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Henry Wong
author
Marshal
Pie
Posts: 20822
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
Hibernate Java Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shivi Kashyap
Greenhorn
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Nilay for your post towards this topic. This is really useful
 
Winston Gutkowski
Bartender
Pie
Posts: 9443
49
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vivek Thakur
Greenhorn
Posts: 3
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is impossible because an Interface can never extend any class. It can only extend other interfaces.
 
Shivi Kashyap
Greenhorn
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Vivek Thakur
 
Vivek Thakur
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ritchie
 
Shivi Kashyap
Greenhorn
Posts: 16
Java Redhat Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to welcome you Vivek. Welcome to coderanch society.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic