Preview

07 - References and Inheritance Hierarchies

 1. ___________________________________, it is known as hierarchical inheritance.

  When two or more super classes over ride a single subclass

  When two or more classes inherits a single class

  When one class inherits from two or more classes

  When a subclass inherits from multiple or multi level super classes

 2. Below is a list of the types of inheritance. What is a definition of Multilevel inheritance?
Single inheritance
===================
Class B extends from class A only.

Multilevel inheritance
=====================
????????????????????????

Hierarchical inheritance
=======================
Class A acts as the superclass for classes B, C, and D.

Multiple inheritance
=======================
Class C extends from interfaces A and B.

Hybrid inheritance
======================
Mix of two or more types of inheritance.

  Class A extends from class B; then class B extends from class A.

  Class A, B and C all extend from class A, including Class A itself (as a duplicate)

  Class B extends from class A; then class C extends from class B.

  Class B extends from class A; then class C extends from class A.

 3. Java does not support multiple and hybrid inheritance through classes. However, we can achieve multiple inheritance in Java through _______.

  interfaces

  polymorphism

  composition

  duplication

 4. Here is an inheritance hierarchy of _______. A Venus fly trap is-a Plant and a _________of Plant.
inheritance_heirarchies_image1.png

  subclasses / inherited class

  Species / super class

  LivingBeings /sub class

  LivingBeings / superclass

 5. One of the main reasons to use an inheritance hierarchy is that the instance variables and methods from a superclass are __________________.

  inherited and can be used in a subclass by rewriting the code and copying it again.

  inherited and can be used in a subclass without rewriting or copying code.

  duplicated, thereby making the code more complex and usable.

  overridden, thereby increasing their capacity for achieving specifics.

 6. A superclass reference variable can hold an object of that superclass or of any of its subclasses. For example, a Shape reference variable can hold _____________________.
Shape s1 = new Shape();
Shape s2 = new Rectangle();
Shape s3 = new Square();

  an object that is not directly related to a rectangle, e.g. a circle

  objects that are of different types, e.g. circles

  only a Rectangle object (not a Square)

  a Rectangle or Square object.

 7. A Square reference can hold a Shape object because all Shapes are Squares.

  FALSE

  TRUE

 8. The code below will give an ?Incompatible types: Shape cannot be converted to Square? error.
Square q = new Shape(); 

  TRUE

  FALSE

 9. Given the above context, you ____________________________.

  could use a type-cast to get it to be a Square.

  could never get it to be a Square without changing the class.

  could use a Super keyowrd to get it to be a Square.

  could use a inhertiance dynamic to get it to be a Sqaure.

 10. A class Pupil inherits from the superclass Human. Which of the following assignment statements will give a compiler error?

  Pupil p = new Human();

  Human h = new Pupil();

  Human h = new Human();

  Pupil p = new Pupil();

 11. Given the above example, which of the following is correct:

  Human is not always a Pupil.

  Human is not always a Human.

  Pupil is not always a Human.

  Pupil is not always a Pupil.

 12. ________ an inheritance hierarchy is that we can write methods with parameters of the superclass type and pass in subclass objects to them.

  One disadvantage of

  One example of single level inheritance in

  Another advantage of

  One grossly underused feature of

 13. If class B is subclassed from class A then which of the following is the correct syntax?

  Class B:A{}

  class B implements A{}

  class B extends class A{}

  class B extends A{}

 14. The concept of multiple inheritance is implemented in java by ?
A - extending two or more classes
B - extending one class and implementing one or more interfaces
C - implementing two or more interfaces
D - all of these

  B and C

  A and C

  A and B

  D

 15. What is the output of the following?
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3{  
public static void main(String args[]){  
Cat c=new Cat();  
c.meow();  
c.eat();  
//c.bark();//C.T.Error  
}}  

  eating?.

  meowing... eating?

  eating?.meowing?..

  meowing?.