aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overidden Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overidden Method" Watch "Overidden Method" New topic
Author

Overidden Method

Bhaswati Karmakar
Ranch Hand

Joined: May 10, 2001
Posts: 32
In order to override a method, what are things that should be taken into consideration ?
Thanks
SRV Gopal
Ranch Hand

Joined: May 16, 2001
Posts: 44
Hi Bhaswati,
In order to 'override' a method that is in a superclass, in the subclass, declare/define the same method with the same 'signature', but give the new implementation that is specific to the subclass ... I can't think of anything else.
Please correct me if I am wrong.


S.R.VenuGopal,<br />Senior Analyst/Programmer,<br />B.E.(EE), GNIIT, SCJP2
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Also, remember that when you override a method ( not a constructor) that the errors thrown cannot be greater than the superclass.
In other words, if a parent method throws exception x, the child process can throw x and any exception that is a child of x. Also, the child could also not throw an exception.
The above note to exceptions is in addition to what was mentioned earlier that the parameters must be the same.
Also the access method of the child method must not restrict
the parent method. For example, if the parent method has protected access, the child method can have protected or public access. A friendly parent method, can be overriden by friendly, protected or public.
Please correct if anyone finds this incorrect.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Folks,
Further to what Charlie has said. You cant override a
static method.
Ravindra Mohan..
Bhaswati Karmakar
Ranch Hand

Joined: May 10, 2001
Posts: 32
Hi
Thanks to all for your posts.
So to override the method, should it have the same return type, same list of arguments with same data type as the parent method ?
Could you please clarify ?
Sodiambakkam !
Could you please clarify the word 'signature'? Other than name, what else does it include.
thanks
bhakti soman
Greenhorn

Joined: Nov 12, 2000
Posts: 22
HI all
above to all you can't override final method
Originally posted by Bhaswati Karmakar:
In order to override a method, what are things that should be taken into consideration ?
Thanks

SRV Gopal
Ranch Hand

Joined: May 16, 2001
Posts: 44
Hi Bhaswati,
By 'Signature', I meant the exact 'number' and 'type' and 'order' of parameters. 'Overridden' methods must have exact same 'Signature' while 'Overloaded' methods must have different 'Signature' ... I hope I make sense
SRV Gopal
Ranch Hand

Joined: May 16, 2001
Posts: 44
Hi Bhaswati,
I quote from java.sun.com tutorial on overriding methods:
"The return type, method name, and number and type of the parameters for the overriding method must match those in the overridden method. The overriding method can have a different throws clause as long as it doesn't declare any types not declared by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more access than the overridden method, but not less. For example, a protected method in the superclass can be made public but not private.A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message.Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass.A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract."
Bhaswati Karmakar
Ranch Hand

Joined: May 10, 2001
Posts: 32
Thank to all very much. It helped.
Sodiambakkam!
Thanks for the quote.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overidden Method