File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes confused in output Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "confused in output" Watch "confused in output" New topic
Author

confused in output

Gurjit Sandhu
Greenhorn

Joined: Jan 29, 2009
Posts: 11
class Dog {

public static void bark() {

System.out.print("woof ");

}

}



class Basenji extends Dog {

public static void bark() { }

}



public class Bark {

public static void main(String args[]) {

Dog woofer = new Dog();

Dog nipper = new Basenji();

woofer.bark();

nipper.bark();

}

}

it appear that this program should just print woof but it print woof woof.
why it prints woof 2 times.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Gurjit,

It prints two time 'woof' because it calls two times bark method of class Dog. Remember one thing that you can't override the static methods because they are related to class and not specific to object. What do you say?
Gurjit Sandhu
Greenhorn

Joined: Jan 29, 2009
Posts: 11
Thanks dear.
now i got it. it means static methods or data variables can't be overriden.
thansk once again.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
You are welcome.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: confused in output
 
Similar Threads
method call
Please clarify my understanding of Polymorphism
Quick question about instance variables
Reference Variable casting ... compile Errors vs Runtime errors
SCJP Brainteaser (11)