cse 1020:inheritance

31
CSE 1020:Inheritance Mark Shtern 1-1

Upload: donat

Post on 06-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

CSE 1020:Inheritance. Mark Shtern. Course evaluations. Lecture: Tuesday, July 12 Lab: Tuesday, July 12 Both will take place at the beginning of the lecture / lab. Summary. Aggregation Collection Composition Traversing. UML. Definition. C extends P: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 1020:Inheritance

CSE 1020:Inheritance

Mark Shtern

1-1

Page 2: CSE 1020:Inheritance

Course evaluations

• Lecture: Tuesday, July 12• Lab: Tuesday, July 12

• Both will take place at the beginning of the lecture / lab

Page 3: CSE 1020:Inheritance

Summary

• Aggregation• Collection• Composition• Traversing

1-3

Page 4: CSE 1020:Inheritance

UML

1-4

Page 5: CSE 1020:Inheritance

Definition

• C extends P:– Every fields and public method present in P is also

present in C

• Extend is inheritance relation between the child and parent

• C is a P• C is specialization of P and P is generalization

of C

1-5

Page 6: CSE 1020:Inheritance

UML diagram

1-6

Page 7: CSE 1020:Inheritance

1-7

Page 8: CSE 1020:Inheritance

Inheritance

• Inheritance chain• Inheritance hierarchy• Descendant• Java does not allow multiple- inheritance

1-8

Page 9: CSE 1020:Inheritance

Subclass API

• The constructor section• The method section

– Overriding methods– Override vs Overload

• The field section– Shadow Field

1-9

Page 10: CSE 1020:Inheritance

Example 903

• Examine API of the reward card subclass and its supper class, and identify the methods that were– Overridden– Added by subclass

• Provide a rational to justify why these methods were overridden or added

1-10

Page 11: CSE 1020:Inheritance

The substitutability Principle• It says:

– When parent is expected, a child is acceptedoutput.println(“Enter C or P”);char choice = input.nextChar();P x;If (choice ==‘P’){

x = new P();} else {

x = new C();}

1-11

Page 12: CSE 1020:Inheritance

Example 904

• Write a fragment that asks the user whether an ordinary or a reward card is wanted, then create the appropriate card with card number 9 and cardholder name “Adam”

1-12

CreditCard card;If (type.equals(“O”)){ card = new CreditCard(9,”Adam”);} else { card = new RewardCard(9,”Adam”);}

Page 13: CSE 1020:Inheritance

Early and Late Binding

• The compiler performs early binding as before• Assuming no errors, it culminates in a target• VM performs late binding

– Determine the class of the object– If reference null, then a “Null Point Exception” is

thrown– Searches for method with matching signature and

binds with it

1-13

Page 14: CSE 1020:Inheritance

What is expected output

CreditCard card1 = new RewardCard(9, “Adam”);CreditCard card2 = new RewardCard(9, “Adam”);card1.charge(500); card1.pay(500);output.println(card1.isSimilar(card2));

1-14

Page 15: CSE 1020:Inheritance

Polymorphism

CreditCard card;....Card.charge(500.0);• Polymorphic is capable of having multiple

forms• Polymorphism refers to the ability of the same

entity to have multiple forms• instanceof

1-15

Page 16: CSE 1020:Inheritance

Predict output of the following code

CreditCard card1 = new RewardCard(9, “Adam”);CreditCard card2 = new RewardCard(9, “Adam”);card1.charge(500);card1.pay(100);output.println(card1.isSimilar(card2));output.println(card1.isSimilar((RewardCard)card2));output.println((RewardCard)card1.isSimilar(card2));output.println((RewardCard)card1.isSimilar((Rewar

dCard)card2));

1-16

Page 17: CSE 1020:Inheritance

Abstract Class

• Abstract class does not encapsulate an actual object

1-17

Page 18: CSE 1020:Inheritance

Abstract Class

• API public abstract class Vehicle• Factory Method

Vehicle myCar = Vehicle.createCar();

• Subclass constructorVehicle myCar = new Car();

1-18

Page 19: CSE 1020:Inheritance

Example 908

• Create an instance of the type Calendar

1-19

Page 20: CSE 1020:Inheritance

Interface

• Interface retains only– Constants– Contracts of the shared methods

1-20

Page 21: CSE 1020:Inheritance

Interface

• A class contains implementation of all interface methods is said to implement interface

• Implementation is-a relation• A class can implement as many interfaces as

needed

1-21

Page 22: CSE 1020:Inheritance

Object Class

• Boolean equals(Object other)• String toString()• Class getClass()

1-22

Page 23: CSE 1020:Inheritance

Generic

• Generic or Parameterized Type• The generic idea allows an implementer to

write a component that can handle only one type T but without specifying T

Bar<CreaditCard> bag = new Bag<CreaditCard>();

1-23

Page 24: CSE 1020:Inheritance

Summary

• Inheritance, Polymorphism and Substitutability, Binding

• Interfaces, Abstract class• Generic

1-24

Page 25: CSE 1020:Inheritance

Exercise 9.4(modified)

• Determine the API of class Bee in this UML diagram

Page 26: CSE 1020:Inheritance

Exercise 905678

• Based on the inheritance chain determine whether the following fragments have– Compiler-error– Run-time error

• Justify the cast in

1-26

Page 27: CSE 1020:Inheritance

Exercise 920

• The following fragment seeks to invoke toString method of the Object class in order to determine the memory address of a Fraction instanceObject tmp = new Fraction();output.println(tmp.toString());output.println((Object)tmp.toString());

• Predict and Explain the output

1-27

Page 28: CSE 1020:Inheritance

Exercise 922

• Consider the following fragmentFraction x = new Money(2.25);Money y = new Money(5.27);output.println(x.resembles(y));output.println(((MixedNumber)x).resembles(y));output.println(((Money)x).resembles(y));• Predict output

1-28

Page 29: CSE 1020:Inheritance

Same as previous (Exercise 921)Fraction x = new Money(2.25);Fraction xFr = new Fraction(225, 100);

Fraction yFr = new Fraction(527, 100); output.println(xFr.resembles(yFr));

MixedNumber xMi = new MixedNumber(1, 2, 25, 100); MixedNumber yMi = new MixedNumber(1, 5, 27, 100); output.println(xMi.resembles(yMi));

Money xMo = new Money(2.25); Money yMo = new Money(5.27); output.println(xMo.resembles(yMo));

1-29

Page 30: CSE 1020:Inheritance

Exercise 9.16

• Write a program that generates statistics about the return type of the getRandom method of the MixedNumber class

• Invoke 100 times and output the number of times a Fraction or a MixedNumber was returned

• Use instanceof

Page 31: CSE 1020:Inheritance

Exercise 9.17

• Write a program that generates statistics about the return type of the getRandom method of the MixedNumber class

• Invoke 100 times and output the number of times a Fraction or a MixedNumber was returned

• Use getClass