Anda di halaman 1dari 24

Object Oriented Programming

Object Oriented Programming Object Class Variables instance variables class variables Methods instance methods class methods Constructors Method Overloading Method Overriding

Encapsulation Inheritance Composition Polymorphism Abstraction Interfaces Nested Classes Access Modifiers public, private, protected Packages Keywords this, super, final, static

In object-oriented programming, a computer program may be seen as a collection of individual units, or objects, that act on each other.

It is opposite to a traditional programming in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer.

Each object is capable of receiving messages, processing data, and sending messages to other objects.

Each object can be viewed as an independent little machine or actor with a distinct role or responsibility.

Object

Object-oriented programming focuses on the development of reusable software components, called objects. An object is a building block which contains variables and methods. Objects are key to understanding object oriented technology. You can look around and can see many examples of real-world objects: dog, car, table, chair, bicycle.

All real world objects have two characteristics:

state and behavior


For example car have states
(current gear, number of gears, color, number of wheels)

and behaviors
(braking, accelerating, slowing down, changing gears)

Software objects are modeled after real-world objects and they also have state and behavior. A software object maintains its state in one or more variable. A software object implements its behavior with methods.

methods (behavior)

variables (state)

Software Object

So our car object look like the following figure.


10 mph (speed) red (color)

Change gear

brake
5th gear (currentgear)

accelerate

Car Object

Class

In the real world, we often have many objects of the same kind. For example, my car is just one of many cars in the world.

Using object-oriented terminology, we can say that my car object is an instance of the class of objects known as cars.

Cars have state (4 gears, 1 engine, 4 wheels) and behavior (change gears, accelerate) in common. However, each cars state is independent and can be different from each other.

When car manufacturers build cars they take advantage of the fact that cars share common characteristics, by building many cars from the same blueprint. It would be very inefficient to produce a new blueprint for every individual car manufactured. In object-oriented, it is also possible to have many objects of the same kind that share characteristics. Classes provide the benefits of creating a template of objects.

we can take advantage of the fact that objects of the same kind are similar and we can create a blueprint for those objects. A template or blueprint of objects is called a class.
A class is a template or blueprint that defines the variables and the methods common to all objects of a certain kind.

Change gear

Number of gears Number of wheels

Brake

Car object After you've created the car class, you can create any number of car objects from the class. Each object gets its own copy of all the variables defined in the class.

speed = 15 Brake Change gear Color red gears = 4 = Brake

speed = 10 Color = blue gears = 4

Change gear My Car

Your Car

These two car objects created from the car class.

Creating Classes

class ClassName { variable 1; variable 1; method1(){} method2(){} }

class Car { int gears; int wheels;

public void changeGear() {} }

Creating Objects

You can create an object of class with the following syntax: ClassName objVariable = new ClassName( );

So the car class object can be created as:


Car c = new Car( );

The object creation statement has three parts.


Car c = new
Instantiation

Car( );
Initialization

Declaration

1. Declaration 2. Instantiation 3. Initialization

Declaration
You declare variables of int type as: int a; You can say that a is a variable who can refer to any type of int data. Classes in java are also types so you can declare class type variable as: Car c; You can say that c is a variable who can refer to any type of Car.

Declaring a variable do not create any object. The code Car c; does not create a new car object, it just declare a variable named c that will be used to refer to a Car object. The reference is still empty until assigned with a new keyword. The empty reference is called null reference in java.

Instantiation
The new operator instantiates a class by allocating a memory for a new object. The new operator returns a reference to the object it created and this reference is assigned to the appropriate variable.

c
Car object

Initialization
The object is always initialize by calling a constructor. A constructor is a special method which has a same name as class and used to initialize the variables of that object. In this case the Car class object is initialized by calling the constructor of Car class Car( );

Anda mungkin juga menyukai