Java Questions 31 - 40  «Prev  Next»

Overriding, Overloading methods (Interview Questions)/February 25, 2024

  1. What must be true for the access modifier of a subclass when it is overriding a method in the superclass?

    Answer:
    The overriding method cannot have a more restrictive access modifier than the method being overridden.
    You are allowed to override a method as long as the overiding method's access modifier is not more restrictive than the original one. This means you can do the following:
    Allowed:
    class Parent {
      public void aMethod() {
      }
    }
    class Child extends Parent {
      public void aMethod() {
      }
    }
    

    You are not allowed to do the following:
    Not Allowed:
    class Parent {
      public void aMethod() {
      }
    }
    class Child extends Parent {
      protected void aMethod() {
      }
    }
    
    In the above example, protected in the subclass is more restrictive than public in the superclass.

  2. What must be observed when overriding methods that throw exceptions?

    Answer:
    The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method.

  3. How does the JVM determine which method to run when a method is overridden?

    Answer:
    At runtime the JVM uses virtual method invocation to dynamically select the actual version of the method that will run, based on the actual instance.

  4. What is the relation between a superclass and subclass reference?

    Answer:
    A superclass reference can always refer to a subclass instance.

  5. What is the difference between method overriding and method overloading in Java?

    Answer:
    The overriding method must fulfill the contract of the superclass. Overloading occurs in Java when you have a different argument list.

  6. What will happen if the method of the subclass does not have the same arugment list as the method of the superclass?

    Answer:
    If the method of the subclass does not have the same argument list as the method of the superclass, you will overload instead of override the method.

  7. If a method in a subclass throws an exception, what must be true for the method in the superclass?

    Answer:
    General Rule: If the method in the subclass throws an exception, then the method in the superclass must also throw that exception.
    Example: If a method throws FileNotFoundException in a subclass, the overridden method in the superclass must throw the same exception.

  8. What is true about the overriding method of a subclass in relation to the method being overridden in the superclass?

    Answer:
    The overriding method must NOT throw checked exceptions that are broader (more encompassing) than those declared by the overridden method (in the superclass).

  9. What types of exceptions can the overriding method in the subclass throw?
    Answer:
    The overriding method can throw narrower or fewer exceptions.

  10. What is the relationship between a superclass and a subclass when it comes to throwing exceptions in Java?

    Answer:
    A method in the superclass may throw an exception. However, the overriding method in the subclass does not have to throw that same exception.

SEMrush Software