1) What's the error?
class Clock{ int minute; int hour; public Clock(int minute, int hour){ [Link] = minute; [Link] = hour; } } public class Cuckoo_Clock extends Clock{
public Cuckoo_Clock(int minute, int hour){ [Link] = minute; [Link] = hour; } public static void main(String[] args){ Cuckoo_Clock c = new Cuckoo_Clock(4, 3); } }
Ans: No call to an appropriate super() constructor 2) Whats the error?
class Bank{ private int balance; protected int bankID; public String bankName; public Bank(String name, int balance, int bankID){ [Link] = balance; [Link] = bankID; [Link] = name; } }
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); [Link]([Link]()); [Link]([Link]()); [Link]([Link]()); } }
ANS: [Link]() can't return balance, that's a private variable in Bank
3) what methods must TrickInterfaces implement?
interface Edible { public Edible eat(int e); }
interface Cookable extends Edible{ public int cookTime(int e); public String dishName(int e); }
interface Compostable extends Edible{ public Edible compost(int c); }
public class TrickInterfaces implements Cookable{ }
ANS: eat(), cookTime, dishName
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(); [Link]([Link](10)); pipe p = (pipe)t; [Link]([Link](10)); [Link] = 50; [Link]([Link](10)); [Link]([Link]); [Link]([Link]()); } }
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)
public static void main(String[] args){
tube t = new tube(); // dynamic type : Tube, uses [Link]() => Output: 12 [Link]([Link](10));
pipe p = (pipe)t; //static type of p : Pipe, dynamic type: Tube. Uses [Link]() => Output: 12 [Link]([Link](10));
//static type of p : pipe, we modify t's Pipe level but his Tube level is unchanged [Link] = 50; //dynamic type : Tube. Uses [Link]() => Output: 12 [Link]([Link](10)); // dot operator uses static vars: => Output: 50 [Link]([Link]); //dynamic type : Tube, uses [Link] => Output: 2 [Link]([Link]()); }