Object Oriented Programming in Java Object Oriented Concepts

Object Oriented Programming in Java Object Oriented Concepts 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 1

What is an Object? • An object is a software bundle of related variables and methods. • Software objects are often used to model realworld objects you find in everyday life 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 2

State and Behavior • The “State” of an object is represented by the values of its variables • The Behavior of an object is represented by its methods 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 3

Encapsulation • Not a new idea. Examples are: – DLLs in Windows – Procedure in Pascal – Functions in C • Important because of the need to: – keep system modular – promote information hiding 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 4

Controlling Access to Objects State and Behaviors • Java has a number of built-in mechanism to do so using simple keywords such as: – private – public – protected 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 5

What is a Message • Software objects communicate between each other by sending messages to each other. 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 6

Parts of a Message • Destination – object name • Name of behavior to exercise – object method • How to modify behavior and state – parameters 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 7

Why are messages so great? • Allows hiding of internal data structures • Allows hiding on internal process logic • Allows remote calls 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 8

What is a Class? • A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind • Different objects can have the same class – Cars, Vans, Trucks are all objects of the class automobile – Are automobiles different from vehicles? 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 9

Differences between Object and Classes • Source of confusion because of several definitions • In general: – Objects are instances of their respective class 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 10

What are the possible relationships between objects • “Is-a” relationship – implements the behavior of some type • “Inheritance” relationship – subclass of some other object • “Is made of” relationship – declares other objects 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 11

Inheritance • Key concept in OO programming • Very powerful • To be used very carefully • Java enforces a single inheritance model • can inherit State and Behavior from super class • can override or add to super class state and behavior 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 12

Interfaces • • Very powerful Difficult to understand where to use at first To be used a lot Allows to: – capture similarities between classes without artificially forcing a relationship 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 13

Modeling Example • Write an OO program that simulates a furnace, a thermostat, and a user. • Example behaviors and states: – First thermostat gets the set point from the user. Then thermostat get the current temperature from a thermometer. – Then thermostat tests the current temperature against the set point. If the current temperature is above the set point, thermostat does not to turn the furnace on (or turns it off if already on) – When the furnace is turned on it announces that it has been turned on. 12/17/2021 Object Oriented Programming in Java (95 -707) Object oriented Concepts 14
- Slides: 14