17
Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1

Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Introduction to Object-oriented Program Design

B. Ramamurthy

1/27/2013 1

Page 2: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 2

Object-Oriented Principles

OOP

Encapsulation (class) -- Information Hiding -- Interface and Implementations -- Standardization -- Access Control mechanisms (private /public etc.)

Inheritance

-- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems

Polymorphism

-- Many forms of same function

Page 3: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 3

What is an Object?

• Object-oriented programming supports the view that programs are composed of objects that interact with one another.

• How would you describe an object?

• Using its characteristics (has a ----?) and its behaviors (can do ----?)

• Object must have unique identity (name) : Basketball, Blue ball

• Consider a ball:

– Color and diameter are characteristics (Data Declarations)

– throw, bounce, roll are behaviors (Methods)

Page 4: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 4

Classes are Blueprints

• A class defines the general nature of a collection of objects of the same type.

• The process creating an object from a class is called instantiation.

• Every object is an instance of a particular class.

• There can be many instances of objects from the same class possible with different values for data.

Page 5: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 5

Example

class Rose

blueRose

redRose

class

objects Object References

Page 6: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Bouquet Class

Many instances of real bouquet objects of this Class can be instantiated

1/27/2013 6

Page 7: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Greenfoot Project controls

World

Class diagram

Execution controls

Greenfoot is a 2D environment to develop Java programs

Greenfoot interface

1/27/2013 7

Book scenarios available at http://www.greenfoot.org/book/

Page 8: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Worlds and Actors

• Greenfoot has two main classes

– World • Place to hold and display actors

• Has a width and height and cell size

– Of type integer (int)

• Has a background image

– Actor • Actors know how to act

• Actors have a x and y location in the world

1/27/2013 8

Page 9: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Objects and Classes

• Classes define what objects know and can do

– There is one Balloon class

• Objects do the action

– There can be many objects of the same class

– There are many balloon objects

Classes Objects

1/27/2013 9

Page 10: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 10

Elements of a Class

class

header methods data declarations (variables, constants)

header body

variables, constants

statements modifiers, type, name

parameters

selection repetition

assignment

others

Page 11: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 11

Class Structure

class

variables

constants

methods

Page 12: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 12

Defining Classes

• Syntax:

• class class_name {

• data-declarations

• constructors

• methods }

• Constructors are special methods used for instantiating (or creating) objects from a class.

• Data declarations are implemented using variable and constant declarations.

Page 13: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 13

Operator new and “dot”

• new operator creates a object and returns a reference to that object.

• After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you have access permissions).

Page 14: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Interacting with objects: methods

• void move()

• void turnLeft()

1/27/2013 14

method Return type

Page 15: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

1/27/2013 15

Instantiation : Examples

• new Wombat()

• new Leaf()

constructor

Page 16: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

Parameters

• void setSize (int size)

1/27/2013 16

parameter

Page 17: Introduction to Object-oriented Program Designbina/cse113/spring2013/OO... · Introduction to Object-oriented Program Design B. Ramamurthy 1/27/2013 1 . 1/27/2013 2 Object-Oriented

10 things

1. Greenfoot interface 2. Class 3. Object 4. World class 5. Actor class 6. Instantiate an object 7. Methods and data (defining a class) 8. Return type 9. Parameters 10. OOP (Object oriented principles)

1/27/2013 17