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 _____________________.

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

  both methods take precedence which can cause duplication.

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

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

 3. Method overriding is when the:

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

  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.

  parent class method supercedes any other method in the program.

 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

  TestandTrack Part 2

  TestandTrack Part 2, TestandTrack

  TestandTrack,TestandTrack Part 2

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

  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.

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

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

  mandatory and must be included for the compiler.

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

  mandatory, as it calls a function that performs the override

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

 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!

  TRUE

  FALSE

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

  TRUE

  FALSE

 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 Ruth

  Hello Ruth, Hello Hannah

  Hello Hannah

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

  changes

  overrides

  overloads

  instantiates

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

  FALSE

  TRUE

 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 an animal

  I am a dog, I am an animal

  I am a dog

  I am an animal, I am a dog

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

  same void and return method

  displayInfo() method

  same instance variable

  same class name e.g. Dog

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

  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.

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

  No. Overriding cancels out the method in the superclass.