Polymorphism and interfaces QUIZ QUIZ Definition of Interface - - PDF document

polymorphism and interfaces
SMART_READER_LITE
LIVE PREVIEW

Polymorphism and interfaces QUIZ QUIZ Definition of Interface - - PDF document

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


slide-1
SLIDE 1

Polymorphism and interfaces

QUIZ

slide-2
SLIDE 2

QUIZ

public interface Car { A. private int speed; B. public void speedUp() {speed++;} C. public void applyBrakes(); } Which lines are legal components of an interface? 1. A 2. B 3. C 4. A, B 5. A, C 6. B, C 7. A, B, C 8. none 9. I don’t know Definition of Interface

slide-3
SLIDE 3

QUIZ

public interface Car { A. private int speed; B. public void speedUp() {speed++;} C. public void applyBrakes(); } Which lines are legal components of an interface? 1. A 2. B 3. C 4. A, B 5. A, C 6. B, C 7. A, B, C 8. none 9. I don’t know Definition of Interface A: no private component, no instance variables B: no method implemention C: ok Constant also ok: public static final int NUMBEROFWHEELS = 4;

slide-4
SLIDE 4

Car myCar; myCar = new Fiat(); System.out.println(myCar.getInfo()); myCar = new Porsche(); System.out.println(myCar.getInfo());

QUIZ

public interface Car { public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } } 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 polymorphism B A

slide-5
SLIDE 5

Car myCar; myCar = new Fiat(); System.out.println(myCar.getInfo()); myCar = new Porsche(); System.out.println(myCar.getInfo());

QUIZ

public interface Car { public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } } 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 polymorphism Porsche must explicitly implement Car It is not enough to have method getInfo() B A

slide-6
SLIDE 6

QUIZ

public interface Car { public void speedUp(); public void applyBrakes(); } public class Carriage implements Car { private int speed = 0; public void applyBrakes() {speed=0;} } public class Fiat implements Car { private int speed = 0; public void speedUp() {speed++;} public void applyBrakes() {speed--;} } public class Porsche implements Car { private int speed = 0; public void speedUp() {speed++;} public void applyTurbo() {speed +=10;} public void applyBrakes() {speed=0;} }

Which classes are legal implementations

  • f the interface?

1. A 2. B 3. C 4. A, B 5. A, C 6. B, C 7. A, B, C 8. none 9. I don’t know Implementation of interface C B A

slide-7
SLIDE 7

QUIZ

public interface Car { public void speedUp(); public void applyBrakes(); } public class Carriage implements Car { private int speed = 0; public void applyBrakes() {speed=0;} } public class Fiat implements Car { private int speed = 0; public void speedUp() {speed++;} public void applyBrakes() {speed--;} } public class Porsche implements Car { private int speed = 0; public void speedUp() {speed++;} public void applyTurbo() {speed +=10;} public void applyBrakes() {speed=0;} }

Which classes are legal implementations

  • f the interface?

1. A 2. B 3. C 4. A, B 5. A, C 6. B, C 7. A, B, C 8. none 9. I don’t know Implementation of interface A: needs speedUp() B: ok C: ok (extra methods are ok) C B A

slide-8
SLIDE 8

public void test(Car c) { System.out.println( c.getInfo() ); }

QUIZ

public interface Car { public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } } 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 polymorphism A

slide-9
SLIDE 9

public void test(Car c) { System.out.println( c.getInfo() ); }

QUIZ

public interface Car { public String getInfo(); } public class Fiat implements Car { public String getInfo() { return ”Fiat”; } } 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 polymorphism 3: Consider test(new Fiat()); versus test(new Porsche()); 5: There may be additional classes implementing Car interface, say ”Toyota” A

slide-10
SLIDE 10

QUIZ

public interface Car { public String getInfo(); } private static class Model implements Car { private String name; public Model(String n) { name = n; } public String getInfo() { return name; } } ... 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; } }; } ... 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 Anonymous class B A

slide-11
SLIDE 11

QUIZ

public interface Car { public String getInfo(); } private static class Model implements Car { private String name; public Model(String n) { name = n; } public String getInfo() { return name; } } ... 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; } }; } ... 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 Anonymous class When the only use

  • f class Model is

creating objects, then a factory method may do the same job using anonymous classes. B A

slide-12
SLIDE 12

Which of the 3 variables may NOT be used in line ?

  • 1. a
  • 2. b
  • 3. c
  • 4. a, b
  • 5. a, c
  • 6. b, c
  • 7. a, b, c
  • 8. None
  • 9. I don’t know

public interface Car { public String getInfo(); } public class Automobile { private String brand = "Ford"; private static int productionCount = 0; public Car makeCar(String model) { model = model + " "; return new Car() { private int serialNumber = productionCount++; public String getInfo() { return brand + model + serialNumber; } }; }

a b c

QUIZ

Scope of variable

slide-13
SLIDE 13

Which of the 3 variables may NOT be used in line ?

  • 1. a
  • 2. b
  • 3. c
  • 4. a, b
  • 5. a, c
  • 6. b, c
  • 7. a, b, c
  • 8. None
  • 9. I don’t know

public interface Car { public String getInfo(); } public class Automobile { private String brand = "Ford"; private static int productionCount = 0; public Car makeCar(String model) { model = model + " "; return new Car() { private int serialNumber = productionCount++; public String getInfo() { return brand + model + serialNumber; } }; }

a b c

QUIZ

Scope of variable

  • a. brand is instance variabel in outer class
  • b. model is local variabel in outer class and changes value

model = model + " "; so cannot be used

  • c. serialNumber is defined in inner class
slide-14
SLIDE 14

QUIZ

a) Sad s; b) s = new Sad(); c) s = new Choleric(); d) s.cry(); e) s.laugh();

How many lines result in errors? 1. 2. 3. 4. 5. 6. none 7. I don’t know Interfaces and polymorphism

slide-15
SLIDE 15

QUIZ

a) Sad s; b) s = new Sad(); c) s = new Choleric(); d) s.cry(); e) s.laugh();

How many lines result in errors? 1. 2. 3. 4. 5. 6. none 7. I don’t know Interfaces and polymorphism 2 lines with errors: b) cannot construct object from interface e) no method ”laugh” in type ”Sad”