ObjectOriented Programming Concept Concepts of Object Orientation Objects

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 Objects give you two ways to do these things: 1. Manipulate or inspect its variables. 2. Call its methods.

Referencing an Object's Variables object. Reference. variable. Name Eg. int height = new Rectangle(). height; Calling an Object's Methods object. Reference. method. Name(argument. List); or object. Reference. method. Name(); Eg. rect_two. move(40, 72); int area. Of. Rectangle = 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 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 TOOLBARS BUTTON BARS MENU BARS SCROLL BARS DROP-DOWN SHORTCUT LIST BOX MENUS

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

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

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 Name. Of. Class extends Object{…} Or public class Name. Of. Class {…. . }

Final class n n 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 Chess. Algorithm {. . . }

Class attributes Attributes hold the state of an object. Eg. Bank. Account object might include the attributes -account. Number -account. Owner -overdraft. Limit -balance

Example class defination public class Bank. Account { private int account. Number; private String account. Owner; private float overdraft. Limit; private float balance; Class name Attributes public void deposit(float amount) {…. } public void withdraw( float amount) {…. } Methods public float get. Balance() {…} public void increase. Overdraft (float amount) {…. } public void decrease. Overdraft (float amount) {…} }

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

Example: private class Wang { private int ringgit, sen; public Wang(int r, int s) { ringgit = r; sen= s; System. out. println(“Jumlah sen: ” + jumlah. Sen()); } private int jumlah. Sen(){ return 100*ringgit + sen; } } class Aplikasi { public static void main ( String [] args) { Wang wang = new Wang(5, 20); System. out. println(“sen: “ + wang. jumlah. Sen()); //Ralat } }

Example: public class Wang { public int ringgit, sen; public Wang(int r, int s) { ringgit = r; sen= s; System. out. println(“Jumlah sen: ” + jumlah. Sen()); } public int jumlah. Sen(){ 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. jumlah. Sen()); } }

Class Methods (operations) Methods are the operations that an object can perform or suffer. Bank. Account object might have methods: -deposit() -withdraw() -get. Balance() -increase. Overdraft() -decrease. Overdraft()


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 ASilly. Class { boolean a. Variable; void a. Method() { a. Variable = true; } class ASillier. Class extends ASilly. Class { boolean a. Variable; void a. Method() { a. Variable = false; super. a. Method(); System. out. println(a. Variable); System. out. println(super. a. Variable); } } 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

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

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 boat plane

Inheritance example Superclass car More generalised Sports car More specialised Sub-class Estate car



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 Space. Object draw() Martian Space. Ship Laser. Beam

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/j 2 se/1. 3/docs/api/index. html
- Slides: 42