Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused in output

 
Gurjit Sandhu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dear.
now i got it. it means static methods or data variables can't be overriden.
thansk once again.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic