Top Banner
Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.
34

Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

Dec 16, 2015

Download

Documents

Elvin Alexander
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

Title Slide

CSC 444

Java Programming

Object Oriented Methodology

By

Ralph B. Bisland, Jr.

Page 2: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

2Internet Concepts

What Is Object Orientation?

• A strategy for organizing systems as collections of interacting objects that combine data and behavior.

• Object Oriented Methodology– Object Oriented Design– Object Oriented Programming– Object Oriented Testing

Page 3: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

3Internet Concepts

Developing Programs

• Procedural Method: Developer concentrates on developing code that mimics the procedure to represent the real world system.

• Object Oriented Method: Developer concentrates on developing representations of real world objects, their actions, and how they relate to each other.

Page 4: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

4Internet Concepts

Objects• An object is a software “bundle” of data

and procedures that act on the data.

• A procedure is called a “method”.

• Objects are the heart of OOM.

• Objects share two common characteristics– State: What an object is currently “doing”– Behavior: What can an object do

Page 5: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

5Internet Concepts

Example Of Objects

• A Lion:– States

• Color• Weight• Tired?• Hungry?

– Behaviors• Sleep• Hunt• Roar

Page 6: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

6Internet Concepts

Another Object

• A Car– States

• Current Speed• Type of Transmission• 2/4 Wheel Drive• Lights On• Current Gear

– Behaviors• Turning• Breaking• Accelerating

Page 7: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

7Internet Concepts

One More

• A Baseball Player– States• Batter• Pitcher

– Behaviors• Batting Average• Earned Run Average

Page 8: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

8Internet Concepts

Pictorial View Of A Class

Methods

Data

Message Response

Page 9: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

9Internet Concepts

Objects

• An object is an instance of a class.• Classes must be instantiated to produce

objects.• Examples: FordMustang = new Car

ChevyCorvette = new Car• Each object usually has different values for

its instance variables.

Page 10: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

10Internet Concepts

The Car Object

BreakAccelerateSteerChange GearsToggle Lights

75 mph4th GearLights On

Methods

Data

Page 11: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

11Internet Concepts

Creating Objects

• To create an individual object, a class must be instantiated.

• Example:myCar=new Car (75mph, 4thgear, lightson)yourCar=new Car (60mph, 1stgrear, lightsoff)

• Use the methods to manipulate the instance variables.myCar.Accelerate(+15)yourCar.Accelerate(-10)

Page 12: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

12Internet Concepts

Encapsulation• The process of packaging an object’s data and

methods together.• Objects consist of a public interface (external) and a

private (internal) section.• The internal portion has more limited visibility than the

external portion.• Safeguards the object against unwarranted external

access.• Can only communicate with the object through its

interface.

Page 13: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

13Internet Concepts

Benefits Of Encapsulation• Implementation Hiding : Protection of the

internal implementation of object. The internal data and methods are the parts of the object hidden from other objects. Major benefit: Pata and methods can be changed without affecting other objects.

• Modularity: An object can be maintained independently of other objects. Also easier to distribute objects through the system.

Page 14: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

14Internet Concepts

Messages

• How software objects interact and communicate with each other.

• Analogy: Two people communicating with each other. Person 1 to person 2: “Come here, please”.

• Sometimes the message must contain additional information.

• This is done through message parameters.

Page 15: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

15Internet Concepts

Example

• Message: Accelerate the car by 15 mph.– The object to receive the message: car

– The name of the action to perform: accelerate

– Any parameters the method requires: 15 mph

• Message passing is accomplished through method calling.

• Messages and methods are synonymous.• Messages can be sent to objects in another

location: Distributed Object

Page 16: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

16Internet Concepts

Classes• A class is a template (or prototype) for an object.

• Analogy: A class is to an object is what a blueprint is to a house -- many houses can be built from the same blueprint. A blueprint outlines the makeup of a house.

• An object is an instantitation of a class.• Classes can not be used by themselves.• There may be many different instances of a class.

Many cars, many lions, etc.

Page 17: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

17Internet Concepts

Classes (ctd)

• Benefits of objects: modularity and information hiding.

• Benefits of classes: reusability.

• Each instance of a class has it’s own state variables, but share methods.

• State variables of the instance of a class are called “instance variables”.

Page 18: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

18Internet Concepts

Inheritance

• What happens if an object is basically the same as other objects, but it has a few slight differences?

• Inherit the basic characteristics from a super class and form a sub class (AKA: parent-child classes).

• Inheritance: The process of creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class.

Page 19: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

19Internet Concepts

Inheritance (ctd)

• When a subclass inherits from a superclass, it inherits all of the superclass state variables and the methods.

• Additional methods and/or state variables can be added to the subclass.

Page 20: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

20Internet Concepts

Example

• Class: car• Basic data– Number of wheels– Number of doors

• Types of Cars– Gas powered

• Fuel tank capacity• MPG

– Electric Powered• Size battery

Page 21: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

21Internet Concepts

Inherited Methods

• Inherited methods can be overridden

• Different code can be supplied in the methods for each subclass.

• Example: Gas powered cars can accelerate faster than electric powered cars, so the acceleration routine might be different.

Page 22: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

22Internet Concepts

Another Example

• Class: Figure

• Rectangle– State Variables: Length, Width–Methods: Calculate_Area

• Circle:– State Variables: Radius–Method: Calculate_Area

Page 23: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

23Internet Concepts

Inheritance Tree

Car

Gas Powered Electric Powered

4-Cylinder 6-Cylinder

Page 24: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

24Internet Concepts

Abstract Classes

• Sometimes it is useful to create superclasses that act purely as templates for more subclasses.

• The superclass serves as nothing more than an abstraction for the common class functionality shared by its subclasses.

• Called abstract classes.

• Abstract classes can not be instantiated - an object can not be created from it.

Page 25: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

25Internet Concepts

Abstract Classes (ctd)

• Reason: Parts of it are yet to be defined.

• Parts of methods have yet to be implemented - abstract methods.

• The actual implementation of the method is defined in the subclass.

Page 26: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

26Internet Concepts

Example

• Acceleration method can not be defined until the acceleration capabilities are known.– How a car accelerates is determined by the type

of engine it has.– Engine type is unknown in the superclass– Child class would implement the specific

acceleration method to reflect the acceleration capabilities of their respective engines.

Page 27: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

27Internet Concepts

Another Example

• Class: Baseball Player

• Subclasses: Batter and Pitcher

• All baseball players have some basic data: Team, Uniform#, Birthdate, etc.

• All baseball players have some basic methods: ComputeAge, TaxComputations, etc.

Page 28: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

28Internet Concepts

Another Example (ctd)

• Batters have some specific data: Atbats, Hits, Homeruns, etc.• Batters have some specific methods:• CalculateBattingAverage, CalculateSluggingPercentage, Etc.• Pitchers have some specific data: NumbOfWins, NumbOfLosses,

InningsPitched, EarnedRuns, etc.• Pitchers have some specific methods: CalculateERA, etc.

Page 29: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

29Internet Concepts

Example (ctd)

BaseballPlayerClass

Player Name, Team NameUniform#, Birthdate

CalculateAgeCalculateTaxes

BatterSubclass

AtBats, Hits, HomeRuns

CalculateBattingAverage,CalculateSP

PitcherSubclass

NumberOfWins,NumberOFLosses,InningsPitched, EarnedRuns

CalculateERA

Page 30: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

30Internet Concepts

Example (ctd)

• It doesn’t make sense to create (instantiate) a Baseball Player object from the Baseball Player class (the abstract class).

• We must create an object of the subclasses, Batter or Pitcher (which inherits from the super class Baseball Player)

Page 31: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

31Internet Concepts

Multiple Inheritance

• Enables a subclass to inherit characteristics from more than one superclass.

• Example:A whale inherits some

characteristics from mammals and some from fish.

Page 32: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

32Internet Concepts

Polymorphism

• Means “having many forms”.• Describes an elegant and relative simple

technique by which a reference that is used to invoke a method can actually invoke different methods at different times, depending upon the nature of the invocation.

• Accomplished by late binding of the class type to the method.

Page 33: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

33Internet Concepts

Example

• Assume a superclass of “figure” which has a method called computeArea.

• Assume several subclasses of “figure”. Examples: Square, Triangle, Circle, etc.

• The method of computing the area of a figure is different for each different type of figure.

Page 34: Title Slide CSC 444 Java Programming Object Oriented Methodology By Ralph B. Bisland, Jr.

34Internet Concepts

Example (ctd)

• Since Square, Triangle, etc. are subclasses of figure, they can be assigned into figure.

• The method computeArea can be passed the value figure. figure = square (associated data) computeArea (figure)

• The correct method for computing the area is then executed because of late binding (binding at run time).