+ All Categories
Home > Documents > 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented...

1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented...

Date post: 19-Dec-2015
Category:
View: 217 times
Download: 2 times
Share this document with a friend
Popular Tags:
34
1 CS1001 CS1001 Lecture 17 Lecture 17
Transcript
Page 1: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

11

CS1001CS1001

Lecture 17Lecture 17

Page 2: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

22

OverviewOverview

Homework 3Homework 3 Project/PaperProject/Paper Object Oriented DesignObject Oriented Design

Page 3: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

33

GoalsGoals

Learn Object-Oriented Design Learn Object-Oriented Design MethodologiesMethodologies

Page 4: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

44

AssignmentsAssignments

Brookshear: Ch 5.5, Ch Brookshear: Ch 5.5, Ch 6.3/6.4, Ch 7 (especially 7.7) 6.3/6.4, Ch 7 (especially 7.7) (Read)(Read)

Read linked documents on these Read linked documents on these slides (slides will be posted in slides (slides will be posted in courseworks)courseworks)

Page 5: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

55

Objectives:Objectives: Review the main OOP concepts: Review the main OOP concepts:

– inheritanceinheritance

– abstractionabstraction– encapsulationencapsulation– polymorphismpolymorphism

Get an appreciation for the Get an appreciation for the complexity of object-oriented complexity of object-oriented design.design.

Page 6: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

66

What are OOP’s claims What are OOP’s claims to fame?to fame? Better suited for team developmentBetter suited for team development Facilitates utilizing and creating Facilitates utilizing and creating

reusable software componentsreusable software components Easier GUI programmingEasier GUI programming Easier program maintenanceEasier program maintenance

Page 7: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

77

OOP in a Nutshell:OOP in a Nutshell: A program models a world of A program models a world of

interacting objects.interacting objects.

Objects create other objects and Objects create other objects and “send messages” to each other (in “send messages” to each other (in Java, call each other’s methods).Java, call each other’s methods).

Each object belongs to a class; a class Each object belongs to a class; a class defines properties of its objects. The defines properties of its objects. The data type of an object is its class.data type of an object is its class.

Programmers write classes (and reuse Programmers write classes (and reuse existing classes).existing classes).

Page 8: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

88

Main OOP Concepts:Main OOP Concepts:

InheritanceInheritance

AbstractionAbstraction

EncapsulationEncapsulation

PolymorphismPolymorphism

Event-driven computationsEvent-driven computations

Page 9: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

99

InheritanceInheritance

A class can A class can extendextend another class, another class, inheriting all its data members and inheriting all its data members and methods while redefining some of methods while redefining some of them and/or adding its own.them and/or adding its own.

A class can A class can implementimplement an interface, an interface, implementing all the specified implementing all the specified methods.methods.

Inheritance implements the “is a” Inheritance implements the “is a” relationship between objects.relationship between objects.

Page 10: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1010

subclass

or

derived class

superclass

or

base class

extends

Inheritance (cont’d) Inheritance (cont’d)

subinterface superinterfaceextends

class interfaceimplements

Page 11: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1111

Inheritance (cont’d)Inheritance (cont’d)

In Java, a subclass can extend only In Java, a subclass can extend only one superclass.one superclass.

In Java, a subinterface can extend In Java, a subinterface can extend one superinterfaceone superinterface

In Java, a class can implement In Java, a class can implement several interfaces — this is Java’s several interfaces — this is Java’s form of form of multiple inheritancemultiple inheritance..

Page 12: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1212

Inheritance (cont’d)Inheritance (cont’d) An abstract class can have code for An abstract class can have code for

some of its methods; other methods are some of its methods; other methods are declared declared abstractabstract and left with no code. and left with no code.

An interface only lists methods but does An interface only lists methods but does not have any code.not have any code.

A concrete class may extend an A concrete class may extend an abstract class and/or implement one or abstract class and/or implement one or several interfaces, supplying the code several interfaces, supplying the code for all the methods.for all the methods.

Page 13: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1313

Inheritance (cont’d)Inheritance (cont’d)

Inheritance plays a dual role:Inheritance plays a dual role:

– A subclass reuses the code from the A subclass reuses the code from the superclass.superclass.

– A subclass (or a class that implements an A subclass (or a class that implements an interface) inherits the interface) inherits the data typedata type of the of the superclass (or the interface) as its own superclass (or the interface) as its own secondary type.secondary type.

