Object Oriented Programming

Download Report

Transcript Object Oriented Programming

Object-Oriented Programming
Concept
Concepts of Object Orientation
Objects and classes.
method
Message passing
Inheritance
What is an object?
An object is..
- anything real or abstract that store data and
operations that manipulate the data.
-a software bundle of variables and related
methods (operations).
-anything that supports the concept of a name.
-an instance of a class.
Messages
Software objects interact and communicate with
each other using messages
Customer
object
Account
object
Bank
Object
Using Object
Objects give you two ways to do these things:
1. Manipulate or inspect its variables.
2. Call its methods.
Referencing an Object's Variables
objectReference.variableName
Eg.
int height = new Rectangle().height;
Calling an Object's Methods
objectReference.methodName(argumentList);
or
objectReference.methodName();
Eg.
rect_two.move(40, 72);
int areaOfRectangle = new Rectangle(100, 50).area();
Classes
A class defines the attributes and operations
common to all objects of a certain kind.
A class is an object or a set of objects that share
a common structure and a common behavior.
A class is created when objects are used
repeatedly.
Each class may have one or more lower levels
called subclasses or one or more higher levels
called superclasses.
A unique object or a specific occurrence of a
class of objects is called an instance.
Classes define:
-attributes (state)
-operations or methods (behavior).
Generalization hierarchy
TOOLBARS
The relationship among
the classes, subclasses,
and superclasses form a
BUTTON MENU SCROLL
hierarchy.
BARS
BARS
BARS
A generalization
hierarchy is an objectoriented design tool used
to show the relationships DROP-DOWN SHORTCUT LIST BOX
MENUS
MENUS
MENUS
among the classes of
objects.
An operation, or service, is an activity that reads
or manipulates the data of an object.
Examples of operations include the standard
mathematical, logical operations, input, output
and storage operations associated with
computer data.
A method is the code used to perform the
operation or service.
For an object to do something, it must receive a
message.
The message defines the interaction of the
object.
Everything an object can do is represented by
the message.
The message has 2 parts:
The name of the object to which the message is
being sent.
The name of the method that will be performed.
The trigger that causes the message to be sent
may come from another object or an external user.
The entire process of a trigger sending a message
that causes an operation to occur is called an
event.
For example, if you click a button to save data, that
action is the trigger. A message is sent to the disk
to prepare for input. Writing the data to the disk is
the operation. Saving is the event.
In object-oriented terminology, the data stored
about an object is referenced by an attribute, or
property.
Attributes are identifying characteristic of
individual objects such as name, size, or color.
Attributes should not be confused with the data
itself.
For example, color is an attribute; red is the data.
An Object Structure Diagram
General Form of an
Object Structure Diagram
Object name
Object
attributes
Object
methods
Object
Structure Diagram
Data Record
length
number of fields
sequence number
read
write
delete
modify
Defining new classes
To define a new Java class we must extend
the class java.lang.Object
eg.
public class NameOfClass extends Object{…}
Or
public class NameOfClass {…..}
Final class


