• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between redefining and overriding a method

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the diffference between overriding and redefining a method??
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50180
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
Campbell Ritchie
Sheriff
Pie
Posts: 50180
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pradeepta chopra:
thanks all
You're welcome
 
Asif sheikh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic