Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hide vs override

 
Emma Peel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Sun's Java Tutorial, it says: "A subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass".
So what is the difference between overriding a method and hiding it?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Emma Peel:
In the Sun's Java Tutorial, it says: "A subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass".
So what is the difference between overriding a method and hiding it?

Static methods are not polymorphic, so there is no runtime consideration of which method to use. For example:

Super.java


Sub.java

Here's the output:
 
Emma Peel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your example. I downloaded it, ran it and got the results as stated. Then I removed the "static" from the declaration of the 2 "staticMethod"s. The #1 examples had the same results. The #2 example got the opposite results. The #3 example I commented out.
From that I have inferred that syntactically there is no difference between hiding & overriding a method. But, the results are different based on whether the method is a static one or not.
What I see is that once a method has been overriden by a sub-class, there is no way to call the super's overridden method from a sub object. The only way is to instantiate a Super Object and call it through that instantiation.
If I've got that right, I'm not sure why that is so.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From that I have inferred that syntactically there is no difference between hiding & overriding a method. But, the results are different based on whether the method is a static one or not.
I'd say that was a fair assessment.
What I see is that once a method has been overriden by a sub-class, there is no way to call the super's overridden method from a sub object. The only way is to instantiate a Super Object and call it through that instantiation.
That's more or less right in regards to instance methods. The sub-class itself can call super.method() though, assuming that the subclass has suffiecient access to method() (public, protected).
If I've got that right, I'm not sure why that is so.
What part are you confused about? The fact that instance methods are polymorphic and static methods are not? The reason that instance methods must be selected at runtime based on the most specific class is because you can have arrays or Collections with references to any parent class in the hierarchy. Consider this:

Because of the polymorphic behavior of Java, you can rest assured that the correct toString() method will be selected without casting to the specific class.
 
Emma Peel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification.
The only thing I couldn't get to work is using the "super" reference to call the staticMethod (after I removed the static designation from both staticMethods in your original example).
I removed the instantiation for the "sup" type Super Class object, and I inserted "super.staticMethod(3);" after the"((Super) sub).staticMethod(2);" statement,and I got a the following compile error: "Sub.java:16: non-static variable super cannot be referenced from a static context".
The Sun Tutorial say "super.overriddenMethodName();" is how to do it. Do you know what I am doing wrong?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Emma Peel:
Thanks for the clarification.
The only thing I couldn't get to work is using the "super" reference to call the staticMethod (after I removed the static designation from both staticMethods in your original example).
I removed the instantiation for the "sup" type Super Class object, and I inserted "super.staticMethod(3);" after the"((Super) sub).staticMethod(2);" statement,and I got a the following compile error: "Sub.java:16: non-static variable super cannot be referenced from a static context".
The Sun Tutorial say "super.overriddenMethodName();" is how to do it. Do you know what I am doing wrong?

super can't be referenced from a static context because it requires a particular instance. The compiler has no way of knowing which instance to use. Remember that static methods can be hidden but not overridden, so when the tutorial refers to super.overriddenMethodName() it is in reference to an instance method not a class method. If you had an instance method in Sub you could call super. Modifying Sub.java:

Here's the results:
 
Emma Peel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I understand what you were saying. To make sure, I set up the following scenario:
public class Super {
public Super(){
System.out.println( "Super Constructor Called" );
}

public void instanceMethod(int i){
System.out.println("Super: " + i);
}
}
public class Sub extends Super {
public void instanceMethod(int i) {
super.instanceMethod(5);
System.out.println("Sub: " + i);
}
public static void main(String[] args) {
Sub sub = new Sub();
System.out.println("Call thru instances");
sub.instanceMethod(1);
System.out.println("");
System.out.println("Cast Sub to Super and call (Note no polymorhism)");
((Super) sub).instanceMethod(2);
}
}

When I ran this I got:
Super Constructor Called
Call thru instances
Super: 5
Sub: 1
Cast Sub to Super and call (Note no polymorhism)
Super: 5
Sub: 2

Then to determine how and when the Super-Class constructor was called, I changed the Sub class to the following:

public class Sub extends Super {
public static void main(String[] args) {
Sub sub = new Sub();
System.out.println("Call thru instances");
}
}


with the following results:
Super Class Called
Call thru instances

What I don't understand is:
If instantiating a sub-class object always automatically causes instantiation of the super-class object, why can't I reference the super-class object using the the super key word from the main method?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If instantiating a sub-class object always automatically causes instantiation of the super-class object, why can't I reference the super-class object using the the super key word from the main method?
Because main is static. It can run whether or not an instance of its enclosing class exists. In order to access the special references super and this you must have an instantiated instance. Remeber static methods are classwide, in other words they are stateless. Instances of classes usually have unique state, so if I request an instance reference from a static method how can the compiler possibly know which instance to use or if there is an instance? Consider this:
 
Emma Peel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your example. I thought a static method could not access an instance variable, though the reverse can occur. I thought that class-member variables could either be static or instance variables. Therefore, no static variable could have the same name as an instance variable in the same class.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Emma Peel:
I don't understand your example. I thought a static method could not access an instance variable, though the reverse can occur. I thought that class-member variables could either be static or instance variables. Therefore, no static variable could have the same name as an instance variable in the same class.

Look at the comments. The code will not compile for the very reason you stated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic