2

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.

7
  • 1
    provide the super and sub classes implementations - are the methods you try to define part of the super class? (is Contact a class or interface?) Commented Aug 25, 2015 at 16:51
  • 5
    If your member variable is type of contact, you can only use the methods that are provided by the Contact class. Commented Aug 25, 2015 at 16:52
  • 1
    Please provide the full compilation error your compiler gives. It will make the answer significantly better, focused and faster. Commented Aug 25, 2015 at 16:53
  • 2
    Guys...it's obvious, oneteen posted enough code. Seriously Commented Aug 25, 2015 at 16:53
  • 1
    Thank u all for the comments :) Thanks for the advice amit - it makes sense yes I understand what U mean. I will do this :) Commented Aug 25, 2015 at 17:12

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I used this downcasting it works perfectly :)
1

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);

1 Comment

I am glad that it solved your problem. Happy coding.

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.