Page 14: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1414

Inheritance (cont’d)Inheritance (cont’d) Inheritance leads to a hierarchy of classes Inheritance leads to a hierarchy of classes

and/or interfaces in an application:and/or interfaces in an application:

Game

GameFor2

BoardGame

Chess Backgammon

Solitaire

Page 15: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1515

Inheritance (cont’d)Inheritance (cont’d) An object of a class at the bottom of An object of a class at the bottom of

a hierarchy inherits all the methods a hierarchy inherits all the methods of all the classes above.of all the classes above.

It also inherits the data types of all It also inherits the data types of all the classes and interfaces above.the classes and interfaces above.

Inheritance is also used to extend Inheritance is also used to extend hierarchies of library classes, reusing hierarchies of library classes, reusing the library code and inheriting library the library code and inheriting library data types.data types.

Page 16: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1616

Inheritance (cont’d)Inheritance (cont’d) Inheritance implements the “is a” Inheritance implements the “is a”

relationship.relationship.

Not to be confused with embedding Not to be confused with embedding (an object has another object as a (an object has another object as a part), which represents the “has a” part), which represents the “has a” relationship:relationship:

A sailboat A sailboat is ais a boat boat

A sailboat A sailboat has ahas a sail sail

Page 17: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1717

QuizQuiz

True or False? Inheritance is helpful True or False? Inheritance is helpful for the following:for the following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 18: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1818

AnswerAnswer

True or False? Inheritance is helpful True or False? Inheritance is helpful for the following:for the following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 19: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

1919

AbstractionAbstraction Abstraction means ignoring irrelevant Abstraction means ignoring irrelevant

features, properties, or functions and features, properties, or functions and emphasizing the relevant ones...emphasizing the relevant ones...

... relevant to the given project (with ... relevant to the given project (with an eye to future reuse in similar an eye to future reuse in similar projects).projects).

“Relevant” to what?

Page 20: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2020

Abstraction (cont’d)Abstraction (cont’d) Example from Example from javax.swing:javax.swing:

public abstract class AbstractButtonpublic abstract class AbstractButton

Fields:Fields:protected protected ButtonModelButtonModel model modeletc. etc.

Methods:Methods:void void addActionListeneraddActionListener (ActionListener l); (ActionListener l);String String getActionCommandgetActionCommand();();String String getTextgetText()()etc.etc.

Apply to any button: “regular” button, a checkbox, a toggle button, etc.

The data model that determines the button’s state

Page 21: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2121

Abstraction (cont’d)Abstraction (cont’d)

java.lang.Objectjava.lang.Object

||

+--java.awt.Component+--java.awt.Component

||

+--java.awt.Container+--java.awt.Container

||

+--javax.swing.JComponent+--javax.swing.JComponent

||

+--javax.swing.+--javax.swing.AbstractButtonAbstractButton

Extends features of other abstract and concrete classes

Page 22: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2222

EncapsulationEncapsulation Encapsulation means that all data members Encapsulation means that all data members

((fieldsfields) of a class are declared ) of a class are declared privateprivate. . Some methods may be private, too.Some methods may be private, too.

The class interacts with other classes (called The class interacts with other classes (called the the clientsclients of this class) only through the of this class) only through the class’s constructors and public methods.class’s constructors and public methods.

Constructors and public methods of a class Constructors and public methods of a class serve as the serve as the interfaceinterface to class’s clients. to class’s clients.

Page 23: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2323

Encapsulation (cont’d) Encapsulation (cont’d)

Ensures that structural changes Ensures that structural changes remain remain locallocal::

– Usually, the structure of a class (as Usually, the structure of a class (as defined by its fields) changes more often defined by its fields) changes more often than the class’s constructors and than the class’s constructors and methods.methods.

– Encapsulation ensures that when fields Encapsulation ensures that when fields change, no changes are needed in other change, no changes are needed in other classes (a principle known as “locality”).classes (a principle known as “locality”).

Page 24: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2424

QuizQuiz

True or False? Abstraction and True or False? Abstraction and encapsulation are helpful for the encapsulation are helpful for the following:following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 25: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2525

AnswerAnswer

True or False? Abstraction and True or False? Abstraction and encapsulation are helpful for the encapsulation are helpful for the following:following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 26: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2626

