Asked  7 Months ago    Answers:  5   Viewed   36 times

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?

 Answers

77

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

Tuesday, June 1, 2021
 
tika
answered 7 Months ago
100

Please have a look into the documentation of oracle: http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12110

Static variables are inherited as long as they're are not hidden by another static variable with the same identifier.

Thursday, June 10, 2021
 
laurent
answered 6 Months ago
55

Static methods are resolved on the compile-time type of the variable. m is of type Main, so the method in Main is called.

If you change it to SubMain m ..., then the method on SubMain will be called.

Friday, June 25, 2021
 
peixotorms
answered 6 Months ago
26

The final keyword, when applied to fields of a Java class, has nothing to do with inheritance. Instead, it indicates that outside of the constructor, that field cannot be reassigned.

Java treats name hiding and overriding separately. Overriding actually changes the observable behavior of the program at runtime by switching which function is called, while name hiding changes the program by changing the static interpretation of which field is being reference. final as applied to overriding only works for methods, because fields in Java cannot be overridden. The use of final in these different contexts is a bit confusing, unfortunately, and there is no way to prevent a field from having its name hidden in a subclass.

If you want the buildings to have different costs, one option would be to have an overridden getCost method which is overridden differently in each derived class. Alternatively, you could have a single protected or private field in the base class that stores the cost, then have each subclass set this field either directly (if this is protected) or through a base class constructor (if this field is private).

Hope this helps!

Thursday, August 12, 2021
 
Freddie
answered 4 Months ago
63

Static methods (and non-static methods, and static/member variables) are not loaded into memory directly: the declaring class is loaded into memory in its entirety, including all declared methods and fields. As such, there is no difference in the way that static/non-static methods/fields are loaded.

A class is only loaded by a class loader the first time it is referenced by other code. This forms the basis of the Initialization on demand idiom.

Wednesday, October 20, 2021
 
BeniaminoBaggins
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share