Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

static method in Inheritance

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah you can think this way also.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic