Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Is it necessary for an abstract class to implement all abstract interface methods

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was under the impression of the following:
  • For any class extending a class with abstract methods, the extending class needs to implement all abstract methods, or become abstract itself
  • For any class implementing an interface with abstract methods, the implementing class needs to implement all abstract methods, regardless of whether the class is abstract itself


  • However, when I run the following code, it compiles without problems. I am not getting an error about the TheClass not implementing doSomething().



    Is my impression wrong? Are the rules for classes extending classes with abstract methods and classes implementing interfaces with abstract methods actually the same?

    Thanks in advance!

    Shane
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Shane Jensen wrote:For any class implementing an interface with abstract methods, the implementing class needs to implement all abstract methods, regardless of whether the class is abstract itself


    That's incorrect!

    The rule is fairly simple: a concrete (non-abstract) class must implement all abstract methods. It doesn"t matter if the abstract method is defined in another (abstract) superclass, an interface (which it implements) or both. An abstract class can provide an implementation for an abstract method defined in another (abstract) superclass, an interface (which it implements) or both, but it's definitely not required.
     
    Rebecca Wolf
    Ranch Hand
    Posts: 113
    7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:

    Shane Jensen wrote:For any class implementing an interface with abstract methods, the implementing class needs to implement all abstract methods, regardless of whether the class is abstract itself


    That's incorrect!

    The rule is fairly simple: a concrete (non-abstract) class must implement all abstract methods. It doesn"t matter if the abstract method is defined in another (abstract) superclass, an interface (which it implements) or both. An abstract class can provide an implementation for an abstract method defined in another (abstract) superclass, an interface (which it implements) or both, but it's definitely not required.


    Wow, such a quick reply at this time! (I'm assuming you're in Belgium)

    So basically, the rule for classes implementing interfaces with abstract methods, and for classes extending classes with abstract methods is the same? Either implement all abstract methods, or become abstract yourself?

    Thank you for your reply. I wonder where I got this idea from.
     
    Ranch Hand
    Posts: 117
    11
    Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Shane Jensen wrote:Either implement all abstract methods, or become abstract yourself?



    You can check this out with the following code

    The compiler says:

    InterfaceTest.java:7: error: InterfaceTest is not abstract and does not override abstract method print() in Interface1
    public  class InterfaceTest implements Interface1 {


    So you are supposed to override the method or change the class to become abstract
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Shane Jensen wrote:So basically, the rule for classes implementing interfaces with abstract methods, and for classes extending classes with abstract methods is the same? Either implement all abstract methods, or become abstract yourself?


    Yes!

    And that makes so much sense and provides consistency regardless where the abstract method is defined. Following your (faulty) impression it would be very inconsistent. This code would compileBut if Abstract would have been an interface, this code snippet would not compile anymore Furthermore it would have very strong limitations of what's possible. Imagine the following class hierarchyThis would not have been possible and Carnivore must implement all abstract methods defined in MeatLover, although the actual implementation for both methods depends on the actual carnivore (the meat requirements for a Lion are different than for a Piranha).
     
    Rebecca Wolf
    Ranch Hand
    Posts: 113
    7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your detailed explanations, Roel and João, you have both been enormously helpful!
     
      Bookmark Topic Watch Topic
    • New Topic