The program given below tests your understanding of Inheritance and method overriding in core java.
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.
(1) ,(2) &(3).
Because the method book() of the type Hotel doesn't have the arguments.