Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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
Hi to all...
I have a doubt in static method inheritance....
Static methods as we all know, cannot be overridden but none the less they are inherited as can be confirmed by the code below


The above code calls the static method vaib using the class name Main hence implying that it has been inherited.

And now please look at the code below.


The above code produces output as "Bar" implying re-definition .
My question is if static methods are inherited then why they cannot be overridden?
One can definately overload a Static method but why not override it ?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding does mean you can avail ploymorphism, that is run time method
selection on behalf of object type the reference type is referencing to.
Static methods are for class and they are not specific to objects. You
don't need object ref to access static member.


If you think one has Car(abstract class) ref array, holding reference of
various cars as Honda, TVS, Bajaj etc. All the classes that extend from the
Car override the display method for example.

When you access the overridden method using Car ref, at run time your
particular overridden method is called. That is the flexibility plolymorphism provides. In future you need to add more cars, you can, just
extend from Car, override the display() method, put the object ref to one
element of Car ref array.

Static methods are not meant for providing polymprphism. There selection
is done at compile time on behalf of what parameter you pass to that
method and compiler decides which overloaded version of the method to
call. There is not run time trouble in this case.


Thanks,
 
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
Hello chandra.....
You still did'nt get my point...
My point is if static methods are inherited then why can'nt they be overridden.
Static methods as we all know are class specific and not object specific but still they are inherited why what's the purpose of inheriting static methods then ?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Main2
{
static String vaib()
{
return "Foo";
}
}
class Main extends Main2 {
static String vaib()
{
return "bar";
}

public static void main(String [] args)
{
Main2 m=new Main();
Main d=new Main();
System.out.println(m.vaib());------->inside complier System.out.println(Main2.vaib());
System.out.println(d.vaib());-------->inside complier System.out.println(Main.vaib());
}
}
As static mathods are independent of reference variable they are considered for classes only so they will not undergo runtime polymorphism even though you call methods with reference varibles for objects ex:m.vaib() they are replaced inside constructor by class name they refere like Main2.vaib();
 
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
Well...i already knew that rajesh...but the point is why inheritance is allowed for static methods if it does'nt add any new functionality or code convinience.. ?
 
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
Could any one please answer my question??
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhasker,

You can think in a way, "why do I need static method overriding?". I tried
to elaborated the things in context of compile time and run time method
selection.

If the static methods have not be selected at run time, what is the role of
overriding the method. So far as re implementing the static method is
concerned, it is provided to us so that we could avail the same name of the
method as in our parent class. If you see the re-implementation of the
static method, it looks like you are overriding it but NOT so. When you
use parent class reference referring to child class object and think
that the re-implemented static method (that looks like overridden but
not), would be called at run time, but it is not. It is is called on the
basis of ref type that is of parent class
So what was that, overriding
or re-implementation. If it was overriding then child class static method
would have been called but not so.


In a line I can say, overriding is there to avail something that has to be
known at run time. Runtime polymorphism in other words.

Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhaskar wrote


...but the point is why inheritance is allowed for static methods if it does'nt add any new functionality or code convinience.. ?




Same name convenience that you get. Method logic you can change, availing the
same method name. Isn't it the flexibility you are provided with.


