More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. 1. Overriding method can not be more restrictive than the overridden method.
reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.
If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime when the JVM invokes the true objects version of the method rather than the reference types version then it will be problematic
2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.
3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.
Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.
We cannot access methods defined in String class or in any class in hierarchy between String and Object.
For example - we cannot do obj.append("abc") as it will now give compile time error.
Help us improve. Please let us know the company, where you were asked this question :
Q3. When are static and instance methods resolved ? During compile time or Runtime ?
Ans. Static methods are resolved during compile time and hence they cannot participate in runtime polymorphism. Instance methods are resolved during runtime.
Help us improve. Please let us know the company, where you were asked this question :
Q4. Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?
a. Serialization
b. Runtime Polymorphism
c. Lazy Loading
d. Memory
Ans. Runtime Polymorphism
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  static class   static vs singleton   java   oops   objects  Runtime Polymorphism
Ans. dynamic method dispatch is a process in which a call to an
overridden method is resolved at runtime rather than at compile-time. This is used to achieve runtime polymorphism in java.
Help us improve. Please let us know the company, where you were asked this question :