• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Static Method Inheritance ??

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anybody please comment on this code...it works fine and displays the output as "In Main2's Static" i do not understand why...
Has this anything to do with inheritance please advise...

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method display in Main2 is declared as public, so it is visible (accessible) anywhere. The Main class is a subclass of Main2 and has access to the display method, so yes it is inherited.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Irina for your reply...
But as far as i know that the static methods are not inherited as they are a part of the class but not a part of the any instance ? So don'nt you think it violates the definition of Static ?

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vaibhav Wahee:
...as far as i know that the static methods are not inherited as they are a part of the class but not a part of the any instance ? So don'nt you think it violates the definition of Static ?


When an instance is created, it uses its own class, as well as every class up the hierarchy tree. So in this case, an instance of Main is based on the Main class, the Main2 class, and the Object class.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks marc for your prompt reply. But i did'nt get your point. How can a static method which is specific to Class Main2 be called upon a reference of it's subclass Main. Does this mean that static methods are indeed inherited ?

 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyone please comment on this ?

 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Still waiting for an opinion....

 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already said, think about a class hierarchy. Everything that is accessible is inherited and static just means that you don't need an instance to use it.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your reply and clarification. I never knew that a static is not barred from inheritance ...But the K&B books fails to mention this fact.....
I mean i was not able to extract this info from their SCJP 1.5 book

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Vaibhav,

I wonder if you're mixing up inheritance and overriding? The reason I ask is because it IS true that static methods can't be overridden...

Bert
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This example above clearly states that a static method is indeed inherited but not overridden....but still i've found this to be true that many persons fail to recognise this fact that static methods are also inherited

 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vaibhav, in your last example the static methods are hidden. You still have access to them (see JLS 8.4.8.2), but by definition they are not inherited (you can't call them as Main.display()). Here are some quotes from JLS:
8.4.8 Inheritance, Overriding, and Hiding:

A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class.


8.4.8.2 Hiding (by Class Methods):

A hidden method can be accessed by using a qualified name or by using a method invocation expression (�15.12) that contains the keyword super or a cast to a superclass type.

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what Bert suggested above, maybe it would help to read this: Overriding vs. Hiding.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Bert mentioned there are two concepts here inheritance and overridding.

A subclass inherits the static methods/properties of a superclass but we cannot override a static method and make polymprphic calls to it.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between overriding a method and hiding a method?

Over-riding basically supports late binding . More clearly, which method will be called is decided at runtime.It's for non-static methods.
Hiding is for all other members (static methods , instance members, static members). It is based on the early binding . More clearly , the method or member to be called or used is decided during compile time.


* How can you tell / demonstrate a method is hidden or overridden?

class A { int i=5;
static String s="manish";
static void getClassMethod() {
System.out.println("Static method in Super class");
}

void getInstanceMethod() {
System.out.println("Instance method in Super class");
}
}

class B extends A
{
float i=5.5;
int s=16;
static void getClassMethod() {
System.out.println("Static method in Sub class");
}

void getInstanceMethod() {
System.out.println("Instance method in Sub class");
}
}


class C
{
public static void main(String s[])
{
B b=new b();
System .out.println("Sub class---------------");
System .out.println(b.i+"-------------------------"+b.x);
System .out.println("staic method result"+b.getClassMethod());
System .out.println("non-staic method result"+b.getInstanceMethod());


A a=new A();
System .out.println("Sub class---------------");
System .out.println(a.i+"-------------------------"+a.x);
System .out.println("staic method result"+a.getClassMethod());
System .out.println("staic method result"+a.getInstanceMethod());

a=b;
System .out.println("Closely watch results---------------");
System .out.println(a.i+"-------------------------"+a.x);
System .out.println("staic method result ="+a.getClassMethod());
System .out.println("non-static method result ="+a.getInstanceMethod());
}
}

Guess the ouput :
most people will guess:
Sub class---------------
5.5------------------------------16
staic method result =Static method in Sub class
non-static method result=Instance method in Sub class

Super Class---------------
5---------------------------------Manish
staic method result =Static method in Super class
non-static method result=Instance method in Super class

Closely watch results---------------
5.5------------------------------16
staic method result =Static method in Sub class
non-static method result=Instance method in Sub class

But the last o/p is wrong here hinding comes & relt will be like this
Closely watch results---------------
5------------------------------Manish //hidden data members
staic method result =Static method in Super class //hidden static method
non-static method result=Instance method in Sub class //over-ridden non-static method

5---------------------------------Manish
staic method result =Static method in Super class
non-static method result=Instance method in Super class
 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods cannot be overriden

if you write in Main class

public void display (){...}

you get a compiler error : display() in MainStaticInh cannot override display() in Main2; overridden method is static
public void display(){}

that shows static method is not exactly inherited. I think sttaic methods are internally called by their class name.

But if you write in class Main another method

public static void display(){...}

It works.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But...my core question remains....


ARE STATIC METHODS INHERITED OR NOT ....

My example states that they are ....if someone thinks otherwise please explain why ?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are a feature of a class, not of an object. As such, they can't be inherited or overridden, since those are object features.

So instead of saying that a static method is inherited, you could say that it is accessible (as long as it's public, and not hidden).

The OverridingVsHiding page Marc linked to explains this further.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if static methods cant be overridden why does this give a compilation error.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok , here's another one...


This code compiles fine...which again proves beyond doubt that static methods are indeed inherited. I've seen mixed reviews and off the line answers.

Do we still have a doubt that static methods are not inherited ?

Please give a direct answer....
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i meant was if you run the following command ...

Java Main2 ...

The program displays HI as the output..implying that the main method which is of course static is indeed inherited by the Main2 class....


Please Comment On This
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vaibhav,
static method can not be inherited rather it can be accessible in its subclass. it is a MEMBER of the superclass. here in the example it is 'public', so it is accessible to its subclass. it is like a MEMBER of the subclass, but actually it is coming from its superclass.
got it?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
if static methods cant be overridden why does this give a compilation error.


Because you're confusing overriding and hiding. Follow the link posted above named OverridingVsHiding for further explanation.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vaibhav Wahee:
This code compiles fine...which again proves beyond doubt that static methods are indeed inherited.


If that code proves anything beyond doubt, then you need to explain to us how it does that, because I don't see how it does so.
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
8.4.8 Inheritance, Overriding, and Hiding:

A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class.


[ April 13, 2008: Message edited by: Irina Goble ]
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic