ObjectOriented Programming Concept - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

ObjectOriented Programming Concept

Description:

Objects give you two ways to do these things: Manipulate or inspect its ... The classes are designed for repeated use and become stable over time. Easier design ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 43
Provided by: nurulfa
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming Concept


1
Object-Oriented Programming Concept

2
Concepts of Object Orientation
  • Objects and classes.
  • method
  • Message passing
  • Inheritance

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

4
Messages
  • Software objects interact and communicate with
    each other using messages

Customer object
Account object
Bank Object
5
Using Object
  • Objects give you two ways to do these things
  • Manipulate or inspect its variables.
  • Call its methods.

6
  • 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()

7
(No Transcript)
8
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.

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

10
Generalization hierarchy
  • The relationship among the classes, subclasses,
    and superclasses form a hierarchy.
  • A generalization hierarchy is an object-oriented
    design tool used to show the relationships among
    the classes of objects.

TOOLBARS
BUTTON BARS
MENU BARS
SCROLL BARS
DROP-DOWN MENUS
SHORTCUT MENUS
LIST BOX MENUS
11
  • 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.

12
  • 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.

13
  • 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.

14
  • 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.

15
An Object Structure Diagram
Object Structure Diagram
General Form of an Object Structure Diagram
Object name
Data Record
length number of fields sequence number
Object attributes
read write delete modify
Object methods
16
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 ..

17
Final class
  • 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 . . .

18
Class attributes
  • Attributes hold the state of an object.
  • Eg. BankAccount object might include the
    attributes
  • -accountNumber
  • -accountOwner
  • -overdraftLimit
  • -balance

19
Example class defination
  • public class BankAccount
  • private int accountNumber
  • private String accountOwner
  • private float overdraftLimit
  • private float balance
  • public void deposit(float amount) .
  • public void withdraw( float amount) .
  • public float getBalance()
  • public void increaseOverdraft (float amount)
    .
  • public void decreaseOverdraft (float amount)

Class name
Attributes
Methods
20
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.

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

22
Creating abstract classes
  • To make a class or method abstract we include the
    abstract keyword in the defination. Eg,
  • public abstract class Shape

23
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
  • ..

24
Control Access
  • There are 3 types
  • public
  • private
  • protected

25
  • Private control access
  • A classs private members are accessible
    only in the class methods.
  • Public control access
  • A classs 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 classs protected members can be access by
    the class that define the members and its
    subclass.

26
Exampleprivate
  • 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 100ringgit sen
  • class Aplikasi
  • public static void main ( String args)
  • Wang wang new Wang(5, 20)
  • System.out.println(sen wang.jumlahSen())
    //Ralat

27
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 100ringgit sen
  • class Aplikasi
  • public static void main ( String args)
  • Wang wang new Wang(5, 20)
  • wang.ringgit -100
  • System.out.println(sen wang.jumlahSen())

28
Class Methods (operations)
  • Methods are the operations that an object can
    perform or suffer.
  • BankAccount object might have methods
  • -deposit()
  • -withdraw()
  • -getBalance()
  • -increaseOverdraft()
  • -decreaseOverdraft()

29

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

31
  • 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

32
(No Transcript)
33
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.

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

35
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
36
Inheritance example
Superclass
car
More generalised
Sports car
Estate car
More specialised
Sub-class
37
(No Transcript)
38
(No Transcript)
39
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.

40
PolymorphismExample
SpaceObject draw()
LaserBeam
SpaceShip
Martian
41
The Benefits of Object-Oriented Programming
  • 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.

42
API Specification
  • http//java.sun.com/j2se/1.3/docs/api/index.html
Write a Comment
User Comments (0)
About PowerShow.com