wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods can�t be overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static methods can�t be overridden" Watch "static methods can�t be overridden" New topic
Author

static methods can�t be overridden

Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

" static methods can�t be overridden. "

But i am able to do that. The cod below is compiling fine. Tell me is this legal override? if not, please explain me.

public class Section1 {

public static void main(String[] args) {
Section1SubClass.override();
}

public static void override() {
System.out.println("SOP IN Section1");
}
}

------------------------------------------------

public class Section1SubClass extends Section1{
public static void override(){
System.out.println("SOP");
}
}


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
What's it printing?? The test of override is not just whether that code compiles, but whether the features of overriding can be observed at runtime or not.

You might want to read up on this matter.


ASCII silly question, Get a silly ANSI.
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Hi prabhu, here is an explanation for your question which i copied from a tutorial. It might be helpful to you to understand.


If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications. Let's look at an example to see why. This example contains two classes. The first is Animal, which contains one instance method and one class method:

public class Animal {
public static void hide() {
System.out.println("The hide method in Animal.");
}
public void override() {
System.out.println("The override method in Animal.");
}
}

The second class, a subclass of Animal, is called Cat:
public class Cat extends Animal {
public static void hide() {
System.out.println("The hide method in Cat.");
}
public void override() {
System.out.println("The override method in Cat.");
}

public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
}
}

The Cat class overrides the instance method in Animal called override and hides the class method in Animal called hide. The main method in this class creates an instance of Cat, casts it to a Animal reference, and then calls both the hide and the override methods on the instance. The output from this program is as follows:
The hide method in Animal.
The override method in Cat.

The version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass. For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. In the example, the compile-time type of myAnimal is Animal. Thus, the runtime system invokes the hide method defined in Animal. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called. In the example, the runtime type of myAnimal is Cat. Thus, the runtime system invokes the override method defined in Cat.
[ November 29, 2005: Message edited by: Mani Kumar Santosh Srinivas ]
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Stuart,

Its printing "SOP". But now I understand, even though it looking like legal override, but it won�t work for run time polymorphism.

public class Section1 {
public static void main(String[] args) {
Section1SubClass.test();
}
public static void override() {
System.out.println("SOP IN Section1");
}

}

--------------------

public class Section1SubClass extends Section1{
public static void test() {
Section1 sect = (Section1)new Section1SubClass();
sect.override();
override();
}
public static void override(){
System.out.println("SOP");
}
}

Result :- SOP IN Section1 and then its printing SOP.


But if I remove �static� keyword from �override� methods in both the class its printing �SOP� twice.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

thanks a lot Mani. i got it.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Prabhu"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods can�t be overridden
 
Similar Threads
class methods vs instance method
overriding static methods
Static (Overriden or Hidden) ???????
The game of statics
non-static shades static