kenpino
kenpino

Reputation: 63

Polymorphism, issue on my program

I've been looking around and even though I have found some answers for some reason I can't seem to grasp the concept which is obviously causing a problem in my code.

I have this:

public static Contact createContact() {

    Contact contact = null;

    if (ContactUI.getRdb_acquaintance().isSelected()) {
        contact = new Acquaintance();
    } else if (ContactUI.getRdb_friend().isSelected()) {
        contact = new Friend();
        //contact.setStr_telMobile(ContactUI.getTxt_telMobile().getText());
    } else {
        contact = new Family();
        //contact.setStr_telMobile(ContactUI.getTxt_telMobile().getText());
        //contact.setStr_BDay(ContactUI.getTxt_BDay().getText());
    }

    setCommonDetails(contact);

    return contact;
}

What I would like is to be able to call the subclasses specific methods .setStr_telMobile and .setStr_BDay once I have initialized contact as one of its subclasses, but I get an error, I can't access those methods. I have commented the lines in my code.

I thought I was supposed to be able to initialize an object of type Contact to a Friend (subclass of Contact), for example, and get access to the subclass specialized methods and attributes.

Upvotes: 2

Views: 78

Answers (2)

JacksOnF1re
JacksOnF1re

Reputation: 3522

As I said in the comment you can't access methods from your subclasses if your instantiate the super class as a member variable.

If you need access to the subclass implementations, than you have to cast.

if(contact instanceof Friend){
Friend friend = (Friend) contact;
drinkBeerWith(friend);
}

But, think about another pattern to implement your code. Maybe polymorphism isn't the way to do here. An indicator that the concept of your design may be kind of "wrong" is the use of instanceof.

Cheers

Edit:

What I would like is to be able to call the subclasses specific methods .setStr_telMobile and .setStr_BDay once I have initialized contact

These methods sound as every Contact has kind of a mobile number and a birthday, right?

If so, implement the methods as abstract methods in your super class and let the subclass overright them

public abstract void setStr_telMobile (String mobileNumber);

Upvotes: 1

Mureinik
Mureinik

Reputation: 312219

Regardless of its runtime type, contact is a reference of the Contact type. As such, the compiler will only allow you to use Contact's methods.

The elegant way to solve this would probably be to use a temporary Friend reference to initialize all the members you need and only then assign it to Contact:

// Snipped
} else if (ContactUI.getRdb_friend().isSelected()) {
    Friend temp  = new Friend();
    temp.setStr_telMobile(ContactUI.getTxt_telMobile().getText());
    contact = temp;
}
// snipped

Alternatively, since you know you've assigned contact with a Friend instance, you could explicitly downcast it to a Friend to access its methods:

// Snipped
} else if (ContactUI.getRdb_friend().isSelected()) {
    contact  = new Friend();
    ((Friend) contact).setStr_telMobile(ContactUI.getTxt_telMobile().getText());
}
// snipped

Upvotes: 1

Related Questions