aspose file tools*
The moose likes Beginning Java and the fly likes difference between redefining and overriding a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between redefining and overriding a method" Watch "difference between redefining and overriding a method" New topic
Author

difference between redefining and overriding a method

pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
what is the diffference between overriding and redefining a method??
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
i came acrros this while reading scjp 5 book by sierra and bates.

the topic said that static members cant be overridden.
but they can be redefined.
for eg,
[B]
[/B]

i could not understand the diffeence between the two.
doesnt the above code stating redefinition same as overriddance??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Overriding is closely connected to polymorphism. Static methods are not polymorphic however.

Consider the following:

If doStuff would be a non-static method, it would call Dog.doStuff(). Since it is static however, it uses the reference type Animal instead of the actual type Dog - "a" is printed, not "d".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2711
    
    5

Just to add on, static methods are bound to class rather than objects , so runtime polymorphism is not possible at all.
Therefore you cannot override them.


SCJP, SCWCD.
|Asking Good Questions|
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
that means if i have to find out whether it is overriding or redefining in a subclass, i should look for the keyword static in the method signature in the superclass.
is it like that??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Probably better to use an annotation. Put @Override before the heading of your overridden method and see what happens. If there is the tiniest spelling mistake in the method heading, you will get a compiler error. If you try to override a static method you will get a compiler error. If you misspell "@Override" even slightly it won't work. If you use it on Java1.4 or older you will get a compiler error because annotations were introduced in Java 5.

If you are overriding a method, then you end up with an instance method with the same signature and return type (in Java 5 the return type rule was relaxed slightly) as an instance method in the superclass.
Anything else is redefinition or hiding (see this FAQ). Find a copy of Joshua Bloch's Effective Java, and you find he has a simple rule of thumb about when you should use hiding . . . never!

CR
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
thanks all
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by pradeepta chopra:
thanks all
You're welcome
Asif sheikh
Greenhorn

Joined: Jul 24, 2012
Posts: 3
HI All as pr my understanding "Rob Spoor" call doStuff method by animal.doStuff(); so the Dog class object call the dog class method end print "b" Not "a".....
for refrance please see the below exp and run it....


class Animal
{
void doStuff()
{
System.out.println("Aniaml");

}
}

class Dog extends Animal

{
void doStuff()

{
System.out.println("Dog");

}
}

public class TestAnimal
{
public static void main(String as[])

{
Animal a= new Animal();
Animal b= new Dog();

a.doStuff();
b.doStuff();

}
}
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7045
    
  16

Asif sheikh wrote:please see the below exp and run it....

Hi Asif, and welcome to JavaRanch.

A few things:

1. When posting code, please UseCodeTags (←click) - and please read the page thoroughly; there are a few gotchas to know about.

2. I'm sure your intentions are great, but we try not to post "solutions" here. Please read the NotACodeMill page, and also the HowToAnswerQuestionsOnJavaRanch one.

3. Did you realize that you've revived a 4-year old thread? Again, try and avoid unless you have a good reason.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between redefining and overriding a method
 
Similar Threads
what is the difference between method overriding and redefining
what is the Difference between method overriding and redefining
Overriding and redefining
overriding Vs Redefining
difference between redefining and overriding