43

here is working java code

class Cup {
 public String sayColor() {
 return "i have a color .";
 }
}
class TCup extends Cup{
 public String sayColor(){
 System.out.println(super.getClass().getName());
 return super.sayColor()+"color is tee green.";
 }
}
class MyTCup extends TCup {
 public String sayColor(){
 System.out.println(super.getClass().getName());
 return super.sayColor()+"but brushed to red now!";
 }
}
class Test {
 public static void main(String[] args) {
 Cup c = new MyTCup();
 System.out.print(c.sayColor());
 }
}

and running the Test class prints

MyTCup
MyTCup
i have a color .color is tee green.but brushed to red now!

question 1: At the runtime, the type of object C is MyTCup, but it can always call the super method. Is there a method stack in the memory within MyTCup after initializing the object, and then can call through at runtime like the code ?

question 2: There is no way to call the super method in other objects. As I know ,c++ can cast to call parent method at any time. Why is it different in Java?

asked Jun 23, 2009 at 14:20
1
  • 1
    You should probably accept the answer once you are satisfied with it :) Commented Aug 26, 2013 at 11:06

4 Answers 4

84

You can't call the super method in other objects - that would violate encapsulation. The whole point is that the object controls what its overridden methods do. For instance, you might override a collection's add method to throw an exception in certain circumstances, so it could ensure only "valid" items got added to the collection. That would be pointless if callers could just bypass it with a cast!

The only reason an object gets to call super.foo() for itself is to enable one call to be implemented by using the parent implementation. It's up to the code in the class to make sure it only ever does that sensibly. Again, to take the add-in-a-collection example, if the collection overrides add it would have to have some way of adding the validated item to the collection, which it would do with super.add().

Note that for the same reason of encapuslation, you can only call your parent implementation, not the grandparent implementation - so super.foo() is valid, but super.super.foo() isn't.

answered Jun 23, 2009 at 14:28
Sign up to request clarification or add additional context in comments.

2 Comments

See also stackoverflow.com/questions/586363/… (which you answered very nicely).
And look at that... I used the same example :)
1

1:

Your question is not quite clear. What do you mean by "call through at runtime like the code"? If you are asking how instance c knows what its super class is, then yes, the class hierarchy is stored in memory, and can be accessed by the VM.

2:

Java actually does allow you to cast an instance to its parent. It's just that calling a method on an instance always uses the instance's actual class, not its compile-time class. I.e. in Java all methods are what would be called "virtual" in C++. Why this was decided I do not know.

Edit: Actually Jon Skeet explains very nicely why you cannot call a method of a super class on an instance of a sub class, so now I know :-).

answered Jun 23, 2009 at 14:31

1 Comment

i mean "sayColor" method called from MyTCup to TCup and Cup and the class displayed is "MyTCup".so the methods are belonged to MyTCup or belonged TCup,Cup.I dont familiar with jvm,maybe all in the method area.
1

You can define a new helper method in the subclass so that you don't override the original--call that--it can call the super method or not, depending on what you want.

answered Jul 18, 2011 at 18:24

Comments

0

Actually you can, but you have to use a methodology, so it will only work on your code.
At the super class, add the parameter "Class" to the method, like in:

public String sayColor(Class classFilter){...

now, at every override, you check to see if the current sub-class is the one of the specified filter like:

if(TCup.class!=classFilter)return super.sayColor(classFilter);
return "some text for TCup only";

I coded in a exclusive way. You can add more parameters, or compare with null, so you can join results with the super classes, use your creativity :)

answered Sep 25, 2016 at 22:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.