| Author |
Overridding abstract methods
|
Michael Carlson
Ranch Hand
Joined: Sep 11, 2005
Posts: 78
|
|
why would you want an abstract method of a subclass to override an abstract method of the superclass? Thank you
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
|
To prevent the superclass's method from behind called ? (if the abstract method contains some implementation)
|
[My Blog]
All roads lead to JavaRanch
|
 |
Jay Pawar
Ranch Hand
Joined: Aug 27, 2004
Posts: 411
|
|
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.
|
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
Sorry, I meant a method in an abstract class. Got confused
|
 |
Hamid Virani
Ranch Hand
Joined: Jul 26, 2005
Posts: 41
|
|
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
Joined: Jan 31, 2006
Posts: 4
|
|
Yup, one can do the following: a) Increase the accessibility of the method b) Reduce the number of exception thrown
|
 |
Lakhdeep Singh
Greenhorn
Joined: Feb 01, 2006
Posts: 3
|
|
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
Joined: Jan 31, 2006
Posts: 22
|
|
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
|
It's just what I reckon... ...but it's what I reckon.
|
 |
 |
|
|
subject: Overridding abstract methods
|
|
|