• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg:Override and redefine

 
prema venkat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai all,
i cant understand the difference between override and redefine of a sstatic method.
we know static cant be override but we can redefine in subclass means what it mean?

for example:
class Animal
{
static void daStuff() //Line 1
{
System.out.println("a");
}
}
class Dog extends Animal{
static void daStuff() //line2
{
System.out.println("d");
}
public static void main(String arg[])
{
Animal[] a={new Animal(),newDog(),newAnimal());
for(int x=0;x<a.length;x++)
a[x].doStuff();

}
}

Here line 2 seems like an override of line 1 but in the defination of this program it seems like line2 is redefinition not override.(coz static)

Please clear by doubt that how the redefinition differs from override coz its having same modifier and return type as like override.

Thanks in advance
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 98, Chapter 2 of our famous SCJP book explains it well, I think: "Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type."
 
prema venkat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris.Now i am clear with this.
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prema,

as you know, static is per class.

Overriding is the concept of polymorphism which can be applied to objects.

So the Animal class' doStuff belongs to that class only and Dog class' doStuff belongs to that class.

Even the object of Dog dont have a idea of the Animal class' doStuff method

Thats why its called redefinition

Hope you got the point

cheers
Guru
 
prema venkat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Guru.I got the point.Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic