1) What's The Error?
1) What's The Error?
class Clock{ int minute; int hour; public Clock(int minute, int hour){ this.minute = minute; this.hour = hour; } } public class Cuckoo_Clock extends Clock{
public Cuckoo_Clock(int minute, int hour){ this.minute = minute; this.hour = hour; } public static void main(String[] args){ Cuckoo_Clock c = new Cuckoo_Clock(4, 3); } }
public class BankBranch extends Bank{ public BankBranch(String name, int balance, int bankID){ super(name, balance, bankID); }
public int getBalance(){ return balance; } public int getBankID(){ return bankID; } public String getBankName(){ return bankName; }
public static void main(String[] args){ BankBranch b = new BankBranch("Chase", 54, 10282); System.out.println(b.getBalance()); System.out.println(b.getBankID()); System.out.println(b.getBankName()); } }
interface Cookable extends Edible{ public int cookTime(int e); public String dishName(int e); }
class pipe{
int level = 1; int level = 2; public int carry(int b){ public int carry(int a){ return b; return level + a; } } public int getLevel(){ return level; } } public int getLevel(){ return level; } public int TubePressure(){ return 10282; } }
class tube extends pipe{ public class Shadowing{ public static void main(String[] args){ tube t = new tube(); System.out.println(t.carry(10)); pipe p = (pipe)t; System.out.println(p.carry(10)); p.level = 50; System.out.println(p.carry(10)); System.out.println(p.level); System.out.println(p.getLevel()); } }
4)Whats the output for each line of main ? What are the static and dynamic types of all the object on each line of main? (ans on next page)
tube t = new tube(); // dynamic type : Tube, uses Tube.carry() => Output: 12 System.out.println(t.carry(10));
pipe p = (pipe)t; //static type of p : Pipe, dynamic type: Tube. Uses Tube.carry() => Output: 12 System.out.println(p.carry(10));
//static type of p : pipe, we modify t's Pipe level but his Tube level is unchanged p.level = 50; //dynamic type : Tube. Uses Tube.carry() => Output: 12 System.out.println(p.carry(10)); // dot operator uses static vars: => Output: 50 System.out.println(p.level); //dynamic type : Tube, uses Tube.getLevel => Output: 2 System.out.println(p.getLevel()); }