PolymorphismPolymorphism We often want to refer to an object by its We often want to refer to an object by its

primary, most specific, data type.primary, most specific, data type.

This is necessary when we call methods This is necessary when we call methods specific to this particular type of object:specific to this particular type of object:

ComputerPlayer player1 = new ComputerPlayer(); HumanPlayer player2 = new HumanPlayer("Nancy", 8); ... if ( player2.getAge () < 10 ) player1.setStrategy (new Level1Strategy ());

Page 27: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2727

Polymorphism (cont’d)Polymorphism (cont’d) But sometimes we want to refer to But sometimes we want to refer to

an object by its inherited, more an object by its inherited, more generic type:generic type:

Player players[ ] = new Player[2]; players[0] = new ComputerPlayer(); players[1] = new HumanPlayer("Nancy”, 8);

game.addPlayer(players[0]); game.addPlayer(players[1]);

Both ComputerPlayerand HumanPlayer implement Player

Page 28: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2828

Polymorphism (cont’d)Polymorphism (cont’d)

Why disguise an object as a more Why disguise an object as a more generic type?generic type?

– To mix different related types in the To mix different related types in the same collectionsame collection

– To pass it to a method that expects a To pass it to a method that expects a parameter of a more generic typeparameter of a more generic type

– To declare a more generic field To declare a more generic field (especially in an abstract class) which (especially in an abstract class) which will be initialized and “specialized” later.will be initialized and “specialized” later.

Page 29: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

2929

Polymorphism (cont’d)Polymorphism (cont’d) Polymorphism ensures that the Polymorphism ensures that the

appropriate method is called for an appropriate method is called for an object of a specific type when the object of a specific type when the object is disguised as a more generic object is disguised as a more generic type:type:

while ( game.notDone() ) { players[k].makeMove(); k = (k + 1) % numPlayers; }

The appropriate makeMove method is called for all players (e.g., for a HumanPlayer and a ComputerPlayer).

Page 30: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

3030

Polymorphism (cont’d)Polymorphism (cont’d) Good news:Good news: polymorphism is already polymorphism is already

supported in Java — all you have to supported in Java — all you have to do is use it properly.do is use it properly.

Polymorphism is implemented using Polymorphism is implemented using a technique called a technique called latelate (or (or dynamicdynamic) ) method bindingmethod binding: which : which exact method to call is determined exact method to call is determined at run time.at run time.

Page 31: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

3131

OO Software DesignOO Software Design Designing a good OOP application is a Designing a good OOP application is a

daunting task.daunting task.

It is largely an art: there are no precise It is largely an art: there are no precise rules for identifying classes, objects, rules for identifying classes, objects, and methods.and methods.

Many considerations determine which Many considerations determine which classes should be defined and their classes should be defined and their responsibilities.responsibilities.

A bad design can nullify all the potential A bad design can nullify all the potential OOP benefits.OOP benefits.

Page 32: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

3232

OO Design (cont’d)OO Design (cont’d) A few considerations that determine A few considerations that determine

which classes are defined and their which classes are defined and their responsibilities:responsibilities:

– Manageable sizeManageable size– Clear limited functionalityClear limited functionality– Potential reusePotential reuse– Support for multiple objectsSupport for multiple objects– The need to derive from a library classThe need to derive from a library class– The need to make a listener or to implement The need to make a listener or to implement

a particular interfacea particular interface– The need to collect a few data elements in The need to collect a few data elements in

one entityone entity

Page 33: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

3333

Review:Review: Name the main software Name the main software

development concerns that are development concerns that are believed to be addressed by OOP.believed to be addressed by OOP.

Explain the dual role of inheritance.Explain the dual role of inheritance. Can an interface extend another Can an interface extend another

interface? If so, what does it mean?interface? If so, what does it mean? Can an interface extend a class? If Can an interface extend a class? If

so, what does it mean?so, what does it mean? Why do you think Java does not allow Why do you think Java does not allow

a class to extend several classes?a class to extend several classes?

Page 34: 1 CS1001 Lecture 17. 2 Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.

3434

Review (cont’d):Review (cont’d): What is abstraction?What is abstraction? Explain how encapsulation helps in Explain how encapsulation helps in

software maintenance.software maintenance. Why sometimes objects end up Why sometimes objects end up

disguised as objects of more generic disguised as objects of more generic types?types?

What is polymorphism?What is polymorphism?


Recommended