Self-Review Questions

  1. What is the subclass?
    A subclass is the derived class of another class.

  2. What is the superclass?
    A superclass is the parent class of another class.

  3. What does a subclass inherit?
    A subclass inherits all variables and methods that are public.

  4. Are constructors inherited by subclasses?
    No.

  5. Does Java allow to a supeclass and subclass to have members with the same names?
    Yes.

  6. Explain the differences between super and this in Java.
    You use super to call the methods of the superclass and this to call the methods that the subclass has overriden.

  7. Which methods can a subclass not override?
    A child class cannot override any public parent's method that is defined with the final modifier.

  8. What type of inheritance does Java have?
    a)   single inheritance.
    b)   multiple inheritance.
    c)   class inheritance.
    d)   I am not sure.

  9. What restriction is there on using the super reference in a constructor?
    a)   It can only be used in the parent's constructor.
    b)   Only one child class can use it.
    c)   It must be used in the first statement of the constructor.
    d)   none of the above

  10. A class Animal has a subclass Mammal. Which of the following is true:
    a)   Mammal can have no subclasses.
    b)   Mammal can have no other parent than Animal.
    c)   Animal can have only one subclass.
    d)   Mammal can have no siblings.

  11. Consider the following set of Java class definitions:
    class A
    {
       int m1() {return 1;}
       int m2() {return m1();}
       int m4() {return m5();}
       int m5() {return 4;}
    }
    class B extends A
    {
       int m1() {return super.m2();}
       int m3() {return 7;}
    }
    class C extends B
    {
       int m1() {return 2;}
       int m2() {return super.m1();}
       int m3() {return super.m2();}
       int m5() {return 1;}
    }
    class D extends C
    {
       int m1() {return 3;}
       int m2() {return super.m4();}
       int m3() {return super.m3();}
       int m4() {return super.m2();}
    }
    
    What is the output of the call new D().m2()?
    1

    What is the output of the call new D().m3()?
    3

    What is the output of the call new C().m3()?
    2