Are static methods inherited in Java?

JavaInheritanceStatic

Java Problem Overview


I was reading A Programmer’s Guide to Java™ SCJP Certification by Khalid Mughal.

In the Inheritance chapter, it explains that

> Inheritance of members is closely tied to their declared > accessibility. If a superclass member is accessible by its simple name > in the subclass (without the use of any extra syntax like super), that > member is considered inherited

It also mentions that static methods are not inherited. But the code below is perfectlly fine:

class A
{
    public static void display()
    {
        System.out.println("Inside static method of superclass");
    }
}

class B extends A
{
    public void show()
    {
        // This works - accessing display() by its simple name -
        // meaning it is inherited according to the book.
        display();
    }
}

How am I able to directly use display() in class B? Even more, B.display() also works.

Does the book's explanation only apply to instance methods?

Java Solutions


Solution 1 - Java

All methods that are accessible are inherited by subclasses.

From the Sun Java Tutorials:

> A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members

The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.

From the page on the difference between overriding and hiding.

> The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass

Solution 2 - Java

You can experience the difference in following code, which is slightly modification over your code.

class A {
    public static void display() {
        System.out.println("Inside static method of superclass");
    }
}

class B extends A {
    public void show() {
        display();
    }

    public static void display() {
        System.out.println("Inside static method of this class");
    }
}

public class Test {
    public static void main(String[] args) {
        B b = new B();
        // prints: Inside static method of this class
        b.display();

        A a = new B();
        // prints: Inside static method of superclass
        a.display();
    }
}

This is due to static methods are class methods.

A.display() and B.display() will call method of their respective classes.

Solution 3 - Java

Static methods in Java are inherited, but can not be overridden. If you declare the same method in a subclass, you hide the superclass method instead of overriding it. Static methods are not polymorphic. At the compile time, the static method will be statically linked.

Example:

public class Writer {
	public static void write() {
		System.out.println("Writing");
	}
}

public class Author extends Writer {
	public static void write() {
		System.out.println("Writing book");
	}
}

public class Programmer extends Writer {
	
	public static void write() {
		System.out.println("Writing code");
	}
	
	public static void main(String[] args) {
		Writer w = new Programmer();
		w.write();
		
		Writer secondWriter = new Author();
		secondWriter.write();
		
		Writer thirdWriter = null;
		thirdWriter.write();
		
		Author firstAuthor = new Author();
		firstAuthor.write();
	}
}

You'll get the following:

Writing
Writing
Writing
Writing book

Solution 4 - Java

If that's what the book really says, it's wrong.[1]

The Java Language Specification #8.4.8 states:

> # 8.4.8 Inheritance, Overriding, and Hiding > > A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true: > > - m is a member of the direct superclass of C. > >- m is public, protected, or declared with package access in the same package as C. > >- No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.

[1] It doesn't say that in my copy, 1st edition, 2000.

Solution 5 - Java

B.display() works because static declaration makes the method/member to belong to the class, and not any particular class instance (aka Object). You can read more about it here.

Another thing to note is that you cannot override a static method, you can have your sub class declare a static method with the same signature, but its behavior may be different than what you'd expect. This is probably the reason why it is not considered inherited. You can check out the problematic scenario and the explanation here.

Solution 6 - Java

Static methods are inherited in Java but they don't take part in polymorphism. If we attempt to override the static methods they will just hide the superclass static methods instead of overriding them.

Solution 7 - Java

This concept is not that easy as it looks. We can access static members without inheritance, which is HasA-relation. We can access static members by extending the parent class also. That doesn't imply that it is an ISA-relation (Inheritance). Actually static members belong to the class, and static is not an access modifier. As long as the access modifiers permit to access the static members we can use them in other classes. Like if it is public then it will be accessible inside the same package and also outside the package. For private we can't use it anywhere. For default, we can use it only within the package. But for protected we have to extend the super class. So getting the static method to other class does not depend on being Static. It depends on Access modifiers. So, in my opinion, Static members can access if the access modifiers permit. Otherwise, we can use them like we use by Hasa-relation. And has a relation is not inheritance. Again we can not override the static method. If we can use other method but cant override it, then it is HasA-relation. If we can't override them it won't be inheritance.So the writer was 100% correct.

Solution 8 - Java

Static methods in class are inherited while static methods in interface aren't inherited.

Static methods in class can be invoked using instance while static methods in interface can be invoked only through interface name as they aren't inherited

Solution 9 - Java

All the public and protected members can be inherited from any class while the default or package members can also be inherited from the class within the same package as that of the superclass. It does not depend whether it is static or non static member.

But it is also true that static member function do not take part in dynamic binding. If the signature of that static method is same in both parent and child class then concept of Shadowing applies, not polymorphism.

Solution 10 - Java

Static method is inherited in subclass but it is not polymorphism. When you writing the implementation of static method, the parent's class method is over hidden, not overridden. Think, if it is not inherited then how you can be able to access without classname.staticMethodname();?

Solution 11 - Java

You can override static methods, but if you try to use polymorphism, then they work according to class scope(Contrary to what we normally expect).

public class A {

	public static void display(){
		System.out.println("in static method of A");
	}
}

public class B extends A {

	void show(){
		display();
	}

     public static void display(){
		System.out.println("in static method of B");
	}

}
public class Test {

	public static void main(String[] args){
		B obj =new B();
		obj.show();
		
		A a_obj=new B();
		a_obj.display();
		
		
	}
	
	
}

IN first case, o/p is the "in static method of B" # successful override In 2nd case, o/p is "in static method of A" # Static method - will not consider polymorphism

Solution 12 - Java

We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism.Because since all static members of a class are loaded at the time of class loading so it decide at compile time(overriding at run time) Hence the answer is ‘No’.

Solution 13 - Java

Many have voiced out their answer in words. This is an extended explanation in codes:

public class A {
	public static void test() {
		System.out.println("A");
	}
	public static void test2() {
		System.out.println("Test");
	}
}

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

// Called statically
A.test();
B.test();
System.out.println();

// Called statically, testing static inheritance
A.test2();
B.test2();
System.out.println();

// Called via instance object
A a = new A();
B b = new B();
a.test();
b.test();
System.out.println();

// Testing inheritance via instance call
a.test2();
b.test2();
System.out.println();

// Testing whether calling static method via instance object is dependent on compile or runtime type
((A) b).hi();
System.out.println();

// Testing whether null instance works
A nullObj = null;
nullObj.hi();

Results:

A
B

Test
Test

A
B

Test
Test

A

A

Therefore, this is the conclusion:

  1. When we call the static in a static manner via ., it will look for the static defined in that class, or the class nearest to that in the inheritance chain. This proves that static methods are inherited.
  2. When static method is called from an instance, it calls the static method defined in the compile-time type.
  3. Static method can be called from a null instance. My guess is that the compiler will use the variable type to find the class during compilation, and translate that to the appropriate static method call.

Solution 14 - Java

Static members are universal members. They can be accessed from anywhere.

Solution 15 - Java

Static members will not be inherited to subclass because inheritance is only for non-static members.. And static members will be loaded inside static pool by class loader. Inheritance is only for those members which are loaded inside the object

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlgorithmistView Question on Stackoverflow
Solution 1 - JavayincrashView Answer on Stackoverflow
Solution 2 - JavaGauravView Answer on Stackoverflow
Solution 3 - Javauser1134181View Answer on Stackoverflow
Solution 4 - Javauser207421View Answer on Stackoverflow
Solution 5 - JavaKaiView Answer on Stackoverflow
Solution 6 - JavaPraveen KumarView Answer on Stackoverflow
Solution 7 - JavaNoman_ibrahimView Answer on Stackoverflow
Solution 8 - JavaVenkateshView Answer on Stackoverflow
Solution 9 - JavashubhamView Answer on Stackoverflow
Solution 10 - Javauser4016405View Answer on Stackoverflow
Solution 11 - JavaManav GarekarView Answer on Stackoverflow
Solution 12 - JavaNGKView Answer on Stackoverflow
Solution 13 - JavaJaiView Answer on Stackoverflow
Solution 14 - JavaPavanView Answer on Stackoverflow
Solution 15 - JavaAshwini EView Answer on Stackoverflow