3

There is a code:

class Test1{
    public static void main(String[] args){     
        System.out.println("Test1");
    }
}

class Test2 extends Test1{
}

When I try to execute java Test1 I'm getting, of course, this:

Test1

But, when I try to execute java Test2 I'm still getting:

Test1

Why? In class Test2 doesn't exist main() method. And static methods don't inherited. If I'll add main() in Test2 (with string "Test2" instead of "Test1") I'll get:

Test2

I understand why I'm getting Test2 in this example. But don't understand why I'm getting Test1 if main() doesn't exist in class Test2.

  • 6
    `And static methods don't inherited.` Who told you that? Static methods can't be **overridden** (they are not polymorphic), but they can be *inherited* and *shadowed* like you see clearly in your example. – Pshemo Mar 22 '14 at 12:38
  • @KlemensMorbe now that's a lie. – Boris the Spider Mar 22 '14 at 12:41
  • possible duplicate of [Are static methods inherited in Java?](http://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java) – Pshemo Mar 22 '14 at 12:49
  • When classloader can't find main() method in class, does he search main() method in parent classes? – Too Strong Magic Mar 22 '14 at 12:57
  • My previous comment has error: static methods are *hidden*, not *shadowed*. – Pshemo Mar 22 '14 at 13:03

2 Answers2

3

And static methods don't inherited.

Static methods are inherited. Take a look at jls-8.4.8 Inheritance, Overriding, and Hiding

A class C inherits from its direct superclass and direct superinterfaces all abstract and non-abstract methods of the superclass and superinterfaces that are public, protected, or declared with default access in the same package as C, and are neither overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the class.

There is no information about not inheriting static methods, which is why you can run main declared in Test1 via Test2 class (it was inherited).


Also from jls-8.4.8.2 Hiding (by Class Methods)

If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.

So when you created new main method in Test2 class you hidden (not overridden) Test1.main method which is why you saw as output

Test2
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Thank you. If i understand correctly, when creating Class-object, Test2 inherits static method main() from Test1. And if don't create it explicitly, then will invoked inherited method. – Too Strong Magic Mar 22 '14 at 13:32
2

Static methods do in fact get inherited. That's what's happening here. Ex. this works just fine:

class Base {
    public static void saySomething() {
        System.out.println("Hi!");
    }
}

class Extended extends Base {
    public static void main(String[] args) {
        saySomething();
    }
}
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83