Thanks,
[ June 07, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bhaskar,

I think that the answer is that static methods become available when a class is loaded and subclasses may not even be loaded yet so methods of subclasses may not be available to be called yet.

On the other hand, instance methods, cannot be run until all of the constructors at every level are called. Every class along the way must have a constructor called or the class cannot be instantiated. So, for instance methods, not only have all of the instance variables have been initialized, but all of the superclasses have been loaded as well.

Maybe this is why every class must have a constructor and why Java supplies default constructors. Maybe this is done for the side-effect of loading the class being instantiated and loading *every* superclass.

Kaydell
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhaskar and Chandra

It's still confusing. I mean though after override / re-implement static method in subclass, using reference type of superclass and object of subclass, can't really invoke the static method of subclass, then what is the use of override/ re-implement the static methods.

I think the only use is, it's class specific / similar to constructor. so whenever any class extends it gets that class specific behavior, that's all.


public class Chap5 {

static String display(){

return "Hello";
}
}

class runChap5 extends Chap5 {

static String display(){
String c = "How you doing";
String s = c + Chap5.display();
return s;
}

public static void main(String []args)
{
Chap5 c = new runChap5();

System.out.println(""+c.display());

}
}

-- Waiting for inputs,
- bhaskar and chandra please correct me wherever i did wrong or misconcept,
- thank you ranchers !!
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chintan,

You didn't get the soul of story!

The term overriding and reimplementation are completely different things.

1- You can't override the static method but you can re-implement it.
2- You can override the non-static method provided it is not final.



Line #1: The output you see because static methods can't be overridden.
Static method selection happened on behalf of type of "a1" that is Animal
so Animal version of meth() is called. This is called compile time method
selection.


Line #2: Availing POLYMORPHISM, because you have overridden the display()
method of Animal. The Dog version of display() has been called and this
method selection happened at run time on behalf of object type a1 is
referring to that is Dog.


So overriding and re-implementing are two completely different things,
don't mix them.


Re-implementing term is used in context of static method when you re-define
the same method in the sub-class. No BENEFIT of re-defining what to saw in
Line #2, that is calling Dog class display() using Animal ref variable.

The only benefit of re-defining/re-implementing is that you are availing
the same name of the static method that you have in Animal class and
you can change the code inside the re-defined method that we did. In
re-defined method we wrote "Dog meth()".




Thanks,
[ June 07, 2007: Message edited by: Chandra Bhatt ]
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Chndra,

Thank you for your great explanation !, As we know , there is no problem in overriding the non-static method. so when sub-class overrides the method, we can use that method provided super class has that method, using subclass object and super class reference. I am not explaining other rules for that, but this is the most obvious to me. right.

now the main thing i can see in this thread is that, STATIC method in super class is like a glue with SUPER CLASS (class member as constructor), so anyway overriding STATIC METHOD is not gonna have invoccation using the subclass object and SUPER reference type unless you use sublclass reference and subclass object. so i dont know what to avail ?

Please pardon for verbose explanation, i hope you will be able to understand what i am trying to explain.

Thank you once again in clearing very fundamental doubt
- Chintan Ramavat
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see, you are correct!


Thanks,
 
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
One more point....even the explicit reference cast will call the Static method of the reference type i.e when m1 is explicitly casted to point to object of Main it stills accesses the static method vaib of it's own class ..
It can be verified by the following code:


Output is
Bar
Foo
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhaskar,

static methods can be overridden in its subclass. But they can not be overridden as non static.i.e, The overriding method must also be static.

As static members are associated with the class, if you call with the superclass reference variables(whatever the assigned object) superclass static method is executed.

please check the following program:

class Base
{
static void simple()
{ System.out.println("Base class");
}
}

class Derived extends Base
{
static void simple()
{ System.out.println("Derived class");
}
}

class MainClass
{
public static void main(String s[])
{
Base b=new Derived();
b.simple(); // here only super class simple() method is called because static is attached to class
(new Derived()).simple(); // now overridden method simple is executed
}
}
 
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
The point stated by you is slightly off the hook...
Static methods can be overloaded in the subclass or a class but burn this point in your mind that static methods can never ever be overidden.
Static method overload can also happen in a base class derived class situation
for example
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bhaskar sir:
One more point....even the explicit reference cast will call the Static method of the reference type i.e when m1 is explicitly casted to point to object of Main it stills accesses the static method vaib of it's own class ..



Hi bhaskar,

Please look carefully at this fragment of your posted code:


Main2 m1 = (Main)me; // where me is Main2

Althought you are casting to Main, the variable that gets that reference is still of type Main2. So when you call m1.vaib() you are saying Main2.vaib() and not Main.vaib(). When you are at that line, the cast is history.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic