Date post: | 18-Jan-2018 |
Category: |
Documents |
Upload: | sibyl-powers |
View: | 220 times |
Download: | 0 times |
Object-Oriented Design
Bina RamamurthySUNY at Buffalo
Topics of Discussion OO Principles Class diagrams Program Structure Defining methods Return statements Parameters Defining classes Modifiers Summary
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
Java Class Diagram
Data Declarations
Methods
EXAMPLE :Rectangle
LengthWidthColor
AreaPerimeterDisplay
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();
Examples
Class Rose
HerRose
YourRose
class
objects Object References
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.
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());
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.
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.
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.
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
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.
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....)
Method Definition : review
return type name parameter list
{ statements }
headerbody
definition
modifiers
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
...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}
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.
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
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.
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.
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.
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}
SummaryClass definition and Object
instantiationMethod definition and invocationDesign and implementation of
methods and class