Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java abstract methods

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declare a method in an abstract class should it be always declared in abstract subclasses as well or only when defined in leaf classes?
Thanks guys for help
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm understanding you, yes, a method can be declared abstract in a subclass, but then that subclass must be abstract too.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Ayoub!

As Knute says, the abstract method can be declared again in an abstract subclass, but it definitely doesn't have to be. A normal (non-abstract) class just has to implement all the abstract methods it has inherited from parent class(es) and interfaces.

I can imagine only a few reasons to declare an method class again in an abstract class:
  • The new declaration returns more specific type than the original class (for example, the parent class method might return a Number, while the child class method returns a Double.
  • The new declaration has a broader scope than the original one, for example public instead of protected (it looks like a questionable design, though).
  • The new declaration is accompanied by a more specific comment than the one provided by the parent class.

  • Personally, I'd avoid declaring the abstract method in the child class again without a good reason.
     
    Marshal
    Posts: 76479
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch again.

    I agree with Martin that it usually isn't a good idea to redeclare such a method. If you do redeclare it, be sure to use the @Override annotation, to detect any tiny spellling errors you might have made.
     
    There is no "i" in denial. Tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic