This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Static methods and instance variables are not overridden

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

As we know only instance methods can be overridden, so why can we call super.x but not super.getRating() where x is an instance variable in super class and getRating() is a static method in superclass
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static members (variables and methods) belong to the class and not to an instance of the class, although an instance of the class can access the static member. HOwever, static members can't access instance members in a direct way.

And static members are NOT inherited so therefore, they CAN'T be overriden. Overriden applies only to those inherited methods
 
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not able to understand what you mean



You can access super class static methods using super keyword. This is only not allowed in static methods. This is because super and this are available only in non-static methods...
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what Ankit said.......... you can access the superclass static method from the static method though, using the class name itself
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:I'm not able to understand what you mean



You can access super class static methods using super keyword. This is only not allowed in static methods. This is because super and this are available only in non-static methods...



Both 'this' and 'super' used with instances NOT WITH STATIC. So we can not use 'super' keyword to access a static method. We can use class name to access STATIC method.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anbarasu aladiyan wrote:Both 'this' and 'super' used with instances NOT WITH STATIC. So we can not use 'super' keyword to access a static method. We can use class name to access STATIC method.


That's not right. We can use super to access a static method.
From K&B

Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.

When super.method() is called, the compiler gets the SuperClass and invokes the method.
That's what the code snippet from Ankit proves. Its not good practice to call a static method that way, but it is surely legal though.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Adat wrote:

anbarasu aladiyan wrote:Both 'this' and 'super' used with instances NOT WITH STATIC. So we can not use 'super' keyword to access a static method. We can use class name to access STATIC method.


That's not right. We can use super to access a static method.
From K&B

Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.

When super.method() is called, the compiler gets the SuperClass and invokes the method.
That's what the code snippet from Ankit proves. Its not good practice to call a static method that way, but it is surely legal though.




Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.



this means we can use object reference of the class to access its static method. IT does not means we can use super keyword in static method. Always 'super' and 'this' must not be used from static method
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anbarasu aladiyan wrote:this means we can use object reference of the class to access its static method.


Hence we can use super from a non-static method to call a superclass static method

anbarasu aladiyan wrote:IT does not means we can use super keyword in static method. Always 'super' and 'this' must not be used from static method

That's exactly what we are talking about, and Ankit also mentioned, that we cannot use super and this from a static method.
I am not saying that you can call super or this from a static method. What I tried to tell was that if you want to call a static method from subclass static method you have to use the super class name 'Super' (watch the case) in the example to invoke the static method or you can also call the method directly since subclasses have access to Superclass static methods.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Adat wrote:

anbarasu aladiyan wrote:this means we can use object reference of the class to access its static method.


Hence we can use super from a non-static method to call a superclass static method

anbarasu aladiyan wrote:IT does not means we can use super keyword in static method. Always 'super' and 'this' must not be used from static method

That's exactly what we are talking about, and Ankit also mentioned, that we cannot use super and this from a static method.
I am not saying that you can call super or this from a static method. What I tried to tell was that if you want to call a static method from subclass static method you have to use the super class name 'Super' (watch the case) in the example to invoke the static method or you can also call the method directly since subclasses have access to Superclass static methods.



Hi

Could you please go through your message posted on today (07-07-09 ) at 4:07:52 PM
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anbarasu aladiyan wrote:Could you please go through your message posted on today (07-07-09 ) at 4:07:52 PM


Whatever I said in that message was about accessing a static method in a super class from a non static method in the sub class.
Hope this clarifies any doubt. If still you think I am wrong, we may ask someone else to clear the doubt. I still think what I said was right.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ranch Hand

Joined: Sep 25, 2007
Messages: 41


[Post New]posted Yesterday 11:26:40 PM private message
Quote [Up]

And static members are NOT inherited so therefore, they CAN'T be overriden. Overriden applies only to those inherited methods



This message is for Joshua Ebarvia , please look at the code below to see if static members including methods can be inherited or not:



The call to the method TopMethod() was made by the subclass name and still got the result which means that the subclass BottomClass has his own copy inherited from the top parent class. I never heard or read anywhere something that says static members are not inherited, there are inherited like any other members.

Cheers!!
 
Ankit Garg
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static members are inherited but they cannot be overridden. I look at it as call to static methods are not polymorphic...
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the work around trick below and see if it is overriden or not:


Cheers!!
 
Ankit Garg
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mo, you've introduced a type cast when you called TopMethod. This is not the correct way to test whether TopMethod was overridden or not. remove the cast and you'll see that the call to TopMethod is not polymorphic meaning that TopMethod was not overridden...
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know that it is a type cast, that's why I said a work around trick the issue.
I know that static methods cannot be overridden as it is clearly stated in Kathy and Bert book for Java 6.

Cheers!!!
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This typecast thing was a real great learning
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that static method cannot be overridden in subclass but can you please explain the following code;


This produces the following compilation error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot override the final method from Base

at Derived.method(test.java:12)
at test.main(test.java:21)

Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.
 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhrubajyoti Chatterjee wrote:I agree that static method cannot be overridden in subclass but can you please explain the following code;


This produces the following compilation error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot override the final method from Base

at Derived.method(test.java:12)
at test.main(test.java:21)

Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.



I think you point a good question!!!
It seems to be a big controversial assertion in java.
Maybe you can create another post Dhrubajyoti. (only my opinion).
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhrubajyoti Chatterjee wrote:
Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.


Final method cannot be overridden so it is a compiler error.
And static method is overshadowed not overridden if you remove the final keyword.
To call a specific static method you can use classname.methodname .
 
Greenhorn
Posts: 23
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dhrubajyoti Chatterjee wrote:I agree that static method cannot be overridden in subclass but can you please explain the following code;


This produces the following compilation error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot override the final method from Base

at Derived.method(test.java:12)
at test.main(test.java:21)

Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.



Buddy,
You need to understand the difference and similarities between two things: - Hiding and Overriding.
In case of Overriding, polymorphism can be used.
In case of Hiding, its not !!

Apart from the issue of Polymorphism, rest everything is same in "hiding" and "overriding" in the sense that both need the same signature, compatible return types, etc etc etc.. i.e. the rules of overriding are applicable even when hiding a method.

If you don't use the final keyword in the Base class method, you will be "hiding" the method by re-declaring it in the sub class. Hiding because you know that static methods cannot be overridden. So any re-declaration in the sub class would mean that you are "hiding" the method declared in the super class (not overriding it).

Now since the rules of overriding are applicable while "hiding", you cannot hide/override a final method.

In case of "private" methods, the sub class would not know about them as they are not inherited. So the sub class is free to re-declare it.

If you replace the "final" keyword with "private", or declare your method as private final your code will work fine.

In case of final (non-private) methods, the sub class "knows" that such a method exists but it is NOT ALLOWED to change the method's functionality.


 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic