This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Overridding abstract methods

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why would you want an abstract method of a subclass to override an abstract method of the superclass?
Thank you
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To prevent the superclass's method from behind called ? (if the abstract method contains some implementation)
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
To prevent the superclass's method from behind called ? (if the abstract method contains some implementation)



Abstract methods cannot have implementation.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I meant a method in an abstract class. Got confused
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can think only one thing which can be achieved by doing this i.e.

making a protected abstract method in super class, a public abstract method
E.g.:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, one can do the following:
a) Increase the accessibility of the method
b) Reduce the number of exception thrown
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
firstly i want to tell u what r abstract methods--which have no body.
Basically we use abstract methods to hide the implementation from the classes which have their prototypes. Overriding of abstract method is compulsary if u dont want 2 make sub class as an abstract & want to give the body 2 abstract method , otherwise it's not necessary at all.

Thank you
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are three main reasons:

1) To ensure that for subsubclasses which eventually implement the method, the return type is narrowed to a subclass of the return type specified by the superclass.
eg:



2) As already mentioned by Tia, to reduce the number of exceptions thrown by classes derived from the subclass, BUT ALSO to restrict the exceptions thrown by these classes' implementation to subclasses of the Exceptions declared in abstract method declaration in the superclass.

3) As already mentioned by Hamid and Tia, to increase the accessibility of the method in classes derived from the subclass - ie: from default or protected to coderanch.

Cheers,

Tim
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic