tags:

views:

175

answers:

7

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?

+1  A: 

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.

Kaleb Brasee
+2  A: 

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?

  1. Create a class A with a staticMethod()
  2. Create a class B extends A
  3. Try calling the static method with B.staticMethod()
Bozho
Inheritance relates specifically to instances. Static members are specifically UNRELATED to instances.
Adam Robinson
yes, I made a clarification about that.
Bozho
+1  A: 

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.

Nick Fortescue
+8  A: 

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
Winston Smith
The question was tagged Java - this looks like C++/C# to me. Hence vote down
Nick Fortescue
@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
@Nick - fixed. Hard to get away from the beauty of C# :)
Winston Smith
@Adam I always vote down if incorrect, which it was. Now it's fixed I've voted up.@Winston - thanks for edit
Nick Fortescue
@Winston - I've made the code a bit more Java code style as well
Nick Fortescue
@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
A: 

The parent static method will be accessible in subclass, but cannot be overriden.

Dolfa
A: 

Yes you can inherit static methods. But if your static method has private access modifier you can't inherit it

Upul
A: 

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: http://stackoverflow.com/questions/87192/when-would-you-need-to-use-late-static-binding/87351#87351

Alex