No comments yet

Top 10 Interview Questions in Overriding – Runtime Polymorphism

[Social9_Share type="horizontal"]

Top 10 Interview Questions in Overriding – Runtime Polymorphism

In this blog lets discuss about the interview questions relevant to overriding

  1. What is Overriding ?
  2. Overriding comes under polymorphism, which is one of the Object Oriented Programming concept. Overriding happens in a super class-subclass scenario. When you rewrite a method of the super class, in the subclass for providing a different functionality, it is called as Overriding. Overriding is also called as runtime polymorphism

    Rules for overriding

    • The method name in the subclass is same as that of the super class.
    • The arguments should be same as that of the super class method.
    • The return type should be same as that of the super class method except covariant return types. Refer question-6
    • The access specifier of the overriding method should be less restrictive. Refer question-7
    • The subclass method can throw same exception /subclass of the exception thrown by the super class method or does not have to throw any exception. Refer question-10
    • The subclass method may not throw new or broader exceptions from that of the super class. Refer question. Refer question-10

    Example

    In the above case, the method calcBonus() of the super class Employee is overridden by the subclasses Manager and Programmer to give a different functionality/logic.

  3. What is the purpose of Overriding?
  4. The purpose of overriding is to get a different functionality for the same method of the super class in the subclass.

  5. How will you call an overridden method in the subclass?
  6. Create a reference of the super class and make it point to any of the subclass object. Then, when you call the method using the super class reference, only the overridden method in the subclass will be called. Based on the above example, we can write the java code as follows.

    Always call the overridden method in the subclass using the super class reference only.

  7. Why Overriding is called runtime polymorphism?
  8. Lets understand this using the above example. We have Employee as super class and Manager and Programmer as subclasses. We have overridden calcBonus() method in the subclass. To call the method our super class reference, has to point to a subclass object.
    How does it know to which subclass to point to? Based on the user input.
    In the main class, if you give the input saying that you want to calculate bonus of Programmer, then the Employee reference will point to the Programmer object.If you give the input to calculate bonus of Manager, then the Employee reference has to point to Manager object.
    Because, this happens during the run time based on the user input, this is called as runtime polymorphism.

    Become an expert Java Developer

  9. Can we call the own methods of the subclass using the super class reference?
  10. No. Using the super class reference, you can call all the methods of the super class and only the overridden methods in the subclass. Lets understand this with an example.

    Example

  11. What are covariant return types?
  12. Assume, the return type of a super class method is a class itself, that is if the method returns an object(public Employee findEmployeeById(int empId)). In this case, if you override the method in the subclass, you can have the return type as Employee or subclasses of Employee. This is called as covariant return type. Lets understand this with an example.

    Example

  13. Can static methods be overridden?
  14. No. static methods cannot be overridden. Let us understand this with an example.

    Example

  15. What should be the access specifier of the overridden method in the subclass?
  16. The access specifier of the overridden method should be less restrictive.

    • If the super class method has the access specifier as public, the overridden method should have access specifier as public.
    • If the super class method has the access specifier as protected, the overridden method can have access specifier as public or protected.
    • If the super class method has the access specifier as default(package level), the overridden method can have access specifier as public or protected or default.
  17. Can we override a private method?
  18. No. The private method the super class is not visible to the subclass. So there is no way of overriding it

  19. What kind of exception can a overridden method throw?
  20. The overridden method in the subclass may or may not throw an exception based on the exception thrown by the super class method.If the super class method throws an exception, then the subclass

    1. Doesn’t have to throw the exception
    2. Can throw the same exception as thrown by the super class. If the super class method throws IOException then the subclass can also throw IOException
    3. Can throw the subclass of the Exception thrown by the super class.If the super class method throws Exception(the super class of all the exceptions) then the subclass can throw FileNotFoundException or IOEXception or RuntimeException(subclass of Exception)
    4. The overridden method in the sub class can throw any RuntimeException, even if the super class is not throwing any Exception
    5. The overridden method in the subclass cannot throw new or broader exceptions. This will give a compiler error

    6. If you have some more questions relevant to overriding, then do share with us.Also learn about top questions and answers in abstraction.

      Happy Learning

Post a comment