Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

overriding vs redefining?

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the K&B for SCJP5 Books page 147 when illustrate static ...there are different between overriding and redefining... ooh!what is the different?
Thanks.
[ March 14, 2006: Message edited by: alaa trok ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You cannot overide static method as static method belog to the class and overriding is a behaviour related to an instance of a class. we can only override instance methods of a class.

You can declare a static method in the child class but the Overiding rules will not apply to the static method and it will be considered a static method only for the sub class.
for eg:
class Super
{
static void meth(){}
}

class Sub
{
static void meth() {} ---> this method is not overridden. Its specific to the class.

}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Seema said, static methods belong to the class itself, which is why you can call static methods directly from the class(example: Math.cos(4.54)).

An important implication of this is that if you have a superclass reference to a subclass object (Superclass super = new Subclass()) and the Subclass has *redefined* a static method in Superclass, if you call super.staticMethod() (not good practice, but legal) you will _not_ get the static method of the Subclass object, as you would if you invoked an overridden instance method, instead you get the Superclass method because you have a Superclass reference.
[ March 14, 2006: Message edited by: David Rowland ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more details, see Overriding vs. Hiding.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic