Inheritance 1
Inheritance 2
Inheritance 3
100

Which of the following best describes the relationship between a superclass and a subclass in Java?

a. A subclass can access all of the methods and fields of its superclass, including the private ones.

b. A superclass is a different version of a subclass and contains more specific functions.

c. A subclass inherits non-private attributes from its superclass and can add its own methods and fields.

d. A subclass and a superclass must have the same methods to maintain their compatibility.



c

100

In Java, method overriding happens when a subclass provides a specific implementation of a method that is already defined in the ______ class.



superclass

100

If a subclass has a constructor that takes parameters and the superclass has a constructor with no parameters as well as one with parameters, the subclass can choose to call either one. However if the subclass constructor does not explicitly invoke one of these superclass constructors, Java will attempt to call which constructor of the superclass by default?



No-argument constructor of the superclass

200

What keyword in Java is used to create a “is-a” relationship between a subclass and superclass?

a. new

b. this

c. extends 

d. super

c

200

Fill in the blanks: In Java, inheritance allows a subclass to inherit methods and fields from its _______ class. A subclass can modify the behavior of an inherited method by _______ it. This concept is known as _______ and allows the subclass to define specific behaviors. 



Answers: superclass. overriding, polymorphism

200

In Java, constructors are inherited. 

True/False

False

300

 class Parent { 

public void display() { 

System.out.println("Parent"); } }

 class Child extends Parent { 

public void display(){ 

System.out.println("Child"); } }

What is the output of the following code?

Parent obj = new Child();

obj.display();

A. Parent
B. Child
C. Compilation error
D. Runtime error



B

300

What does the following code segment print?

class Faculty { 

    void message()  { 

        System.out.println("This person works at a school\n");   } } 

class Teacher extends Faculty { 

    void message()   { 

        System.out.println("This person is a teacher");  } 

    void display()  { 

        message(); 

        super.message();  } } 

// Driver Class 

class Test { 

    public static void main(String args[])    { 

        Teacher a = new Teacher(); 

         a.display();    } }



This person is a teacher

This person works at a school



300

Which of the following statements about inheritance in Java is true?

A) A subclass can directly access private members of its superclass.

B) A subclass can inherit the constructors of its superclass.

C) A subclass can override static methods from its superclass.

D) A subclass can access protected members of its superclass.



D

400

public class Vehicle {

    public Vehicle(int wheels, int speed) { /* ... */ }

}

public class Car extends Vehicle {

    public Car(String model, int wheels, int speed) { /* ... */ }

}

And the following declaration:

Vehicle[] garage = new Vehicle[3];

garage[0] = new Vehicle(4, 120);

garage[1] = new Car("Sedan", 4, 150);

Car sportsCar = new Vehicle(2, 200);

garage[2] = sportsCar;

Why does this code not compile? 


A Car cannot reference a Vehicle.

400

public class Vehicles{

public void drive(){System.out.print(“I can drive!!”);}}

public class Cars extends  Vehicles {

public void drive(){System.out.print(“I can drive a car!);}}

public class Toyota extends Cars{}

public class Sienna extends Toyota{}

The following code segment appears in another class

rob.drive();

Under which of the following conditions will the code segment print “I can drive a car!”

a. If rob is an instance of Cars or Toyota

b. If rob is an instance of Cars, Toyota, or Sienna

c. If rob is an instance of Vehicles

d. If rob is an instance of Vehicles, Cars, Toyota or Sienna

b

400

public class Animal {

    public void makeSound() {

        System.out.println("Animal makes a sound");    }}

public class Dog extends Animal {

    public void makeSound() {

        System.out.println("Dog barks");    }}

public class Cow extends Animal{

    public void cowEats() {

        System.out.println("The cow eats grass.");    }}

Write the code for the driver class to create a new Dog object and print “Dog barks”.




public class Main {

    public static void main(String[] args) {

        Animal dog = new Dog();

        dog.makeSound();     }}