• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding abstract methods

 
Michael Carlson
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To prevent the superclass's method from behind called ? (if the abstract method contains some implementation)
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant a method in an abstract class. Got confused
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.:
 
Tia Ko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Lakhdeep Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Timothy E. Hay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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 public.

Cheers,

Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic