chapter 10 object-oriented...

18
CHAPTER 10 OBJECT - ORIENTED THINKING Dr. Jones Kennesaw State University CS 2302/01 Summer 2015 slides created by Rashaad Jones

Upload: others

Post on 14-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CHAPTER 10OBJECT-ORIENTED

THINKINGDr. Jones

Kennesaw State University

CS 2302/01

Summer 2015

slides created by Rashaad Jones

Page 2: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATION

Copyright © Rashaad Jones 2015 all rights reserved

Page 3: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONKEY TERMS

• Class Creator

• Author of the class (.java and .class file)

• Class User

• Programmer(s) who use classes created by others (e.g., in Java, this is typically done via the import

statement)

• Class Abstraction

• Separation of class implementation from the user of a class

• Class Encapsulation

• Details of implementation are hidden from the class user

• Class Contract

• Collection of methods and fields that are accessible from outside the class

Copyright © Rashaad Jones 2015 all rights reserved

Page 4: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONAPPLICATION

• Software developed in the real world

• Team-based environment

• One person does NOT code the entire program

• In most cases, done in an asynchronous manner and code contributors do not know each other

• Utilize existing libraries

Copyright © Rashaad Jones 2015 all rights reserved

Page 5: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONEXAMPLE

• Create a program where an university academic advisor can keep track of students

enrolled, courses taken by each student, and the GPA of each student

Copyright © Rashaad Jones 2015 all rights reserved

Page 6: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONEXAMPLE: CLASS ABSTRACTION

Course (class file)

• Course Name

• Credit Hours

• Grade

• GPA Calculator

Main (class file)

• Uses Course class files

Copyright © Rashaad Jones 2015 all rights reserved

Page 7: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONEXAMPLE: CLASS CONTRACT

Course

(Creator: Dr. Jones)

_courseName: String

_creditHours: int

_letterGrade: char

//constructors

Course()

Course(String, int, char)

//accessors (getters)

getCourseName(): String

getCreditHours(); int

getLetterGrde(): char

//mutators (setters)

setCourseName(): void

getCreditHours(): void

getLetterGrde(): void

//static

computeGPA(Course []):double

Main (CourseCollectorTool)

(Creator: You)

//static public methods

createCourse(String, int, char): Course

determineGPA(): double

Copyright © Rashaad Jones 2015 all rights reserved

Page 8: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONREVIEW

• The class creator is the ________ of the class.

• The class user creates a class that uses _______ classes. (e.g., in Java, this is typically

done via the import statement).

• Class abstraction is the _________ of class implementation from the user of a class.

• Class encapsulation _____ implementation details from the class user.

• Class contract is a collection of methods and fields that are accessible from _______

the class.

Copyright © Rashaad Jones 2015 all rights reserved

Page 9: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS ABSTRACTION AND ENCAPSULATIONREVIEW

• The class creator is the author of the class.

• The class user creates a class that uses existing classes. (e.g., in Java, this is typically

done via the import statement).

• Class abstraction is the separation of class implementation from the user of a class.

• Class encapsulation hides implementation details from the class user.

• Class contract is a collection of methods and fields that are accessible from outside

the class.

Copyright © Rashaad Jones 2015 all rights reserved

Page 10: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

THINKING IN OBJECTS

Copyright © Rashaad Jones 2015 all rights reserved

Page 11: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

OBJECT ORIENTED PROGRAMMING

• Create a program where an university academic advisor can keep track of students

enrolled, courses taken by each student, and the GPA of each student

• CS 2301 (before week 6)

• All code written in main

• CS 2301 (after week 6)

• All code in written in one java file

Copyright © Rashaad Jones 2015 all rights reserved

Page 12: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

PROCEDURAL PROGRAMMING

…Step NStep 2Step 1

Copyright © Rashaad Jones 2015 all rights reserved

Page 13: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

OBJECT ORIENTED PROGRAMMING

Parent

Child

GrandchildrenSub-Sub

Class

Sub Class

Class Class

Sub Class

Class

Copyright © Rashaad Jones 2015 all rights reserved

Page 14: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

PROCEDURE ORIENTED PROGRAMMINGVS

OBJECT ORIENTED PROGRAMMING

Procedure Oriented Programming

• Divided into small parts (functions or

methods)

• Follows top down approach (sequential

order to statements)

• Best for small and simple programs

• Data sharing cannot be controlled

• Ideal for programs written by one

person

Object Oriented Programming

• Divided into objects

• Code accesses and manipulates objects

• Follows bottom-up approach

• Best for complex programs

• Data sharing can be controlled

• Ideal for programs written by teams

• Reusable

• Manageable

• Evolve to support new functionality

Copyright © Rashaad Jones 2015 all rights reserved

Page 15: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS RELATIONSHIPS

Copyright © Rashaad Jones 2015 all rights reserved

Page 16: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

CLASS RELATIONSHIPSKEY TERMS

Association

• Relationship between objects

Aggregated Object

• A form of association where an object that is owned by another object

Aggregating Object

• A form of association where an object that owns other objects

Composition

• A special association where an object is exclusively owned by an aggregating object

Copyright © Rashaad Jones 2015 all rights reserved

Page 17: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

IMMUTABLE

Copyright © Rashaad Jones 2015 all rights reserved

Page 18: CHAPTER 10 OBJECT-ORIENTED THINKINGksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/doc/Le… · PROCEDURE ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMMING Procedure Oriented

IMMUTABLE

• Immutable objects cannot be changed

once it is set

• Strings are immutable

String s = “Hello World”;

s = “Hello”;

s

Hello World

Hello

Copyright © Rashaad Jones 2015 all rights reserved