Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static methods can be inherited and overrriden, something wrong!!!  RSS feed

 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main (String... arg) throws Exception {
B.sleep();
// the output is "I want to sleep, it is late"
//and if you activate sleep method in B the output will be "B"
}
}

class A {
public static void sleep() {
System.out.println("I want to sleep, it is late");
}
}

class B extends A{
/*
public static void sleep() {
System.out.println("B");
}
*/
}

// there is no object there So What is happened
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Overriding vs. Hiding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!