1

I have to use Polymorphism and ArrayList in my assignment in java but i need help. I would really appreciate it if anyone could help me. I can't acces my subclass methods from ArrayLis.

So I have a superclass

    public class Table{
    private int TotalAmount;
    //setters and getters
    }

And I have two subclasses

    public class TableFidelity extends Table{
    private int Points;
    //setters and getters
    }


    public class TableCompany extends Table{
    private int NumberOfTimesServed;
    //setters and getters
    }

I have to use both my subclasses in the same ArrayList

    public static void main(String[] args){
    Table table=new Table();
    ArrayList<Table> Tables=new ArrayList<Table>();
    Table tablefidelity =new TableFidelity ();
    Table tablecompany =new TableCompany ();
    Tables.add(tablefidelity);
    Tables.add(tablecompany);
    System.out.println(Tables.get(0).getTotalAmount());
    //I can get the total Amount from the superclass
    System.out.println(Tables.get(0).getPoints());
    //but I can't do this
    System.out.println(Tables.get(1).NumberOfTimesServed());
    //or this 
    }

Netbeans dosen't show getPoints or NumberOfTimesServed when I press . it shows only the superclass methods.

8
  • 1
    That's because your compiler only knows that the list contains instances of Table; it can't know that there might be specific subclasses there, or that any particular element will be an element of a particular subclass. Commented Feb 19, 2016 at 15:28
  • 1
    That's because the compiler only knows that the elements in the list are of type Table. It doesn't know that element 0 is a TableFidelity etc. (and it shouldn't know that). Commented Feb 19, 2016 at 15:29
  • 1
    @AndyTurner ^^ gz on the almost identical first sentence ;) Commented Feb 19, 2016 at 15:29
  • 1
    To use methods only defined in the subclass, you have to cast the object to the subclass type : ((TableFidelity)(Tables.get(0)).getPoints()) Commented Feb 19, 2016 at 15:30
  • @Berger But the casted object will it have the same data as the previous object? Commented Feb 19, 2016 at 15:32

1 Answer 1

1

If what you want is for each of your Tables to print information about what it contains, you'd be better off defining a method on the Table class to do that. You can then override this in your subclasses to print the extra information produced by those classes.

At the simplest level, you'd have

class Table {
  ....
  public void printInfo() {
    System.out.println(totalAmount);
  }
}

and then

class TableFidelity extends Table {
  ....
  @Override
  public void printInfo() {
    super.printInfo();
    System.out.println(Points);
  }
}

and so on. This is polymorphism at work: the class that invokes printInfo() doesn't need to know whether it's invoking it on a Table or a TableFidelity or whatever. The method overriding deals with that.

In practice, you might not want to restrict it to going to standard output. So you could pass in something to tell it where you want the information to go.

Perhaps even better, you could just override toString() in Table and its subclasses, along the lines above (but constructing a String instead of printing it). But it depends on exactly how you're going to use the classes.

One more note: if this is an assignment, you're probably going to drop marks for bad style for your naming conventions. Make sure your instance fields have names that start with lower case letters, not upper case.

Sign up to request clarification or add additional context in comments.

7 Comments

@chiastic-security I have to use those variables in my assignment not only to print them. Sorry for not mentioning it.
@Alb What do you have to use them for?
I have to make calculations of them in my main method. I tried to cast like this ((TableFidelity)(Tables.get(0)).getPoints()) but its not working
@Alb I suspect (but there isn't enough info here to be certain) that the calculations need to be pushed down to the classes, into a method that can be overridden. Otherwise there's no polymorphism anywhere. What calculations do you need to perform?
So i can't do a list with mixed subclasses?
|

Your Answer

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.