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.

Transcript of 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.

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

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.

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

5Internet Concepts

Example Of Objects

• A Lion:– States

• Color• Weight• Tired?• Hungry?

– Behaviors• Sleep• Hunt• Roar

6Internet Concepts

Another Object

• A Car– States

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

– Behaviors• Turning• Breaking• Accelerating

7Internet Concepts

One More

• A Baseball Player– States• Batter• Pitcher

– Behaviors• Batting Average• Earned Run Average

8Internet Concepts

Pictorial View Of A Class

Methods

Data

Message Response

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.

10Internet Concepts

The Car Object

BreakAccelerateSteerChange GearsToggle Lights

75 mph4th GearLights On

Methods

Data

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)

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.

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.

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.

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

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.

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”.

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.

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.

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

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.

22Internet Concepts

Another Example

• Class: Figure

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

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

23Internet Concepts

Inheritance Tree

Car

Gas Powered Electric Powered

4-Cylinder 6-Cylinder

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.

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.

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.

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.

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.

29Internet Concepts

Example (ctd)

BaseballPlayerClass

Player Name, Team NameUniform#, Birthdate

CalculateAgeCalculateTaxes

BatterSubclass

AtBats, Hits, HomeRuns

CalculateBattingAverage,CalculateSP

PitcherSubclass

NumberOfWins,NumberOFLosses,InningsPitched, EarnedRuns

CalculateERA

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)

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.

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.

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.

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).