File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what exactly overriding is?

 
Arvind Giri
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
please tell me what exactly overriding is?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did say exactly? Then take this: Java Language Specification on method overriding
 
swathi mims
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using a version 1.4.2 compiler on code meant for the version 5.0 compiler.
 
Arvind Giri
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Berry,
thanx for making it clear to me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic