简体   繁体   中英

How to call subclass method (Java)

I'm fairly new to Java. I was working on an assignment where I have a superclass and several subclasses, where some subclasses have attributes that the others don't. Essentially, I have to make an array of SuperClass objects, and then initialize each element of the array with one of the subclasses.

However, when I try to use one of the subclasses' specific methods, I get a compiling error telling me that the method .getY() wasn't found in the SuperClass , when it is clearly defined as a method from SuperClass . I boiled down the problem to the following code (making sure I still get the same error):

public class StackOverFlow {
public static void main(String[] args) {
    /*
    SubClass obj = new SubClass(4, 5); 
    System.out.println(obj.getX()); 
    System.out.println(obj.getY()); 
    */
    SuperClass[] arr; 
    arr = new SuperClass[5]; 
    arr[0] = new SubClass(5, 10); 
    System.out.println(arr[0].getX());  
    System.out.println(arr[0].getY()); 
}}

public class SuperClass {
private int x; 
public SuperClass(int x){
    this.x = x; 
}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}}


public class SubClass extends SuperClass{
private int y; 
public SubClass(int x, int y){
    super(x); 
    this.y = y; 
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}}

When I use the code that's commented I don't get an error, which probably means the error comes from incorrectly using the array of classes. Can anybody help me out?

Your declared array arr is an array of type (class) SuperClass objects. Everything in the array must be a thing that is a SuperClass .

Your subclass named SubClass IS a thing of type SuperClass — it is an extension of that class, by adding y and getY , but it still conforms to SuperClass . Anything that you can do with SuperClass you can also do with SubClass, so you are allowed to add SubClass objects to the array of type SuperClass .

Where your error is, at arr[0].getY() , the thing that is in the spot at arr[0] is, in fact, a SubClass, because you just put it there, but there is no guarantee that that is the case — the only guarantee is that things in the array are of the type SuperClass , and things of type SuperClass don't have a getY() method.

When you know that a thing is of type SubClass you can cast it, like: (SubClass) arr[0] , and must parenthesize it to apply the cast before you attach the method call, so it would be ((SubClass) arr[0]).getY()
Casting has it's own potential errors, if you try to cast something to SubClass when that thing isn't actually a SubClass it will cause an error (but that is another discussion).

So the answer to the question "How to call subclass method" is to cast the object to the subclass:

// Direct replacement for your line of code:
System.out.println( ((SubClass) arr[0]).getY() );

// or assign it to another variable first:
SubClass sc = (SubClass) arr[0];
System.out.println( sc.getY() );

If you want to call a method on SubClass but your array is of type SuperClass[] , you must cast the object:

SuperClass[] arr; 
arr = new SuperClass[5]; 
arr[0] = new SubClass(5, 10); 

SuperClass elem = arr[0];
SubClass subClassElem = (SubClass) elem; // here's the cast
subClassElem.getY();

// or shorter:
((SubClass) arr[0]).getY();

If you're unsure what's in the array use instanceof :

SuperClass elem = arr[0];
if(elem instanceof SubClass) {
  SubClass subClassElem = (SubClass) elem;
  subClassElem.getY();
}

The method getY() is not defined in SuperClass and as your arrays is of type SuperClass it it don't recognize it. You can cast the array object to SubClass to specify the type:

        System.out.println(((SubClass) arr[0]).getY());

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