• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static (Overriden or Hidden) ???????

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
After going thorough many previous posts, I concluded that
STATIC METHODS CAN"T BE OVERRIDEN, BUT THEY ARE HIDDEN.
But while I was experimenting a piece of code on static methods
as below
class A{
protected static void st(){
System.out.println("Aruna");
}
}
class B extends A{
// public static void st() {//If given like this no error
private static void st() { //compilation error
System.out.println("Aru");
}
}
The compiler screams saying
C:\Java\bin>javac Test.java
Test.java:20: The method void st() declared in class B cannot override the method of the same signature declared in class A. The access modifier is made more restrictive.
private static void st() {//It's gives compilation error
^
1 error
Why does the access modifier "private" in the class B cause the compiler crib...? Does that mean Static Methods can be OVERRIDDEN.......
Can anyone explain me clearly....
Thx in Advance.
Aruna
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it not happening that the child class is trying to override a method with more restricton? some of the basic rules? or am i wrong?
Aruna i think you are overridng in the child class with a more restriction. You can override a protected with proteced or public. but not private or default.
[This message has been edited by kishor kotecha (edited October 17, 2000).]
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kishor...
Static methods are never OVERRIDEN... they are bound to a class.
The rules of overriding what u said is perfectly fine for non static methods....
Or is my understanding about Static methods WRONG...? Can they be overriden..... ???
Aruna

[This message has been edited by Aru Ven (edited October 17, 2000).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kishor is right. Keep Cool when you solve a question.
 
vijay krishna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aruna What do you mean by HIDDEN??
STATIC METHODS CAN BE OVERRIDDEN ONLY BY ANOTHER STATIC METHOD.
STATIC METHOD MAY NOT BE OVERRIDDEN TO BE NON-STATIC.
Vijay.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A static method may not be overridden to be non-static(i.e. they are Hidden)
( Ref. http://www.javaranch.com/ubb/Forum24/HTML/004701.html )
but in this case, I think the same rule applies which is with non-static methods i.e. Methods can not be more private in sub-classes.
anyone pls correct me if I am wrong !
Manish Singhal
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
Please check out the following link. MA says that Static Methods are never overriden....
Aruna
http://www.javaranch.com/maha/Discussions/Language_Fundamentals/difference_between_static_and_final_method_-_JavaRanch_Big_Moose_Saloon.htm
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aruna,
"STATIC METHODS CAN"T BE OVERRIDEN, BUT THEY ARE HIDDEN"--that's right. But I think hiding also a sort of overriding.
Suppose, if we have method1() in subclass and superclasses like this..
Super class:
public void method1()
{
System.out.println("Super class");
}
Sub class:
public void method1()
{
System.out.println("Sub class");
}
SuperClass objSup = new SuperClass();
Sublass objSub = new SubClass();
SuperClass obj = new SubClass();
objSup.method1()---->should call super class method and print "Super Class "
objSub.method1()---->should call sub class method and print
"Sub Class "
obj.method1()---->should call sub class method and print
"Sub Class", because of overriding.

If method1() is static method, then out put would be
objSup.method1()---->should call super class method and print "Super Class "
objSub.method1()---->should call sub class method and print
"Sub Class "
obj.method1()----> will call Super class method and prints
"Super Class" (Even though reference is SubClass)
..So while hiding the static methods, we need to follow the rules of overriding, but it will not give the complete overriding functionality.
Please correct me , if I'm wrong.
Thanks,
Anand

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all I hope this is clear here, this is an example from JLS with some additions..


class Super
{
int x=10;
static String greeting() { return "overridden static method"; }

String name() { return "overridden instance method "; }
}
class Sub extends Super
{
float x=20.0f;
static String greeting() { return "overriding static method"; }
String name() { return "overriding instance method "; }
}
class Test
{
public static void main(String[] args)
{
Super s1 = new Sub();
Sub s2=new Sub();
System.out.println(s1.greeting() + ", " + s1.name()); 1
System.out.println(s2.greeting() + ", " + s2.name()); 2
System.out.println(s1.x); 3
System.out.println(s2.x); 4
}
}
here are the outputs
1. overridden static method,overiding instance method
2. overriding static method,overiding instance method
3.10
4. 20.0
In overriding instance variables and static members are considered as hidden not as overridden, hence a variable of type superclass ( it is referencing to an object of subclass type or of super class type. s1 in the Ex. ) always invokes members of super class if they are

1. static methods and static variables
2. Instance variables
and always invokes members of sub class if they are
1. Instacnce methods.
can please some one mail me how to use HTML tags to post a mesg.
thanks& regds.
vb_reddy@yahoo.com

[This message has been edited by vb reddy (edited October 19, 2000).]
[This message has been edited by vb reddy (edited October 19, 2000).]
[This message has been edited by vb reddy (edited October 19, 2000).]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my explanation goes.
Though you are able to compile the following code but still it hides the static method and NOT overrides it.
Please run the following code.
If it is really overrides the static method then it should have printed "Aru" right?. But it prints "Aruna". And implies that it just hides and NOT overrides static method.

Hope this helps,
Anna S Iyer.
[This message has been edited by Anna s Iyer (edited October 20, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic