• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static variables and methods..

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

my question is would x and method in class A be inherited by B?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first thing id that your code will not compile because of no return type.

and the second thing is that static method and variable will not be inherited but that will be overhide.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you compiled this example ?
Methods should have return type.

I think static methods can be inherited. Please check the link given below .

https://coderanch.com/t/411599/java/java/static-constructor
[ September 25, 2008: Message edited by: bittoo garg ]
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sun page
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can static variables be inherited?
Think of it - Static variables are class variables. They belong to class and they have exactly one copy. Each instance created shares that copy. So if it was inherited then this rule would be violated isn't it?
As far as inheriting static methods are concerned, yes they are inherited, but they wont perform the way you think when you try polymorphism. Actually they are hidden.
To learn more, goto SCJP FAQ, and there is a very nice article on this.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


http://java.sun.com/docs/books/tutorial/java/IandI/override.html



In the above link .. nothing has been written about inheritence with regard to static methods . This link just explain the difference between overriding and overhiding.



output is "Hi I am static method in parent class"
So I think static methods can be inherited.
 
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,

static members can partcipate in inheritance. But static method cannot be ovverriden. But the rules for overriding a method and hiding a method is same.

Please let me know if my understanding is wrong?

Have a nice day ahead!

Regards,
Gopal
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Please loot at SCJP FAQ.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul

The link you gave again describe the difference between overriding and overhiding.

The example which I posted in my last post is am example of inheritence.
where subclass object is invoking display function of parent class which is static.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)

But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:

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

Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)



I am not talking about overriding.
overriding involves two classes parent and child class .
In my example in my post display method is a static method in parent class only. There is no display method in child class .So no question arises of overriding or overhiding.



But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:


code:
--------------------------------------------------------------------------------

class parent{public static void display(){System.out.println("Hi I am static method in parent class");}}public class subclass extends parent{public void display (){System.out.println ("In subclass");}public static void main(String args[]){subclass s = new subclass();s.display();}}

--------------------------------------------------------------------------------



Yes, You are very true. But this is not my point.

I am just opposing the point that


the second thing is that static method and variable will not be inherited but that will be overhide



I am again posting my example which is not talking about overriding.

Question here is .. can static method be inherited?
I am saying yes. Avobe example clearly shows this.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a devil of a time with this one. I have not found Chapter and Verse quoted anywhere in Java documentation that flat out says that static methods are not inherited. What gives me grief proving it is that there are two ways to call a static method a) by class name:

( ClassName.statMethod() ),

or b) using a reference to an object

( objReference.statMethod() ).

Both are legal but the second makes it look like the subclass object inherited the method and it is being called via inheritance.

I'm still looking....
[ September 25, 2008: Message edited by: Bob Ruth ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Bob has been very clear in putting out this point...
"....but the second makes it look like the subclass object inherited the method and it is being called via inheritance"

So, in Bittoo's original code, the function display() is called using the subclass object, which is very similar to calling the function as parent.display () -- which means its NOT inherited. Its just calling the static method, this time, using a reference which IS-A parent (as opposed to calling the static method using its class)
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

See this code also.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My contribution:


[ September 25, 2008: Message edited by: Rudolf Meerkotter ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic