object oriented programming april 13, 2007. problem description “ …wombats are allowed to have...

24
Object Oriented Programming April 13, 2007

Upload: frederica-mcgee

Post on 01-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Object Oriented Programming

April 13, 2007

Problem Description• “ …Wombats are allowed to have

different directions and number of eaten leaves, move, turn, find leaves and eat leaves”

Mapping the world to software

• Objects in the problem domain are mapped to objects in software

011101

10011

11101

0110100

11010

010101

1110101

10101

Object Oriented• Data and operations are grouped together

WombatWorld

Data Encapsulationclass Wombat{ private int direction; private int leavesEaten;

public Wombat() {… }public move() { … }

}

Advantages of Encapsulation

• Protection

• Consistency

• Allows change

Objects and Classes• Classes reflect concepts, objects reflect

instances that embody those concepts.

Dana Jane EmilySarah

womanclassobject

Not Fred

Objects and Classes cont’dClass BankAccountBalanceInterestYTDOwnerAccount_number

Balance:500InterestYTD: 10Owner: MarkAccount_number:

12345

Balance 10,000InterestYTD: 25Owner: MarthaAccount_number:

76543

Operations

MakeDesposit

Transfer

WithDraw

GetBalance

Objects as instances of Classes

• The world conceptually consists of objects• Many objects can be said to be of the same

type or class• My bank account, your bank account, Bill Gates’

bank account …

• We call the object type a class

Instantiation• An Object is instantiated from a Class

BankAccount myAccount;

myAccount = new BankAccount();

Objects and Classes• Class

• Visible in source code

• The code is not duplicated

• Object• Own copy of data• Active in running

program• Occupies memory• Has the set of operations

given in the class

Classification

Mammal

Rodent Primate Cat

Reptile

Animal

Squirrel RabbitMouse

Classification

Wombat Rock

Actor

Inheritance• A class which is a subtype of a more

general class is said to be inherited from it.

• The sub-class inherits the base class’ data members and member functions

Inheritance cont’d• A sub-class has all data members of its

base-class plus its own• A sub-class has all member functions of

its base class (with changes) plus its own

• Look at the wombatWorld -- what Actor functions or methods are used in the Wombat class?

Polymorphism

• Polymorphism comes from the Greek language and means ”many shapes”.

• Overloading• One interface with multiple implementations or

methods. Each method must have a distinct signature.

• Overriding• When a subclass has a method with the same

signature (number, type and order of parameters) as a method in one of its superclasses.

Overloading Example

• One interface with multiple implementations or methods. Each method must have a distinct signature.

• Example: fill()• fill(gray)• fill(gray, alpha)• fill(value1, value2, value3)• fill(value1, value2, value3, alpha)• fill(color)• fill(color, alpha)• fill(hex)• fill(hex, alpha)

Polymorphism

• Parameters • gray int or float: number specifying value

between white and black• alpha int or float: opacity of the fill• value1 int or float: red or hue value• value2 int or float: green or saturation value• value3 int or float: blue or brightness value• color color: any value of the color datatype• hex int: color value in hexadecimal notation (i.e.

#FFCC00 or 0xFFFFCC00)

Overriding : Example

• Look at the wombat class and the actor class: each has an act() method.

• A wombat instance of the Wombat class will use the wombat act().

(Check out the Actor class’s act() method. What does it do?)

What is a good class ?• A class abstracts objects

• A class should be non-trivial in the context of the program (has data structures and operations different from other classes)

Class Design

• Use “is a” or “has a” to determine whether something should be a subclass or be a data item.

• Ex:• A dog has a name. Not: A dog is a name.• A dog is a mammal. Not: A dog has a

mammal. (unless its in predator-mode which isn’t a step in oo design).

Summary• What is Object Oriented Programming?• Object-oriented programming is a method of

implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships

Importance of Documenation

• The Greeps assignment will require you to read the documentation.

• As practice for the Greeps assignment, in class today create the car game found at http://www.greenfoot.org/greenroom/images/c/c2/Car_Game_Tutorial.zip

• The line image isn’t included -- use whatever you want!

• What method has replaced stopSimulation() in Greenfoot? What method can you use to play a sound?

Things to Note

• getX() and getY() are methods of the base class Actor but are used in the Car class.

• World class methods used in Car class include removeObject(), getHeight() and addObject(). They are called by getWorld().method-call Example: getWorld().removeObject(this)