Preview

05 - Overriding Methods

 1. Inheritance allows us to derive a new class (subclass) from an existing class (superclass). The subclass inherits the attributes and methods of the superclass.

  FALSE

  TRUE

 2. If the same method is defined in both the superclass class and the subclass class, then _____________________.

  the method of the subclass class overrides the method of the superclass.

  the method of the superclass class overrides the method of the subclass.

  both methods take precedence which can cause duplication.

  both methods are automatically overridden (e.g. cancelled out).

 3. Method overriding is when the:

  parent class method supercedes any other method in the program.

  method of the subclass class overrides the method of the superclass.

  method of the superclass cancels out the method of the subclass if it exists.

  the method of the superclass overrides the method of the subclass.

 4. What is the output of the following code?
public class Learner
{
   public String speak()
   {
      return "TestandTrack";
   }

   public static void main(String[] args)
   {
      Learner g1 = new Learner();
      System.out.println(g1.speak());
      Learner g2 = new Learner2();
      System.out.println(g2.speak());
   }
}

class Learner2 extends Learner
{
   public String speak()
   {
      return "TestandTrack Part 2";
   }
}

  TestandTrack Part 2, TestandTrack

  TestandTrack,TestandTrack Part 2

  TestandTrack Part 2

  TestandTrack

 5. To override an inherited method, the method in the child class __________________________________.

  need not have the same name, as long as the parameter list is the same.

  must have the same name, parameter list, and return type (or a subclass of the return type) as the parent method.

  must have its own constructor with no return type.

  must have the asme name and return type, but the parameter lists can vary.

 6. You may see the @Override annotation above a method. This is _______________________.
>>@Override
public String speak()
{
     return "TestandTrack Part 2";
}

  optional and it is the same as a comment (has no real function)

  mandatory and must be included for the compiler.

  optional but it provides an extra compiler check to ensure accuracy.

  mandatory, as it calls a function that performs the override

 7. Overloading a method is when several methods have the same name but the parameter types, order, or number are different.
Note: Do not get overriding and overloading confused!

  FALSE

  TRUE

 8. In overloading, only the method names are identical and they have different parameters.

  FALSE

  TRUE

 9. What is the output of the following code?
public class Hello
{
   public String greet()
   {
      return "Hiya there";
   }

   public String speak(String who)
   {
      return "Hello " + who;
   }

   public static void main(String[] args)
   {
      Hello h1 = new Hello();
      System.out.println(h1.speak("Ruth"));
      Hello h2 = new Goodbye();
      System.out.println(h2.speak("Hannah"));
   }
}

class Goodbye extends Hello{
   public String speak()
   {
      return "Goodbye and God Bless";
   }
}

  Hello Ruth, Hello Hannah

  Hello Hannah

  Hello Ruth

  Hello Hannah, Hello Ruth

 10. In the example above the speak(String who) method ______________the speak() method of Hello.

  changes

  instantiates

  overrides

  overloads

 11. In the example provided in question 9, Goodbye does not inherit any method and is therefore overriden.

  TRUE

  FALSE

 12. A subclass is usually designed to have modified (overridden) or additional methods or instance variables.

  FALSE

  TRUE

 13. What is the output of the following code?
class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   @override
   public void displayInfo() {
      System.out.println("I am a dog.");
   }
}

class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.displayInfo();
   }
}

  I am a dog

  I am an animal, I am a dog

  I am a dog, I am an animal

  I am an animal

 14. In the above program, the ___________ is present in both the Animal superclass and the Dog subclass

  same class name e.g. Dog

  same void and return method

  same instance variable

  displayInfo() method

 15. Can we access the method of the superclass after overriding?

  No. Overriding cancels out the method in the superclass.

  Yes. To access the method of the superclass from the subclass, we use the super keyword.

  Yes, but only if the method in the superclass has not been overridden.

  No. To access the method of the superclass overriding should not have taken place.