Chapter-9

# Chapter-9 - Exercise 1 public class Circle cfw private int...

This preview shows pages 1–8. Sign up to view the full content.

1 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class Circle { private int radius; public Circle( int r ) { radius = r; } public String toString() { return “Radius: “ + radius ; } } Exercise 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 1 (contd.) import java.util.ArrayList; public class Foo { public static void main( String[] args ) { ArrayList<Circle> circles = new ArrayList<Circle>(); circles .add( new Circle(5) ); for( Circle c : circles ) System.out.println( c ); } } output ??
3 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Fruit +getColor(): String Grape +Grape(r: String) +getColor() : String - region: String public String getColor() { return “Green”; } public String getColor() { if( region.equals(“Florida”)) return “Red”; else return super.getColor (); } Exercise 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 2 (contd.) public class TestMethodOverriding { public static void main( String[] args ) { Grape g1 = new Grape( “Ottawa” ); Grape g2 = new Grape( “Florida” ); System.out.println( g1.getColor() ); System.out.println( g2.getColor() ); } } Output ??
5 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 3 True or False ?? • A Car has an “is-a” relationship with Wheel and  Brake class. • Constructors are not inherited by subclasses. • A superclass’s  private members can be accessed  in the subclass declaration. Answer: false Answer: true Answer: false

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class A1 { public int x; private int y; protected int z; } public class A2 extends A1 { protected int a; private int b; } Exercise 4 public class A3 extends A2 { private int q; } Q) Which of the following is true ? y of class A1 is : a) accessible in A1, A2 and A3 b) accessible in A1 and A2 c) accessible only in A1 d) accessible only in A3 e) not accessible to any of the three classes Answer: (c)
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class A1 { public int x; private int y; protected int z; } public class A2 extends A1 { protected int a; private int b; } Exercise 5 public class A3 extends A2 { private int q; } Q) Which of the following lists of instance variables are accessible in class A2? a) x, y, z, a, b

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 30

Chapter-9 - Exercise 1 public class Circle cfw private int...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online