This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what exactly overriding is? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what exactly overriding is?" Watch "what exactly overriding is?" New topic
Author

what exactly overriding is?

Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
hi friends,
please tell me what exactly overriding is?


Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You did say exactly? Then take this: Java Language Specification on method overriding


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
swathi mims
Greenhorn

Joined: Sep 04, 2005
Posts: 4
overriding is what happens to your methods when u do inheritence.lets for example take CLASS A and CLASS B WHICH EXTENDS CLASS A.assume class A has a method named public void Hello() and class B too has the same class with the same name and same arguements and return type.Then it tries to override the method from its superclass.when you thus try to access the hello() from main it will first access the overridden method from class B unless ther is a reference variable specified that refers to its superclass.

Hope u got it
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Note that in the JLS definition there is the concept of subsignature. This means that the superclass method could have a parameter of type List<? extends Number> for example, and the overriding method could have List (just List, the raw type) as its parameter type.

Also for reference return types, the overriding method's return type can be a subclass of the overridden method's return type.
[ September 11, 2005: Message edited by: Barry Gaunt ]
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
Hi Berry,
That link is really useful.
However I have doubt about the overriding method's return type can be a subclass of the overridden method's return type. as following code is not working.
plz clear my doubt




thanks in advance
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by Arvind Giri:
...as following code is not working.
plz clear my doubt ...



thanks in advance


Hi Arvind,
well the code is compiling and running with no errors
here is the modified version:



here "B y=(B)a.returnObject();//cast needed" cast is required since the overriden method returns the object of class A.

Please correct me if i am wrong...


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You code compiles OK for me: Are you using Java 5.0?
With Java 5.0 this code for class B:


runs as expected printing true showing that method returnObject() is being properly overridden.
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
hi friends,
I am still feeling problem in compiling that code



when I compiled above code I got the following error.


C:\j2sdk1.4.2_09\bin>javac test26.java
test26.java:12: returnObject() in B cannot override returnObject() in A; attempt
ing to use incompatible return type
found : B
required: A
public B returnObject()
^
1 error


Plz tell me where I am wrong?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You are using a version 1.4.2 compiler on code meant for the version 5.0 compiler.
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
hi Berry,
thanx for making it clear to me.
 
 
subject: what exactly overriding is?
 
Similar Threads
overriding and overloading
toString()
overriding static/nonstatic variables and methods
methods that are static AND final
Overriding