Skip to content
Advertisement

Why do I get an error when entering child classes to a TreeMap defined for their parent? [closed]

In my program I have an abstract class TelephoneEntry which holds a TelephoneNumber(it implements Comparable) and an addres, then I have child classes Person and Company that extend the TelephoneEntry class.

abstract class TelephoneEntry {
    protected TelephoneNumber number;
    protected String address;

    public abstract void description();

    public TelephoneNumber getNumber() {
        return number;
    }

    public void setNumber(TelephoneNumber number) {
        this.number = number;
    }
}

class Person extends TelephoneEntry {
    protected String name;
    protected String surname;

    public Person(String name, String surname, String address, int code, long number) {
        this.name = name;
        this.surname = surname;
        this.address = address;
        this.number = new TelephoneNumber(code, number);
    }

    @Override
    public void description() {
        System.out.println("Name: " + name);
        System.out.println("Surname: " + surname);
        System.out.println("Address: " + address);
        System.out.println("TelephoneNumber: " + number.toString());
        }
    }

class Company extends TelephoneEntry {
    protected String name;

    public Company(String name, String address, int code, long number) {
        this.name = name;
        this.address = address;
        this.number = new TelephoneNumber(code, number);
    }

    @Override
    public void description() {
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("TelephoneNumber: " + number.toString());
    }
}

public class TelephoneNumber implements Comparable<TelephoneNumber> {
    private int countryCode;
    private long localNumber;

    public TelephoneNumber(int code, long number) {
        countryCode = code;
        localNumber = number;
    }


    @Override
    public int compareTo(TelephoneNumber otherNumber) {
        if (Integer.compare(getCountryCode(), otherNumber.getCountryCode()) != 0) {
        return Long.compare(getLocalNumber(), otherNumber.getLocalNumber());
    } else
        return Integer.compare(getCountryCode(), otherNumber.getCountryCode());

    }

    public String toString() {
        String out = "";
        out += ("+" + countryCode + " " + localNumber);
        return out;
    }
}

But when I try passing them to a tree map defined as

TreeMap<TelephoneNumber,  TelephoneEntry> map = new TreeMap<>();

I get an error stating that:

"The method add(TelephoneNumber, Person) is undefined for the type TreeMap<TelephoneNumber,TelephoneEntry>"
"The method add(TelephoneNumber, Company) is undefined for the type TreeMap<TelephoneNumber,TelephoneEntry>"

I’m not sure what may the cause of this error be as they both inherit the used key from TelephoneEntity.

Advertisement

Answer

Maps have put() method, not an add() method, for adding entries.

Change your code to:

myMap.put(myTelephoneNumber, myTelephoneEntry);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement