2.oop concept

57
Endina Putri Purwandari, M.Kom

Upload: robbie-akachopa

Post on 05-Dec-2014

170 views

Category:

Education


1 download

DESCRIPTION

Materi Kuliah Pemrograman Beorientasi Object (PBO) - Teknik Informatika Universitas Bengkulu @2013/2014 - Endina Putri Purwandari, S.T., M.Kom - by:akachopa [ www.akachopa.com ]

TRANSCRIPT

Page 1: 2.oop concept

Endina Putri Purwandari, M.Kom

Page 2: 2.oop concept

OOP Concepts : Illustration Sending Flowers to a Friend Suppose I wish to send flowers to a friend, Sally, who

lives in a city many miles away. ◦What should I do?

Page 3: 2.oop concept

Agents and Communities Solution: Find an appropriate agent, namely Flora, and

pass to her a message containing my request It is the responsibility of Flora to satisfy my request There is some method–some algorithm or some set of

operations –used to satisfy my request This information, i.e., details, is usually hidden from my

inspection.

Page 4: 2.oop concept

The community of agents helping me

Page 5: 2.oop concept

An observation An object-oriented program is structured as a

community of interacting agents, called objects. Each object has a role to play. Each object provides a service, or performs an action, that

is used by other members of the community.

Page 6: 2.oop concept

Messages and Methods Action is initiated in OOP by the transmission of a

message to an agent (an object) responsible for the action. The message encodes the request for an action and is

accompanied by any additional information (arguments) needed to carry out the request.

The receivers the object to whom the message is sent. If the receiver accepts the message, it accepts the responsibility to carry out the indicated action. In response to a message, the receiver will perform some

method to satisfy the request.

Page 7: 2.oop concept

Classes and Instances We can use the term Florist to represent the category (or

class) of all florists. I am able to make certain assumptions because I have

information about florists in general, and I expect that Flora, being an instance of this category, will fit the general pattern.

All objects are instances of a class The method invoked by an object in response to a

message is determined by the class of the receiver All objects of a given class use the same method in

response to similar messages.

Page 8: 2.oop concept
Page 9: 2.oop concept
Page 10: 2.oop concept

The principle that knowledge of a more general category is also applicable to a more specific category is called inheritance. The class Floristwill inherit attributes of the class (or

category) Shopkeeper. Classes can be organized into a hierarchical inheritance

structure. A child class(or subclass) will inherit attributes from a

parent class higher in the hierarchy. An abstract parent class is a class for which there are no direct instances; it is used only to create subclasses.

Page 11: 2.oop concept

That Liz and my florist Flora will respond to my message by different methods is an example of one form of Polymorphism.

◦That I do not, need not, know exactly what method Flora will use to honor my message is an example of information hiding.

Page 12: 2.oop concept

Java: Good and Bad Good:

Platform independent execution Platform independent binary data (files etc) Robust Does not allow operator overloading. Some people regard this as a

limitation. Others think operator overloading is not a good idea anyway! Comes with a huge class library which allow:

File input / output Graphics Event trapping / handling 3D modelling

Bad: Syntax is adopted from C. This means that some control structures are

primitive and unstructured Graphics library still provides problems across different platforms

Page 13: 2.oop concept

OO Characteristics 1. Everything is an object. 2. A program is a bunch of objects telling each other what

to do by sending messages. 3. Each object has its own memory made up of other

objects. 4. Every object has a type. 5. All objects of a particular type can receive the same

messages

Page 14: 2.oop concept

Everything is an object. Think of an object as a fancy variable; it stores data, but

you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.

Page 15: 2.oop concept

A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to

that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object.

Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making

a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects.

Page 16: 2.oop concept

Every object has a type. Using the parlance, each object is an instance of a class, in which

“class” is synonymous with “type.” The most important distinguishing characteristic of a class is: “What messages can you send to it?”

All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because

an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.

Page 17: 2.oop concept

To be OO or not to be OO? There are two basic paradigms for designing algorithms: Non Object Oriented:

Focus is on the steps required to perform the task. The design of the steps lead to the types of data structures

that will be required. Object Oriented:

Focus is on the entities required. i.e. what are the things that need to be represented in the algorithm and:

what functionality will each thing require. how these things will communicate with each other.

◦Each entity will be represented as an object.

Page 18: 2.oop concept
Page 19: 2.oop concept

Small Program Example

Page 20: 2.oop concept

