Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

A class extending a class and implementing an interface with same methods ?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if a class extends another class and implements an interface both with the same method , which function will it implement ??

eg:



which show() does c implement ??

thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing is that the rules for the implementation of interfaces and abstract classes have been obeyed.

So, if you do


or



both will do the same thing...
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Albus':

Aren't you dead? You probably should change your username to your real name. Fake names are against the rules, and the moderators will get on your case.

John.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

albus dumbeldore wrote:which show() does c implement ??


From a pragmatic standpoint, what's the difference? If the signatures are the same, the implementing subclass will be an instance of both the superclass and interface (unless I'm misunderstanding something).
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See java follows bottom to up approach but in this code you are overriding the same method show() in main. So it doesn't matter here which show() will be implemented here as it is defined in main class but according to java basics the show function of class B will be executed here as it is bottom to class A.
 
Sheriff
Posts: 17005
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jd Sethi wrote:according to java basics the show function of class B will be executed here as it is bottom to class A.


Nope.

b.show() is declared as abstract, therefore it has no implementation, therefore there is no b.show() to execute. Same deal with a.show(). They both are just declarations and cannot be executed. A subtype must provide the implementation and in this case, that's c.show(), therefore the only executable show() is c.show().
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic