File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding 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 "overriding" Watch "overriding" New topic
Author

overriding

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
can anyone expalin wat happens here .wats the explanation given down,here while o/p it shld print the hide() of cat,as per shadowing concept.

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.

another example
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}}

ans: Prints: P.printS1 Q.printS2 how???


Harish<br />SCJP 1.4 (85%)
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi

Static method looks on references
Instances methos looks on objects

hope this helps
Jimmy Thomas
Greenhorn

Joined: Aug 17, 2005
Posts: 19
Harish,

Static methods are invoked always on the reference type on which the call is made. Having said that, in your example 2, the call
new Q().printS1S2();, results in invoking the method declared in the base class.

Since the method definition of printS1S2 is,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding