32
More Inheritance 7/2/2009

More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Embed Size (px)

DESCRIPTION

equals(Object o) AnimalDogDalmatianPoodleCat Did you understand Wednesday’s lab?

Citation preview

Page 1: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

More Inheritance

7/2/2009

Page 2: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Impo

rtan

t Dat

es• Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab

• Review Session– Sunday 7/05/2009 – 306 Soda 1-4pm

• Midterm 1– Tuesday 7/07/2009 – 10 Evans 5-6pm– Covers everything through Monday’s lab

• Project 2 released– Thursday 7/09/2009

• Project 1 due– Monday 7/13/2009 – 10pm

Page 3: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

equals(Object o)• Did you understand

Wednesday’s lab?

Page 4: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Animal a1 = new Dog();Animal a2 = new Dog();Dog d1 = new Dog();a1.sniff(d1);d1.sniff(a1);a1.sniff(a2);

← Uses a1’s dynamic type← Uses a1’s static type← Uses a1’s dynamic type

Uses a2’s static type

Page 5: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Inheritance

• Compilation:– CALLER: It makes sure that the static type of the

object has the appropriate method– ARGS: It makes sure that the method takes in the

static type of the arguments• Run-time:– CALLER: When you call a method on an object it

looks for the method starting at the object’s dynamic type

– ARGS: When you pass an object as an argument, it looks for a method with that static type

Page 6: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Dog

agebites

0true

main

Animal a1

Animal a2

Dog d1

Animal a1 = new Dog();Animal a2 = new Dog();Dog d1 = new Dog();a1.sniff(d1);d1.sniff(a1);a1.sniff(a2);

Dog

agebites

0true

Dog

agebites

0true

Page 7: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

What about equals(Object o)

• ArrayList was using an Object reference

Object o1 = new Animal();Object o2 = new Animal();o1.equals(o2);• If others will call your methods with more

generic references, you want to provide a method that takes in an Object

Page 8: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Regular Inheritance (extends)

Page 9: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Animals

PetsEat Bananas

Page 10: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Interfaces

• I have a GREAT idea! • Everyone will want to

make Pets that can – eatKibble()– sitOnLap()

• How?!?!– I don’t care!!!!

Page 11: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Animals

PetsEat Bananas

Page 12: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

The Pet interface

Page 13: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Dog can implement the Pet interface

Page 14: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Cat can implement the Pet interface

Page 15: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

We can have a Pet remote control(Pet reference)

Pet p1 = new Dog();Pet p2 = new Cat();p1.eatKibble();p2.sitOnLap();

Page 16: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Can we have a Pet object?

Pet p1 = new Pet();

• No! You can’t create an object of an interface!

Page 17: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Interfaces can be cool!!!

Page 18: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Example

• The sort method of the Array class promises to sort an array of objects, but under one condition: the objects in the array must implement the Comparable interface:

public interface Comparable {int compareTo(Object other);

}

Page 19: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Summary of Interfaces

• Interfaces don’t implement ANY methods– Just put a semicolon at the end of the method

• Classes can implement multiple interfaces• To implement an interface you must write all of

the methods that the interface defines

Page 20: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Cookies!

Page 21: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Abstract Classes (less lazy than Interfaces)

• I have a GREAT idea! • Everyone will want to make Cookies that can

have these methods:– ingredients()– isDelicious()

• How?!?!– isDelicious() is pretty simple, I’ll write that one– ingredients() ?!? Sounds too hard! I’ll make

that abstract

Page 22: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Abstract Class Cookie

Page 23: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

ChocolateChipCookie can extend the Abstract class Cookie

Page 24: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

GirlScoutCookie can extend the Abstract class Cookie

Page 25: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

We can have a Cookie remote control

(Cookie reference)

Page 26: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Can we have a Cookie object?

Cookie p1 = new Cookie();

• No! You can’t create an object of an abstract class!

Page 27: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Abstract Class Summary• Label an Abstract class as abstract• Label any methods that you don’t want to

implement as abstract– Your children MUST write all of the abstract

methods• Instance variables in the abstract class will be

available in the child class• You can only extend one Class (abstract or

otherwise)

Page 28: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm
Page 29: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

How to do Pair Programming?• Share one computer with your partner. • The “Driver” types and uses the mouse.• The “Navigator” watches, discusses and

focuses on the big picture. • Take turns (about every 30 minutes) to switch

roles.

Page 30: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Why to do Pair Programming?• Writing code is easy compared to making code work!– The bugs are the part that takes a long time

• Clearer programs, better designs, fewer bugs.• Great to talk about at an interview!• “Overcoming difficult problems”• Research Study – Error free code went from 70% to

85% with pairs. So 30% to 15% or a 50% reduction in bugs.

Source: Wikipedia: Pair Programming

Page 31: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm
Page 32: More Inheritance 7/2/2009. Important Dates Project 1 - Check-off – Thursday 7/02/2009 ready BEFORE lab Review Session – Sunday 7/05/2009 – 306 Soda 1-4pm

Working with Partners

• Use pair programming!• Talk about other commitments– Travel plans– Other classes– Jobs

• Assume that if you haven’t seen their code – it doesn’t exist!

• Talk to your partner the moment you get stuck