File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static  methods and instance variables are not overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static  methods and instance variables are not overridden" Watch "Static  methods and instance variables are not overridden" New topic
Author

Static methods and instance variables are not overridden

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 172
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
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
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


Lovin' java
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
To add to what Ankit said.......... you can access the superclass static method from the static method though, using the class name itself


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

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.


A.A.Anbarasu
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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

Joined: Jun 02, 2009
Posts: 182

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

Joined: Sep 03, 2007
Posts: 213
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

Joined: Jun 02, 2009
Posts: 182

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

Joined: Sep 03, 2007
Posts: 213
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.
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83

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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Feb 16, 2009
Posts: 83
Try the work around trick below and see if it is overriden or not:


Cheers!!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Feb 16, 2009
Posts: 83

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

Joined: Jun 28, 2009
Posts: 172
This typecast thing was a real great learning
Dhrubajyoti Chatterjee
Greenhorn

Joined: Oct 18, 2008
Posts: 2
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.
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 145

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).

Adolfo Eloy
Software Developer
OCPJP 6
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

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 .

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Mandy Singh
Greenhorn

Joined: May 30, 2011
Posts: 23

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.



*** If opportunity doesn't knock, build a door !! ***
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods and instance variables are not overridden
 
Similar Threads
static methods
static methods can't be overidden why
Can a main method be inherited?
Is inheritance is necessary to override?
fun with main() and abstract