Share on Google+Share on Google+

SCJP Module-4 Question-20

Advertisement
The program given below tests your understanding of Inheritance and method overriding in core java.

Given below the sample code :

1   class Hotel {
2   public int bookings;
3   public void book() {
4   bookings++;
5   }
6   }
7   public class SuperHotel extends Hotel {
8   public void book() {
9   bookings--;
10  }
11  public void book(int size) {
12  book();
13  super.book();
14  bookings += size;
15  }
16  public static void main(String args[]) {
17  Hotel hotel = new Hotel();
18  hotel.book(2);
19  System.out.print(hotel.bookings);
20  }}

How can we correct the above code ?

1. By adding argument "int size" to the method book at line number 3.

2. By removing argument '2'  at  line number 18.

3. By creating object of "SuperHotel" subclass at line 17 & calling book(2) from it at line 18

4. No correction needed.

Answer:

(1) ,(2) &(3).

Explanation :

Because the method book() of the type Hotel doesn't have the arguments.

Advertisements

Advertisement

Posted on: July 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.