24
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Embed Size (px)

DESCRIPTION

Object-Oriented Principles OOP Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Virtual functions -- Abstract Base Classes

Citation preview

Page 1: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Object-Oriented Design

Bina RamamurthySUNY at Buffalo

Page 2: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Topics of Discussion OO Principles Class diagrams Program Structure Defining methods Return statements Parameters Defining classes Modifiers Summary

Page 3: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Object-Oriented PrinciplesOOP

Encapsulation(class)-- Information Hiding-- Separation of Interface and Implementation-- Standardization-- Access Control mechanisms (private /public)

Inheritance-- Hierarchy-- Reusability-- Extensibility-- Expressive power-- Reflects many real-world problems

Polymorphism-- Many forms of same function-- Virtual functions-- Abstract Base Classes

Page 4: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Java Class Diagram

Data Declarations

Methods

EXAMPLE :Rectangle

LengthWidthColor

AreaPerimeterDisplay

Page 5: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Examples of class and object

Class : DollObject Reference : Doll MyDoll; ;Object Instantiated: MyDoll = new Doll( ); Semantics (meaning) of “new” : create an object of the class specified and return its reference.

Class : RoseObject References : Rose YourRose, HerRose;Object Instantiated : YourRose = new Rose();One More Object Instantiated: HerRose = new Rose();

Page 6: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Examples

Class Rose

HerRose

YourRose

class

objects Object References

Page 7: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Program Structure

A simple Java application program is a class with at least one method called “main”

main method must always be defined using the words public, static, and void.

main method is where the processing begins in a Java application program.

main contains statements to be executed.File name and a program class name should be

same in the environment you are working.

Page 8: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Method Invocation/CallSyntax:method_name (values); object_name.method_name(values); classname.method_name(values);Examples:YourRose.PaintIt(Red);HerRose.PaintIt(Math.abs(RanGenerator.nextInt());

Page 9: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Defining MethodsA method is group of (related)

statements that carry out a specified function.

A method is associated with a particular class and it specifies a behavior or functionality of the class.

A method definition specifies the code to be executed when the method is invoked/activated/called.

Page 10: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Method Definition : Syntax return_type method_name (parameter_list) { statements}1. return type,2. method_name to call it by3. parameter listThere is one more detail called “access control or

visibility modifiers” associated with methods. We will discuss it later.

Page 11: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Return Typecan be void, type or class identifiervoid indicates that the method called

to perform an action in a self-standing way: Example: println

type or class specify the value returned using a return statement inside the method.

Page 12: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Return StatementSyntax of return statement:return; // for void methodsreturn expression; // for type or class

return value// the expression type and return type

should be same

Page 13: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Parameter List Parameter list specified in method header provides

a mechanism for sending information to a method. It is powerful mechanism for specializing an object. The parameter list that appears in the header of a

method specifies the type and name of each parameter

and is called formal parameter list.

The corresponding parameter list in the method invocation is called an actual parameter list.

Page 14: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Parameter list : SyntaxFormal parameter list: This is like molds or

templates(parm_type parm_name, parm_type

parm_name, ....)Actual parameter list: This is like material

that fit into the mold or template specified in the formal list:

(expression, expression....)

Page 15: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Method Definition : review

return type name parameter list

{ statements }

headerbody

definition

modifiers

Page 16: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Method Definition : ExampleWrite a method that computes and

returns the perimeter of a rectangle class.

Analysis: Send to the method: Length and Width Compute inside the method: Perimeter Return from the method: Perimeter

Page 17: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

...Example (contd.)name : perimeterreturn type : intparameters : int length and int widthcomputation :2 X (length + width)int Perimeter (int Length, int Width){ int Temp; // local temporary variable Temp = 2 * (Length + Width); // compute

//perimeter return Temp; // return computed value}

Page 18: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

What happens when a method is called?Control is transferred to the method

called and execution continues inside the method.

Control is transferred back to the caller when a return statement is executed inside the method.

Page 19: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Method Invocation : semantics

8Main method

Operating System

Rect.Area(….)

Area method

1 2

4

5 6

1. OS to main method2. Main method execution3. Invoke Area4. Transfer control to Area5. Execute Area method6. Return control back to main method7. Resume executing main 8. Exit to OS

37

8

Page 20: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Defining ClassesSyntax:class class_name { data-declarations constructors methods }Constructors are special methods used for

instantiating (or creating) objects from a class.Data declarations are implemented using

variable and constant declarations.

Page 21: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

ConstructorsA Constructor is used to create or instantiate

an object from the class.Constructor is a special method:

It has the same name as the class. It has no return type or return statement.

Typically a class has more than one constructor: a default constructor which has no parameters, and other constructors with parameters.

Page 22: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Visibility Modifiers : private, public Each member of a class whether it is a data

or a method can be given access control or visibility modifier:

private : it means that the item is visible only within the class

public: means that the item is accessible from outside the class thru’ dot notation.

Typically the data members are private, most methods are public.

Page 23: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

Classes and CLASSPATHAll the classes needed for the application are placed

in a file (or files).They can compiled separately.Application using the classes is placed in a file and

compiled.If the classes are located in a different directory than

the application CLASSPATH needs to be set to indicate the directories to search.

CLASSPATH = .:/util/lang/jdk1.1.6/lib/classes.zip:/u0/faculty/bina/cs114a:{absolute path to your class repository}

Page 24: Object-Oriented Design Bina Ramamurthy SUNY at Buffalo

SummaryClass definition and Object

instantiationMethod definition and invocationDesign and implementation of

methods and class