File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes static method in Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static method in Inheritance" Watch "static method in Inheritance" New topic
Author

static method in Inheritance

senthil rajan
Ranch Hand

Joined: Aug 31, 2005
Posts: 36
Hi Frnds,

static methods won't participate in inheritance since its belongs to its class only and we can't override this method. This is my understanding abt static method. But the following code shows compiler error when I change the return type in Subclass.

here is the code :
-------------------------------------------------------
class Parent{
static String display(){
return("Baseclass");
}

public class Child extends Parent{
static int display(){
return(12);
}
}
--------------------------------------------------------


Anyone pls help me where I misunderstood the concept.


Thanks in Advance.
Senthilrajan.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't know what "don't participate in inheritance" means. It is indeed true that static methods are not polymorphic, and you can't override them and get polymorphic behavior. But static methods are inherited in the sense that they're available amongst the child class's methods; you can call a static method using the child class's name, or using an instance of the child class, or without qualification in the body of a child class.

Therefore, if you define another method with the same name, you must follow the rules that apply to method overloading. It's not legal to have two overloaded methods with the same name and argument types but different return types, and that's the problem you're seeing here.


[Jess in Action][AskingGoodQuestions]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
If you can access any member of superclass by its simple name in subclass

without prefixing super, then that member is considered to be inherited.

This is the simple test.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Ernest Friedman-Hill:
I don't know what "don't participate in inheritance" means. It is indeed true that static methods are not polymorphic, and you can't override them and get polymorphic behavior. But static methods are inherited in the sense that they're available amongst the child class's methods; you can call a static method using the child class's name, or using an instance of the child class, or without qualification in the body of a child class.

Therefore, if you define another method with the same name, you must follow the rules that apply to method overloading. It's not legal to have two overloaded methods with the same name and argument types but different return types, and that's the problem you're seeing here.


Actually I believe they must follow the same rules as overriding which are specified in 8.4.8.3 of the JLS 3rd ed. With static methods instead of overriding an inherited method with the same signature it is said to hide the method. However, they must both follow certain rules one of which is that:


If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type substitutable for d2, or a compile-time error occurs. Furthermore, if R1 is not a subtype of R2, an unchecked warning must be issued (unless suppressed (�9.6.1.5)).


That's what the compiler is complaining about.

Originally posted by Naseem Khan:
If you can access any member of superclass by its simple name in subclass

without prefixing super, then that member is considered to be inherited.

This is the simple test.


No. A static method may be hidden by another static method in which case while the method is still inherited it would not be accessible by it's simple name.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Originally posted by Ken Blair:

A static method may be hidden by another static method in which case while the method is still inherited it would not be accessible by it's simple name.



Once super class static method gets hidden as you redefined the same method in subclass, then how super class static method is inherited.

Since superclass version is hidden not visible is subclass, so you can't call super class version by its simple name in subclass and thus not inherited.

private, hidden, overriden members of super class are not accesible by its simple name in sub class, WHY? because they are not inherited in subclass.

Regards

Naseem
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Quote from Khalid Mughal Book A Programmer's Guide to JAVA Certification: Page No. 266

If a superclass member is accessible by its simple name in the subclass (without the use of any extra syntax like super), then that member is considered inherited. This means that private, overridden and hidden members of the superclass are not inherited.



Regards

Naseem
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yes, I was wrong. However, the author's logic is backwards. What members are inherited is defined clearly in the JLS. Private, overridden and hidden methods are not inherited by definition and a consequence of this is that any member which cannot be accessed by it's simple name is one that was not inherited.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Yeah you can think this way also.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method in Inheritance