1

I have two classes: Teacher and Coordinator. Coordinator extends Teacher.

Basically I wanted to make an instance variable that could take in either a Teacher instance or Coordinator instance.

I know this is easily possible by just writing Teacher exp = new Teacher() or Teacher exp = new Coordinator(), however when I do this, I am only able to access Teacher methods and properties when I apply new Coordinator() or new Teacher() to the reference variable which is normal. My question is that is there another way where I only use one instance variable that can be assigned to Teacher OR Coordinator objects and use this variable to call any property/method of the object that was assigned to the object reference variable?

2
  • You do realize that classes can be extend from an existing class. How will your already written code can predict methods from the classes that you haven't extended? reflection API is the only sane mechanism. Other options are to write interfaces which are guaranteed to specify required protocol of the class. Commented Dec 27, 2012 at 23:47
  • How would you know if it is a Coordinator? Commented Dec 27, 2012 at 23:47

5 Answers 5

8

You can check if exp is a Coordinator then cast and call its function accordingly:

if(exp instanceof Coordinator) {
    ((Coordinator)exp).foo();
}

If you have several things you want to do, you can create a variable in a local scope and use it repeatedly:

if(exp instanceof Coordinator) {
    Coordinator expCoord = (Coordinator)exp;
    expCoord.foo();
    expCoord.bar();
    expCoord.baz();
}
Sign up to request clarification or add additional context in comments.

2 Comments

So after doing this, will Teacher exp (Is assigned to a coordinator object) become Coordinator exp permanently?
No. It only works for that expression in the first example or that scope in the second.
1

No way to do that unless all the methods you want to call are present in the parent class.

You can cast what's in a Teacher variable to be a Coordinator but it will fail if what's in the Teacher variable isn't really a Coordinator object instance.

((Coordinator)exp).coordinate();

Comments

0

Whenever you want to call a method of Coordinator, you would have to cast the object to a Coordinator first.

((Coordinator)myTeacher).coordinatorMethod();

Comments

0

No, because of type safety. But you can always do:

Teacher exp = new Coordinator()
((Coordinator)exp).someCoordinatorMethodOrProperty()

Comments

0

Yes you are correct,

Here same concept implements as parent child relationship Since Instance of child class can access the property of child as well as parents both, but the instance of parents class access only the property of child class...

That is why it is happening.

Comments

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.