• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

overridding static methods

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can be overridden by static methods only.
Is this a valied statement.
ThanQ
Hima
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 8.4.6.2 Hiding (by Class Methods)


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
...

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hima it is generally known as hiding for static methods. Check this from JLS
8.4.6.2 Hiding (by Class Methods)
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
Link to JLS http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#39617
-Arun
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I ve read the responses and the referred section of the JLS. Still confused though. Are we talking semantics here? I take it that you can, for all practical purposes, override a static method with another static method, but it's called hiding instead? And that includeds private methods as well? Thanks.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike
i played around with the example
class Base {
int R= 45;
static void Print()
{
System.out.println("Starting Base...");
}
}
class Sub extends Base {

int R= 88;


static void Print()
{
System.out.println("Starting Sub.");
}

public static void main(String args[]) {
Sub b= new Sub();
Base a= new Base();
Base c= new Sub();
b.Print();
a.Print();
c.Print();

System.out.println(c.R);
System.out.println(b.R);
}
}
This prints out Starting sub
Starting Base
Starting Base
45
88
The static Print Method in Base was overwritten by the static Print Method in Sub alos the variable R is shadowed in the subclass Sub.
however if i make the Print method in Base public and i made the one in Sub static i got the following message:
Print() in Sub cannot override Print() in Base; Print() and Print() are static
static void Print()
which explains :
which explains :If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method which explaisn this from JLS also you may want to play with the acess modifiers for the variable R and Print() in both classes and see what kind happens and what kind of errors the compliler will generate . hope this helps
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tosin, I guess that helps since you were able to override print(). However when you changed Base.print() to public, you then tried to override it with a friendly print() in Sub, which the compilier won't allow as you can't override to be more exclusive. Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!