Java OOP

 

SHORT ANSWER QUESTIONS

INHERITANCE & POLYMORPHISM 2

 

QUESTIONS

 

1-A _____________ instance variable in the superclass is fully accessible in subclasses.

2-A _____________ instance variable in the superclass is not accessible in subclasses or in any other class.

 

3-A _____________ instance variable in the superclass in fully accessible in the subclasses but not in other classes.

 

4-A method in a subclass that shares the same name and parameter list as a method in the superclass is said to be a(n) _______________ method.

 

5-We can specify to Java that it needs to verify that a method is in fact overriding another method by adding _______________ on the line above the method header.

 

6-From inside a subclass with an overriding method named toString(), we can call the superclass’ toString method by using ____________________________.

 

7-_________________ allows us to have a superclass reference to a subclass object.  This allows us to process a superclass and subclass objects at once in a loop.  We can also place all of these object in a single array.

8-True or false.  Polymorphism can be used with inheritance and it can also be used with interfaces.

 

9-In the polymorphic reference below, we know that ClassA is a _______________ of ClassB.

 

ClassA a = new ClassB();

 

10-True or False.  Considering the above polymorphic reference, we can call a.methodName() only if methodName() exists in ClassA.

 

11-True or False.  In the above polymorphic reference, if we call a.methodName() and methodName() exists in both ClassA and ClassB, then the ClassB method will be executed.

 

12-In Java, the ______________ class is always at the top of the class hierarchy.  All classes inherit from this class.

 

13-The following code has an error associated with the toString method in Frost.  What needs to be changed to fix the error?

Superclass

Subclass

public class Spell
{
   //only the datafields are shown

 

   private int manaCost;

   private String name;
}

public class Frost extends Spell
{

   private int damage;

 

   //only the toString() method is shown

 

   public String toString()

   {

      return name + "(" + damage + ")";

   }

}

 

14-Let’s consider the classes below.  The Enemy class has a damage method that calculates the standard damage made by an enemy.  The Ghost class overrides that method so that Ghosts do twice the damage. 

However, the Ghost class has a critDamage method that should do 5x the standard damage that is done in the Enemy’s damage method.  Currently, the critDamage is calling the damage method in Ghost.  What needs to be changed so that it calls the damage method in Enemy?

Superclass

Subclass

public class Enemy
{
   //only the damage method is shown

 

   public int damage(int defense)

   {

      return damage * attack/defense;

   }
}

public class Ghost extends Enemy
{

   //only two methods are shown

 

   @Override

   public int damage(int defense)

   {

      return 2*damage * attack/defense;

   }  

 

   public int critDamage(int defense)

   {

      return damage(defense) * 5;

   }

}

 

15-Let’s consider the classes below.  There is an error in the Boss class.  How do you fix it?

 

 Hint: Removing the @Override would actually make the error go away but that is not what we want to do here.  We want to keep the @Override and fix the error.

 

Superclass

Subclass

public class Badguy
{
   //this is the only instance method

   //in the class

 

   public int damage(int defense)

   {

      return damage * attack/defense;

   }
}

public class Boss extends Badguy
{

   //only this method is shown

 

   @Override

   public int dmg(int defense)

   {

      return 2*damage * attack/defense;

   }  

}

 

SOLUTIONS

 

Click here.