class Room { private static int nextID = 1; //id of next room to be created protected int id; private int mess; //messiness index public Room(int mess) { this.mess = mess; id = nextID; nextID++; } public String toString() { return "Room " + id; } public void clean() { mess--; if (mess<0) mess=0; } public void report() { System.out.println( toString()+", has messiness index "+mess); } public static void countRooms() { System.out.println((nextID-1)+" rooms in total"); } } //class Room class Bathroom extends Room { private boolean hasShower; public Bathroom(int mess, boolean hasShower) { super(mess); this.hasShower = hasShower; } public String toString() { String line = super.toString(); line += ", a bathroom"; if (hasShower) line += " with shower"; return line; } public void majorCleanUp() { clean(); clean(); clean(); clean(); } } //class Bathroom public class House { public static void main(String[] args) { Room r1 = new Room(5); Bathroom r2 = new Bathroom(10,true); // Method invocation (super and sub class), overriding System.out.println(r1); System.out.println(r2); r1.report(); r2.report(); System.out.println(); r1.clean(); r1.report(); r2.clean(); r2.report(); r2.majorCleanUp(); r2.report(); //r1.majorCleanUp(); System.out.println(); // Polymorphism Room r3 = new Bathroom(20,false); System.out.println(r3); r3.clean(); r3.report(); //r3.majorCleanUp(); ((Bathroom)r3).majorCleanUp(); r3.report(); System.out.println(); // Static methods and variables Room.countRooms(); Bathroom.countRooms(); } } //class House