Preview

06 - super Keyword

 1. The super keyword in Java is used in subclasses to access superclass members (attributes, constructors and methods).

  TRUE

  FALSE

 2. In the following example, we would use _________________ if the overridden methoddisplay() of superclass Animal needs to be called.
class Animal {

  // overridden method
  public void display(){
    System.out.println("I am an animal");
  }
}

class Dog extends Animal {

  // overriding method
  @Override
  public void display(){
    System.out.println("I am a dog");
  }

  public void printMessage(){

    // this calls overriding method
    display();

    // this calls overridden method
    ?????????WHAT GOES HERE?
  }
}

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

  display.super.dog();

  display.super();

  super.dog();

  super.display();

 3. The superclass and subclass should NEVER have attributes with the same name.

  FALSE

  TRUE

 4. What is the output of the following program?
class Animal {
  protected String type="animal";
}

class Dog extends Animal {
  public String type="mammal";

  public void printType() {
    System.out.println("I am a " + type);
    System.out.println("I am an " + super.type);
  }
}

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

  I am a mammal

  I am a mammal, I am an animal

  I am an animal, I am a mammal

  I am an animal

 5. In the above example, In this example, we have defined the same _________________ in both the superclass Animal and the subclass Dog.

  super class method

  sub class name

  method object

   instance field type

 6. To explicitly call the superclass constructor from the subclass constructor, we use _______.

  display.Super()

  super.display()

  super()

  SUPER.s()

 7. Assume that the following declaration appears in a client program: Base b = new Derived();, what is the result of the call b.methodOne();?
public class Base
{
   public void methodOne()
   {
     System.out.print("A");
     methodTwo();
   }

   public void methodTwo()
   {
     System.out.print("B");
   }
}

public class Derived extends Base
{
   public void methodOne()
   {
      super.methodOne();
      System.out.print("C");
   }

   public void methodTwo()
   {
     super.methodTwo();
     System.out.print("D");
   }
}

  ABC

  ABDC

  AB

  ABCD

 8. The keyword super can be used to call a superclass's _____________________.

  private methods only

  constructors only

  constructors and methods.

  overridden methods

 9. Using super() is not compulsory.

  FALSE

  TRUE

 10. Even if super() is not used in the subclass constructor, the compiler implicitly calls the _______________________________.

  constructors and methods contained in the subclass.

  default constructor of the superclass.

  default constructor of the subclass.

  overridden method contained in the subclass.