GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static (Overriden or Hidden) ??????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static (Overriden or Hidden) ???????" Watch "Static (Overriden or Hidden) ???????" New topic
Author

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

Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
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
kishor kotecha
Greenhorn

Joined: Oct 02, 2000
Posts: 27
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

Joined: Sep 28, 2000
Posts: 199
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).]
vijay krishna
Greenhorn

Joined: Oct 17, 2000
Posts: 8
Kishor is right. Keep Cool when you solve a question.
vijay krishna
Greenhorn

Joined: Oct 17, 2000
Posts: 8
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.
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

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

Joined: Sep 28, 2000
Posts: 199
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
Anand Shivadas
Greenhorn

Joined: Oct 17, 2000
Posts: 22
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

vb reddy
Greenhorn

Joined: Oct 18, 2000
Posts: 14
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).]
Anna s Iyer
Greenhorn

Joined: Aug 21, 2000
Posts: 24
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).]
 
GeeCON Prague 2014
 
subject: Static (Overriden or Hidden) ???????