aspose file tools*
The moose likes Beginning Java and the fly likes confused in output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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://aspose.com/file-tools
 
subject: confused in output