SCJP Module-4 Question-11


 

SCJP Module-4 Question-11

The program given below checks your knowledge of Inheritance in Java and also access restriction on methods in program.

The program given below checks your knowledge of Inheritance in Java and also access restriction on methods in program.

Given below the sample code :

class Hotel {
public int bookings=2;
public void book() {
bookings++;
}
}

public class SuperHotel extends Hotel {
public void book() {
bookings--;
}

public void book(int size) {
book();
super.book();
bookings += size;
}

public static void main(String args[]) {
SuperHotel Shotel = new SuperHotel();
Shotel.book(2);
System.out.print(Shotel.bookings);
}
}

Find the output of the following code :

1. Compile error

2. 2

3. 4

4. No output.

Answer :

(3)

Explanation :

"SuperHotel" Subclass 's object  "Shotel" calls the "book()" method of it.

Ads