0% found this document useful (0 votes)
56 views

1) What's The Error?

The document contains examples of inheritance in Java with errors and questions about the output. 1) The first example shows a CuckooClock class extending the Clock class but missing a call to the super constructor in CuckooClock. 2) The second example shows a BankBranch class extending Bank but getting a private field from the parent class, which causes an error. 3) The third question asks which methods the TrickInterfaces class must implement from the Cookable interface. The rest of the document provides examples of inheritance and polymorphism with questions about the static and dynamic types and output.

Uploaded by

gh269
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

1) What's The Error?

The document contains examples of inheritance in Java with errors and questions about the output. 1) The first example shows a CuckooClock class extending the Clock class but missing a call to the super constructor in CuckooClock. 2) The second example shows a BankBranch class extending Bank but getting a private field from the parent class, which causes an error. 3) The third question asks which methods the TrickInterfaces class must implement from the Cookable interface. The rest of the document provides examples of inheritance and polymorphism with questions about the static and dynamic types and output.

Uploaded by

gh269
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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); } }

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){ this.balance = balance; this.bankID = bankID; this.bankName = 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); System.out.println(b.getBalance()); System.out.println(b.getBankID()); System.out.println(b.getBankName()); } }

ANS: BankBranch.getBalance() 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(); 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)

public static void main(String[] args){

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()); }

You might also like