1

I am taking AP Comp Sci and we are learning about polymorphism and inheritance. Our instructor gave us the following problem:

If classes C1 and C2 both implement an interface Cint, which has a method “whichIsIt”, and if C1 c = new C1( ); is performed at one point of the program, then a later instruction c.whichIsIt( ); will invoke the whichIsIt method defined in C1.

Supposedly the answer is false. However, this makes no sense to me. The whole point of polymorphism is that it uses dynamic binding and will use the method that is defined in the lowest class in the hierarchy. How can the answer to this question be false?

1 Answer 1

2

Java has early binding for final and overloaded method and late binding for overriden methods. So unless whichIsIt method is final or overloaded the answer should be true. Also before java8 interfaces were not allowed to have method implementations.

Cint c = new C1();
c.whichIsIt() //Assuming C1 overrides whichIsIt method defined in Cint, 
              //then this will call C1.whichIsIt method
Sign up to request clarification or add additional context in comments.

3 Comments

Just to clarify, if the method is overriden, it will use the method declared lower in the class hierarchy?
Edited answer to clarify.
Thanks. It seems like the question should have clarified if the method was overriden or not. I appreciate the clarification on the topic.

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.