Polymorphism and interfaces QUIZ
QUIZ Definition of Interface Which lines are legal public interface Car { components of an interface? A. private int speed; 1. A B. public void speedUp() {speed++;} 2. B 3. C C. public void applyBrakes(); 4. A, B } 5. A, C 6. B, C 7. A, B, C 8. none 9. I don’t know
QUIZ Definition of Interface Which lines are legal public interface Car { components of an interface? A. private int speed; 1. A B. public void speedUp() {speed++;} 2. B 3. C C. public void applyBrakes(); 4. A, B } 5. A, C 6. B, C 7. A, B, C A: no private component, no instance variables 8. none 9. I don’t know B: no method implemention C: ok Constant also ok: public static final int NUMBEROFWHEELS = 4;
public interface Car { QUIZ public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } polymorphism } public class Porsche { public String getInfo() { return ”Porsche”; } } Are lines A and B legal? 1. Only A 2. Only B 3. Both A and B 4. Neither A nor B 5. I don’t know Car myCar; myCar = new Fiat(); A System.out.println(myCar.getInfo()); myCar = new Porsche(); B System.out.println(myCar.getInfo());
public interface Car { QUIZ public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } polymorphism } public class Porsche { public String getInfo() { return ”Porsche”; } } Are lines A and B legal? 1. Only A 2. Only B Porsche must explicitly 3. Both A and B implement Car 4. Neither A nor B 5. I don’t know It is not enough to have method getInfo() Car myCar; myCar = new Fiat(); A System.out.println(myCar.getInfo()); myCar = new Porsche(); B System.out.println(myCar.getInfo());
public interface Car { QUIZ public void speedUp(); public void applyBrakes(); Implementation of interface } public class Carriage implements Car { private int speed = 0; Which classes are A legal implementations public void applyBrakes() {speed=0;} of the interface? } 1. A public class Fiat implements Car { 2. B private int speed = 0; 3. C B public void speedUp() {speed++;} 4. A, B public void applyBrakes() {speed--;} 5. A, C } 6. B, C public class Porsche implements Car { 7. A, B, C private int speed = 0; 8. none public void speedUp() {speed++;} 9. I don’t know C public void applyTurbo() {speed +=10;} public void applyBrakes() {speed=0;} }
public interface Car { QUIZ public void speedUp(); public void applyBrakes(); Implementation of interface } public class Carriage implements Car { private int speed = 0; Which classes are A legal implementations public void applyBrakes() {speed=0;} of the interface? } 1. A public class Fiat implements Car { 2. B private int speed = 0; 3. C B public void speedUp() {speed++;} 4. A, B public void applyBrakes() {speed--;} 5. A, C } 6. B, C public class Porsche implements Car { 7. A, B, C private int speed = 0; 8. none public void speedUp() {speed++;} 9. I don’t know C public void applyTurbo() {speed +=10;} public void applyBrakes() {speed=0;} A: needs speedUp() } B: ok C: ok (extra methods are ok)
public interface Car { QUIZ public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } polymorphism } public class Porsche implements Car { public String getInfo() { return ”Porsche”; } } What is returned by call ”getInfo” in line A? 1. ”Fiat” 2. ”Porsche” 3. Either ”Fiat” or ”Porsche” 4. Something else 5. We don’t have enough information to tell 6. I don’t know public void test(Car c) { System.out.println( c. getInfo () ); A }
public interface Car { QUIZ public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } polymorphism } public class Porsche implements Car { public String getInfo() { return ”Porsche”; } } What is returned by call ”getInfo” in line A? 1. ”Fiat” 2. ”Porsche” 3: Consider 3. Either ”Fiat” or ”Porsche” test(new Fiat()); 4. Something else versus 5. We don’t have enough information to tell test(new Porsche()); 6. I don’t know 5: There may be additional classes implementing Car public void test(Car c) { interface, say ”Toyota” System.out.println( c. getInfo () ); A }
public interface Car { public String getInfo(); } QUIZ private static class Model implements Car { private String name; Anonymous class public Model(String n) { name = n; } public String getInfo() { return name; } A } ... Car myFiat = new Model (”Fiat”); ... Car myPorsche = new Model (”Porsche”); private static Car makeCar(final String name) { return new Car() { public String getInfo() { return name; } B }; } ... Car myFiat = makeCar(”Fiat”); ... Car myPorsche = makeCar(”Porsche”); Two objects are created from class Model in A – can we use anonymous class(es) in stead of Model ? 1. No, A and B are completely distinct 2. Yes, A and B are essentially equivalent 3. I don’t know
public interface Car { public String getInfo(); } QUIZ private static class Model implements Car { private String name; Anonymous class public Model(String n) { name = n; } public String getInfo() { return name; } A } When the only use ... Car myFiat = new Model (”Fiat”); of class Model is ... Car myPorsche = new Model (”Porsche”); creating objects, then a factory private static Car makeCar(final String name) { method may do the return new Car() { same job using public String getInfo() { return name; } anonymous classes. B }; } ... Car myFiat = makeCar(”Fiat”); ... Car myPorsche = makeCar(”Porsche”); Two objects are created from class Model in A – can we use anonymous class(es) in stead of Model ? 1. No, A and B are completely distinct 2. Yes, A and B are essentially equivalent 3. I don’t know
QUIZ Scope of variable Which of the 3 variables may NOT be used in line ? 1. a public interface Car { public String getInfo(); } 2. b public class Automobile { 3. c private String brand = "Ford"; 4. a, b private static int productionCount = 0; 5. a, c 6. b, c public Car makeCar(String model ) { 7. a, b, c model = model + " "; 8. None return new 9. I don’t know Car() { private int serialNumber = productionCount++; public String getInfo() { return brand + model + serialNumber ; b a c } }; }
a. brand is instance variabel in outer class QUIZ b. model is local variabel in outer class and changes value model = model + " "; so cannot be used Scope of variable c. serialNumber is defined in inner class Which of the 3 variables may NOT be used in line ? 1. a public interface Car { public String getInfo(); } 2. b public class Automobile { 3. c private String brand = "Ford"; 4. a, b private static int productionCount = 0; 5. a, c 6. b, c public Car makeCar(String model ) { 7. a, b, c model = model + " "; 8. None return new 9. I don’t know Car() { private int serialNumber = productionCount++; public String getInfo() { return brand + model + serialNumber ; b a c } }; }
QUIZ Interfaces and polymorphism How many lines result in errors? 1. 2. 3. 4. 5. 6. none 7. I don’t know a) Sad s; b) s = new Sad(); c) s = new Choleric(); d) s.cry(); e) s.laugh();
QUIZ Interfaces and polymorphism How many lines result in errors? 1. 2. 3. 4. 5. 6. none 7. I don’t know a) Sad s; b) s = new Sad(); c) s = new Choleric(); 2 lines with errors: d) s.cry(); e) s.laugh(); b) cannot construct object from interface e) no method ”laugh” in type ”Sad”
Recommend
More recommend