chapter 10 object-oriented...
TRANSCRIPT
CHAPTER 10OBJECT-ORIENTED
THINKINGDr. Jones
Kennesaw State University
CS 2302/01
Summer 2015
slides created by Rashaad Jones
CLASS ABSTRACTION AND ENCAPSULATION
Copyright © Rashaad Jones 2015 all rights reserved
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
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
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
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
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
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
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
THINKING IN OBJECTS
Copyright © Rashaad Jones 2015 all rights reserved
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
PROCEDURAL PROGRAMMING
…Step NStep 2Step 1
Copyright © Rashaad Jones 2015 all rights reserved
OBJECT ORIENTED PROGRAMMING
Parent
Child
GrandchildrenSub-Sub
Class
Sub Class
Class Class
Sub Class
Class
Copyright © Rashaad Jones 2015 all rights reserved
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
CLASS RELATIONSHIPS
Copyright © Rashaad Jones 2015 all rights reserved
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
IMMUTABLE
Copyright © Rashaad Jones 2015 all rights reserved
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