1.
2.
Declares that the class cannot be subclassed.
Two reasons :
to increase system security by preventing
system subversion.
good object-oriented design.
Eg.
final class ChessAlgorithm { . . . }
Class attributes
Attributes hold the state of an object.
Eg. BankAccount object might include the
attributes
-accountNumber
-accountOwner
-overdraftLimit
-balance
Example class defination
public class BankAccount {
private int accountNumber;
private String accountOwner;
private float overdraftLimit;
private float balance;
Class name
Attributes
public void deposit(float amount) {….}
public void withdraw( float amount) {….}
public float getBalance() {…}
public void increaseOverdraft (float amount) {….}
public void decreaseOverdraft (float amount) {…}
}
Methods
Using packages
To use existing classes in another package we
use the import statement.
-import package;
-import package.class;
-import package.*;
Example:
-import java.awt;
-import java.awt.Button;
-import java.awt.*;
Abstract classes
Abstract classes are classes that embody
coherent and cohesive, but incomplete concepts.
Provide starting points for inheritance.
eg. Motor vehicle can be motor car. Motor bike,
motor boat, lorry etc...
Creating abstract classes
To make a class or method abstract we include
the abstract keyword in the defination. Eg,
public abstract class Shape {…}
Subclasses in Java
To create a subclass of a Java class we use the
extends keyword.
We can only subclass one class at a time.( Java
only supports single inheritance).
public class Circle extends Shape{
…..
}
Control Access
There are 3 types:
1. public
2. private
3. protected
Private control access
A class’s private members are accessible only
in the class methods.
Public control access
A class’s public members are accessible
anywhere that the program has a reference to an
object of the class or one of its subclasses.
Protected control access
A class’s protected members can be access by
the class that define the members and its
subclass.
Example:private
class Wang {
private int ringgit, sen;
public Wang(int r, int s) {
ringgit = r;
sen= s;
System.out.println(“Jumlah sen:” + jumlahSen());
}
private int jumlahSen(){
return 100*ringgit + sen;
}
}
class Aplikasi {
public static void main ( String [] args) {
Wang wang = new Wang(5, 20);
System.out.println(“sen: “ + wang.jumlahSen()); //Ralat
}
}
Example: public
class Wang {
public int ringgit, sen;
public Wang(int r, int s) {
ringgit = r;
sen= s;
System.out.println(“Jumlah sen:” + jumlahSen());
}
public int jumlahSen(){
return 100*ringgit + sen;
}
}
class Aplikasi {
public static void main ( String [] args) {
Wang wang = new Wang(5, 20);
wang.ringgit = -100;
System.out.println(“sen: “ + wang.jumlahSen());
}
}
Class Methods (operations)
Methods are the operations that an object can
perform or suffer.
BankAccount object might have methods:
-deposit()
-withdraw()
-getBalance()
-increaseOverdraft()
-decreaseOverdraft()
Method Body
super
If a method hides one of its superclass's member
variables, you can refer to the hidden variable
through the use of the super keyword. Similarly,
if your method overrides one of its superclass's
methods, your method can invoke the overridden
method through the use of the super keyword.
Eg:
class ASillyClass {
boolean aVariable;
void aMethod() {
aVariable = true;
}
class ASillierClass extends ASillyClass {
boolean aVariable;
void aMethod() {
aVariable = false;
super.aMethod();
System.out.println(aVariable);
System.out.println(super.aVariable);
}
}
Output:
false
true
Constructor.
All Java classes have constructors that are used
to initialize a new object of that type. A
constructor has the same name as the class.
Typically, a constructor uses its arguments to
initialize the new object's state. When creating
an object, choose the constructor whose
arguments best reflect how you want to initialize
the new object.
Encapsulation
Encapsulation is the capability of an object to have
data and functionality (methods) available to the user,
without the user having to understand the
implementation within the object.
Encapsulation is the process of hiding the
implementation and programming details of an object
from its user, making those details transparent.
Eg- stack (LIFO), the last item pushed (inserted) on the
stack is the first item popped (removed) from the stack.
The client of a stack class cares about what
functionality a stack offers, but not about how that
functionality is implemented.
Inheritance (Pewarisan)
The process of defining new classes by reusing
the features of existing classes.
Only the differences between the new class and
the existing class needs to be defined.
Can be found by looking for ‘IS-A” relationships
between objects.
Eg.
vehicle
is a
is a
boat
plane
Inheritance example
Superclass
More generalised
car
Sports car
More specialised
Sub-class
Estate
car
Polymorphism
Polymorphism allows an instruction to be given
to an object using a generalized, rather than a
specifically detailed, command.
The same command will get different, but
predictable result depending on the object that
receives the command.
While the specific actions (internal to the object)
are different, the result would be the same.
Polymorphism:Example
SpaceObject
draw()
Martian
SpaceShip
LaserBeam
The Benefits of Object-Oriented
Programming
1.
2.
3.
4.
Reusability
The classes are designed so they can be reused in many
systems, or modified classes can be created using inheritance.
Stability
The classes are designed for repeated use and become stable
over time.
Easier design
The designer looks at each object as a black box and is not as
concerned with the detailed inside.
Faster design
The applications can be created from existing components.
API Specification
http://java.sun.com/j2se/1.3/docs/api/index.html