简体   繁体   中英

Java: Calling a subclass method from superclass variable

(Question previously asked here . I did not quite get the desired answer.)

A quote from a book:

What if you want to call a method that's defined by a subclass from an object that's referenced by a variable of the superclass? Suppose that the SoftBall class has a method named riseBall that isn't defined by the Ball class. How can you call it from a Ball variable? One way to do that is to create a variable of the sub- class and then use an assignment statement to cast the object:

Ball b = new SoftBall();
SoftBall s = (SoftBall)b;
// cast the Ball to a
// SoftBall
s.riseBall();

In the code snippet above, it shows a new Softball object being created and assigned as a reference to the variable b , which is completely legal since the class Softball is a subclass of the class Ball . Yet the quote states (indirectly) that you have to cast the variable to type Softball before you can use the variable to call methods from the Softball class. Why is that? Why can't I directly use the variable b of type Ball (which contains the reference to the Softball object) to call the desired method? The variable b already has the object.

(Note: I already read this post.)

Java is a statically typed language.

That means that the compiler checks if the type of the variable has the method you are trying to call.

The type of your variable b is Ball . Ball does not have a riseBall method.

That means your code would crash unless that b at runtime happens to contain a Softball (which the compiler cannot guarantee). You may know that it does, but you have to convince the compiler, too (ie give your variables the necessary types).

Ball b = new SoftBall();

This just means that variable b is of type Ball, but if the methods present in class Ball if are overridden in extended class SoftBall, then using variable 'b', we shall be invoking the method definition of class 'SoftBall' rather than 'Ball'. Now, although variable b refers to the implementation of class 'SoftBall', it still is of type class 'Ball' and class Ball does not have any method 'riseBall'

Hope that makes sense.

Because the methods that are exposed depends on the declared type of the variable.

Ball b = new SoftBall();

b is actually a SoftBall but you are "hinding" it because you declared it as a Ball type. Since Ball type doesn't have a riseBall method you cannot access it by asking b .

As we know java is a statically typed language .

This is a feature of statically-typed languages where variables are assigned a type ahead of time, and checked at compile-time to see that the types match.

If this code were performed on a dynamically-typed language , where the types are checked at `runtime, something like the following could be allowed:

Ball b = new SoftBall();
if (b instanceof SoftBall){
  b.riseBall();
}

b.riseBall() is guaranteed only to execute when the instance is a SoftBall , so the call to riseBall will always work.

However, Java is a statically-typed language, so this type of code is not allowed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM