• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

difference between redefining and overriding a method

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pradeepta chopra:
thanks all

You're welcome
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}
}
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic