Java Questions 31 - 40  «Prev  Next»

Overloading vs. Overriding

  1. Do you have to pay attention to the return type when overloading a method?

    Answer:
    No. When you overload a method (by changing the parameters), the return type does not have to match that of the superclass version.

  2. What is the difference between arguments and parameters?

    Answer:
    Parameters are the elements in the methods signature that indicate what the method must receive when its inovked.

  3. Which characteristics must an overriding method in a subclass contain?

    Answer:
    When a subclass wants to override an inherited method the subclass must define a method that matches the inherited version exactly (For reference types covariant returns are allowed).

  4. What is a covariant return type?

    Answer:
    A "covariant return type" in Java means that an overridden method in a subclass can return a more specific type than the method it overrides in the superclass.
    Introduced in Java 5 Prior to Java 5, an overridden method had to return the exact same type as the method in the parent class. Java 5 introduced covariant return types, allowing a subclass method to return a subtype of the return type defined in the superclass method.
    Example of Covariant Return Type
    class Parent {
        Parent getObject() {
            return new Parent();
        }
    }
    
    class Child extends Parent {
        @Override
        Child getObject() {  // Covariant return type (returns a subclass)
            return new Child();
        }
    }
    
    public class CovariantDemo {
        public static void main(String[] args) {
            Parent p = new Child();
            System.out.println(p.getObject().getClass().getName()); // Outputs "Child"
        }
    }
    


    Explanation
    1. The Parent class has a method getObject() returning Parent.
    2. The Child class overrides getObject() but returns Child instead of Parent.
    3. Since Child is a subclass of Parent, this is valid due to covariant return types.

    Why Are Covariant Return Types Useful?
    1. Improves Readability and Type Safety
      • No need for explicit type casting when working with subclass objects.
    2. Enhances Code Maintainability
      • The overriding method can return a more specific type, making it more usable in subclass-specific operations.
    3. Better OOP Principles (Polymorphism and Inheritance)
      • Subclasses can provide more specialized behavior while maintaining the same method contract.

    Without Covariant Return Types (Before Java 5)
    class Parent {
        Parent getObject() {
            return new Parent();
        }
    }
    
    class Child extends Parent {
        @Override
        Parent getObject() {  // Had to return Parent, not Child
            return new Child();  
        }
    }
    
    • Before Java 5, you had to explicitly cast the return type:
          Child c = (Child) new Child().getObject();  // Requires type casting
          
    • With covariant return types, this is no longer necessary.

    Covariant Return Types with Interfaces
    It also works with interfaces:
    interface Animal {
        Animal create();
    }
    
    class Dog implements Animal {
        @Override
        public Dog create() {  // Covariant return type
            return new Dog();
        }
    }
    


    Key Rules of Covariant Return Types
    • The overridden method in the subclass must have the same method signature as in the parent class (except for the return type).
    • The return type in the subclass must be a subtype of the return type in the parent class.
    • Works only for non-primitive return types (can't return an int if the parent method returns double).

    Conclusion The covariant return type feature in Java makes code more flexible, type-safe, and readable by allowing a subclass to return a more specific type in method overriding. It removes the need for explicit casting and aligns well with object-oriented principles.
    As of Java 5, you are allowed to change the return type in the overriding method as long as the new return type is a subtype of the declared return type of the overridden method.


  5. What role does the return type play with regards to overloading and overriding?

    Answer:
    Know the following 2 facts:
    1. Overloaded methods can change the return type
    2. overriding methods can change the return type if the overridden method is a subclass of the Superclass.


  6.  


  7. For primitive return types, what must be true of the overriding method?

    Answer:
    When overriding a method, the method in the subclass must match the inherited version exactly.

  8. What task must be completed to create a new object?

    Answer:
    You can not make a new object without invoking a constructor.

  9. How are objects initialized?

    Answer:
    Constructors are the code that runs whenever you use the keyword new.
    Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

  10. What must every class possess?

    Answer:
    Every class, including abstract class, must have a constructor.

  11. What are two key points to remember about constructors?

    Answer:
    1. Constructors do not have a return type
    2. Their names must exactly match the class name.