Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface inheritance

 
Simon Edwards
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, does method row() in interface Derived overrides method row() in interface Base, or hides it?



Thanks.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried to implement both of these in the same class?
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Edwards wrote:In the following code, does method row() in interface Derived overrides method row() in interface Base, or hides it?



Thanks.


I believe it is override.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon Edwards,

here ther row() method in Base interface will be overriden by the Derived interface row().

Regards,
James.
 
Chintu Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, It is an override in Interface Derived.
When you try to implement both of these interfaces in same class, you cannot implement row() method with "throws Throwable". It should always be an Exception or a subclass of Exception.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overrides, as the complier tells us the following -

public void row() throws Throwable {}
^
overridden method does not throw Throwable
1 error


when running -



Regards,
Dan
 
Himai Minh
Ranch Hand
Posts: 1232
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final3 is a child of Derived. A rule says that an overriding method cannot throw exceptions that is broader than its overridden method.
Throwable in Final3 is broader than Exception.
That is why it won't compile.

Look at this example:


Look at this bad example. This example illustrates why a child class cannot throw a broader exception.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic