Preview

06 - super Keyword

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

  FALSE

  TRUE

 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();
  }
}

  super.display();

  display.super();

  super.dog();

  display.super.dog();

 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 an animal, I am a mammal

  I am a mammal, 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.

  sub class name

  super class method

   instance field type

  method object

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

  super()

  super.display()

  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");
   }
}

  ABDC

  ABC

  AB

  ABCD

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

  private methods only

  overridden methods

  constructors and methods.

  constructors only

 9. Using super() is not compulsory.

  TRUE

  FALSE

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

  overridden method contained in the subclass.

  default constructor of the superclass.