Creating, Compiling, and Running a Java Program The Java code on the previous slide is entered into a text file

using a text editor The name of the .java file MUST be EXACTLY the same as the

name of the class (ie. Welcome1.java) The .java file is then compiled into byte code:

The command would be: javac Welcome1.java If the program contained errors then error messages are

displayed Otherwise the byte code is produced

The byte code is stored in a file called Welcome1.class The program can then be executed using the command: java Welcome1

Page 21: 2.oop concept

The main() Method A Java program starts by executing the main() method The main() method provides the starting point for the

program Within the main() method:

Other methods will be invoked Objects will be created and Methods within those objects invoked

Simple

Page 22: 2.oop concept

Simple Application

Page 23: 2.oop concept

Application Structure and Elements

Page 24: 2.oop concept

Constructors Classes have a special method called a constructor that is

called when a class instance is created. ◦The class constructor always has the same name as the

class and no return type. ◦If you do not write your own constructor, the compiler

adds an empty constructor, which calls the no-arguments constructor of its parent class.

◦The empty constructor is called the default constructor. ◦The default constructor initializes all non-initialized

fields and variables to zero.

Page 25: 2.oop concept

Introduction to Methods A method consists of

A header A body

The header consists of The type of the method The name of the method The parameters to the method

The body consists of a sequence of Java statements encapsulated by { and }

Page 26: 2.oop concept

Methods A method can be called only for an object, and that

object must be able to perform that method call. Call a method:

objectName.methodName(arg1, arg2, arg3); The act of calling a method is commonly referred to as

sending a message to an object boolean flag() { return true; } float naturalLogBase() { return 2.718f; } void nothing() { return; } void nothing2() {}

Page 27: 2.oop concept

Fields

Page 28: 2.oop concept

Fields

Page 29: 2.oop concept

Simple HelloWorld!

Page 30: 2.oop concept

Applet

Page 31: 2.oop concept

Applet (Sample Code)

Page 32: 2.oop concept
Page 33: 2.oop concept

Run the Applet

Page 34: 2.oop concept

Object Oriented Technology Object-oriented design (OOD)

◦Models real-world objects ◦Models communication among objects ◦Encapsulates attributes and operations (behaviors) Information hiding Communication through well-defined interfaces

Object-oriented language ◦Programming in object-oriented languages is called

object-oriented programming (OOP) ◦Java

Page 35: 2.oop concept

Introduction to Class Diagram A class diagram shows the existence of classes and their

relationships in the logical view of a system

Page 36: 2.oop concept

Control Structures Two basic types of control structures: Selection: Given one or more possible choices: choose which

section (if any) of an algorithm to execute. Iteration: Repeat a section of an algorithm provided required

conditions are met. Also known as looping. Selection Control:

The If−Then_Else statement. Provides up to two possible alternatives.

The Case statement. Provides any number of possible alternatives.

Repetition Control: while do…while for

Page 37: 2.oop concept

Exercise

Page 38: 2.oop concept

If-Then-Else: Java Implementation

Page 39: 2.oop concept

Conditional Operator (? : ) Java’s only ternary operator (takes three operands) ? : and

its three operands form a conditional expression Entire conditional expression evaluates to the second

operand if the first operand is true Entire conditional expression evaluates to the third

operand if the first operand is false

Page 40: 2.oop concept

Conditional Operator (? : )

Page 41: 2.oop concept

switch Multiple Selection Statement When the expression matches a case, the statements in

the case are executed until: A break statement is encountered or The end of the switch statement is encountered

The default clause is optional If the default clause is supplied then it is executed if the

switch expression does not match any of the case constants

Page 42: 2.oop concept

switch Multiple Selection Statement

Page 43: 2.oop concept

Mistakes are easy with switch

Page 44: 2.oop concept

Corrected Version

Page 45: 2.oop concept

break and continue Statement

Page 46: 2.oop concept

break Statement

Page 47: 2.oop concept

continue Statement

Page 48: 2.oop concept

Object Creation

Page 49: 2.oop concept

Primitive vs Reference Variables

Page 50: 2.oop concept

Initialization

Page 51: 2.oop concept

Constructors

Page 52: 2.oop concept

The Constructor

Page 53: 2.oop concept

The Java String Class

Page 54: 2.oop concept

Rules for Identifiers

Page 55: 2.oop concept

Guidelines for Identifers

Page 56: 2.oop concept
Page 57: 2.oop concept