• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Reg:Override and redefine

 
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
 
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.
 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • 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.
 
Forget Steve. Look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic