object-oriented programming conceptsarcs.skku.edu/pmwiki/uploads/courses/swpractice1/02-oop.pdf ·...

15
Object-Oriented Programming Concepts ARCS Lab.

Upload: others

Post on 18-Apr-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

Object-Oriented Programming Concepts

ARCS Lab.

Page 2: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

2

Page 3: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is an Object?

Object are understanding object-oriented technology Look around and you’ll find examples of real-world

objrct:► your dog► your desk► your television set► your bicycle

Real-world object share two characteristics:► state► behavior

3

Page 4: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is an Object?

4

State• name• color• breed• hungry

Behavior• barking• fetching• wagging tail

State• current gear• current pedal

cadence• current speed

Behavior• changing gear• changing pedal

cadence• applying breaks

Page 5: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is an Object?

For each object that you see, ask yourself two questions:► What possible states can this object be in?► What possible behavior can this object perform?

5

Page 6: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is an Object?

Software objects are conceptually similar to real-world objects:► They are too consist of state and related behavior► State →Variables► Behavior → Mehtods

6

Methods(behavior)

Variables(state)

Page 7: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is an Object?

Bunding code into individual software object provides a number of benefits, including:► Modularity► Information-hiding► Code re-use► Pluggability and debugging ease

7

Page 8: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is a Class?

In the real world, you’ll often find many indifidual objects all of the same kind► There may be thousands of other bicycles in existence, all of

the same make and model► Each bicycle was built from the same set of blueprints and

therefore contains the same components

8

Page 9: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is a Class?

In object-oriented terms, we say that your bicycle is an instance of the class of objects

9

Page 10: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

10

class Bicycle { int cadence = 0; int speed = 0; int gear = 1;

void changeCadence(int newValue) { cadence = newValue;

} void changeGear(int newValue) {

gear = newValue; } void speedUp(int increment) {

speed = speed + increment; } void applyBrakes(int decrement) {

speed = speed - decrement; } void printStates() {

System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);

} }

Page 11: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

11

class BicycleDemo { public static void main(String[] args) {

// Create two different bicycle objectsBicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle();

// Invoke methods on those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();

bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.changeGear(3); bike2.printStates();

} }

Page 12: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is Inheritance?

Different kinds of objects often have a certain amount in common with each other► Mountain bikes, road bikes, and tandem bikes

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes

12

Page 13: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is Inheritance?

13

Bicycle

Mountain Bike Road Bike Tandem Bike

Superclass

Subclass

Page 14: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

What Is Inheritance?

This gives MountainBike all the same fields and methods as Bicycle

14

class MountainBike extends Bicycle {

// new fields and methods defining// a mountain bike would go here

}

class MountainBike extends Bicycle {

// new fields and methods defining// a mountain bike would go here

}

Page 15: Object-Oriented Programming Conceptsarcs.skku.edu/pmwiki/uploads/Courses/SWPractice1/02-OOP.pdf · 2012-03-12 · Object-oriented programming allows classes to inherit commonly used

Q & A

15