Jim Vekemans
Jim Vekemans

Reputation: 51

Can I get subclass variables from array of superclass objects?

First of all, thanks for reading!

I made a class "Sportsman" that is the Superclass of "Footballer". So I made an array of Sportsman-objects that also contains Footballer objects, no problems here (I have a pretty good idea of how inheritance works).

I can set Footballer-specific variables to the objects in the array, but when I want to print the variables I've just declared to the object i can't call get-methods because the array is a Sportsman-array and not a Footballer-array.

So here is my question: How do i print the Footballer specific variables from a Sportsman Superclass array?

Things to know: I can't make a separate array for the subclass objects. They must be mixed! While putting a subclass object in the arrays of superclass objects, I explicitly make it a subclass object. However, I'm not able to use subclass methods on it.

main code:

public class SportApp {
public static void main(String[] args) 
{
Scanner input = new Scanner(System.in);
Sportsman[] sportArr = new Sportsman[10];
for(int count=0 ; count < sportArr.length ; count++)
{   System.out.println("Is the sportsman a footballer?");
    String answer = input.nextLine();
    System.out.println("Last name?");
    String lastName = input.nextLine();
    System.out.println("name?");
    String name = input.nextLine();
    switch (answer){
    case "yes":     System.out.println("Which club does he play in?");
                String club = input.nextLine();
                System.out.println("At what position?");
                String pos = invoer.nextLine();
                sportArr[count]=new Footballer(lastName,name,club,pos);
                break;
    default:    System.out.println("What sport?");
                String sport = input.nextLine();
                sportArr[count]=new Sportsman(lastName,name,sport);
    }
}

System.out.println("All sportsmen that don't play football:");
for(int count=0 ; count < sportArr.length ; count++)
{   if(!(sportArr[count] instanceof Footballer))
    {   System.out.print("name: ");
        sportArr[count].print();}   }

System.out.println("All football players sorted by position:");
//Same as previous print, but with added player position and club!
for(int count=0 ; count < sportArr.length ; count++)
{   if(sportArr[count] instanceof Footballer)
    {
    /*what I've tried:
    *System.out.println("front players:");
    *if(sportArr[count].getPos()=="front")      //the .getPos doesn't work because it wants to invoke it on a Sportsman where getPos doesn't exist
    *{  sportArr[count].print();}       //as the problem above, it doesn't see the object is also a Footballer so it does the Sportsman print()
    *
    *I wanted to do a regular sportArr[count].pos to print the Position but even now it doesn't recognise the object as Footballer, so I can't see pos.
    */
    }
}}}

Upvotes: 1

Views: 2038

Answers (1)

nickb
nickb

Reputation: 59699

You've done a type check with instanceof in the loop, and if it succeeds, you know that you have a Footballer. So, now you have to cast the object to get the correct type:

if(sportArr[count] instanceof Footballer)
{
    Footballer fb = (Footballer) sportArr[count];

    // Now this should work (note the use of fb, and not using `==` with string literals):
    if(fb.getPos().equals("front")) { 
        // etc..
    }
}

Upvotes: 2

Related Questions