In Inheritance concept, i have a static method in super class and i am inheriting that class to one sub class. In that case the static method is inherited to sub class or not?
7 Answers
Remember that static methods are not instance methods - they relate to a class and not an instance. Since the derived class can be considered to be of the base type, you can access the static method via the derived type.
Given:
class A {
public static void foo(){}
}
class B extends A {
}
Then:
B.foo(); // this is valid

- 43,045
- 26
- 106
- 134

- 21,585
- 10
- 60
- 75
-
The question was tagged Java - this looks like C++/C# to me. Hence vote down – Nick Fortescue Dec 17 '09 at 13:41
-
1@Nick: you voted down because he used a colon rather than the word `extends`? Really? The content is perfectly correct apart from that. – Adam Robinson Dec 17 '09 at 13:45
-
2@Nick - fixed. Hard to get away from the beauty of C# :) – Winston Smith Dec 17 '09 at 13:47
-
@Adam I always vote down if incorrect, which it was. Now it's fixed I've voted up. @Winston - thanks for edit – Nick Fortescue Dec 17 '09 at 14:25
-
@Winston - I've made the code a bit more Java code style as well – Nick Fortescue Dec 17 '09 at 14:27
-
@Nick: The debate about whether or not brackets should be on their own line extends (no pun intended!) much farther back than Java ;) – Adam Robinson Dec 17 '09 at 16:54
Wait, I stand corrected -- static methods ARE inherited. Sort of. However, they don't act like real OO inheritance. For example, with these classes:
public class Parent {
public static void printIt() {
System.out.println("I'm Parent!");
}
}
public class Child extends Parent {
public static void printIt() {
System.out.println("I'm Child!");
}
public static void main(String[] args) {
Parent child1 = new Child();
Child child2 = new Child();
child1.printIt();
child2.printIt();
}
}
If you call child1.printIt()
, it'll use Parent.printIt()
, because child1 has been cast to a Parent
class. If you call child2.printIt()
, however, it'll use Child.printIt()
, because it's cast to a child. So it's not really true inheritance, because overriding doesn't stick if you cast to a supertype. Thus it's not a true polymorphism.

- 51,193
- 8
- 108
- 113
It is inherited, in the sense that it can be accessed as a static method of any subclass
Why didn't you try it yourself?
- Create a
class A
with astaticMethod()
- Create a
class B extends A
- Try calling the static method with
B.staticMethod()

- 588,226
- 146
- 1,060
- 1,140
-
Inheritance relates specifically to instances. Static members are specifically UNRELATED to instances. – Adam Robinson Dec 17 '09 at 13:36
If the method is public static or protected static in the superclass it will be accessible in the subclass. So in that sense it is inherited. The code below will compile and run fine.
public class A {
public static String foo() {
return "hello world";
}
}
public class B extends A {
public void bar() {
System.out.println(foo());
}
}
However, this is a bad use of the term "inherited" as it is not tied to a particular instance - hence Kaleb's answer. Normal OO design does not treat static methods as inherited, and it gets very confusing, especially when you start talking about overriding them.

- 43,045
- 26
- 106
- 134
Yes you can inherit static methods. But if your static method has private access modifier you can't inherit it

- 5,973
- 4
- 37
- 60
Another (advanced) concept that you would probably care about here is the concept of "late static binding" - overriding static functions in child classes. Here's a question on this from the PHP world, but it applies across the board: When would you need to use late static binding?

- 1
- 1

- 9,823
- 9
- 42
- 59