Archive for PBO

Object Oriented Programming

 Variables

 

instance variables class variables

Methods

 

instance methods class methods

Constructors

 

Method Overloading

 

Method Overriding

 

Polymorphism Abstraction Interfaces Nested Classes

Access Modifiers

 

public, private, protected

 

Packages

 

Keywords

 

this, super, final, static

 

 

 

 

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.

 

 

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

 

 

 

Change gear

 

 

 

 

 

 

 

 

brake

 

 

 

 

 

 

 

 

 

accelerate

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Car Object

 

(speed)

 

 

 

 

 

red

(color)

 

 

 

 

 

5th gear

(currentgear)

 

 

 

 

 

 

 

 

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 car’s state is independent and can be different from each other.

 

 

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.

 

 

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

 

 

 

 

 

Brake

 

Number of wheels

 

 

 

 

 

 

 

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

 

 

Brake                              Color     =    Brake

10

Color     =

 

 

 

Change gear

red

 

gears = 4

 

 

 

 

 

Change gear

blue gears = 4

 

 

 

 

 

Your Car

My Car

 

 

 

 

 

 

 

 

These two car objects created from the car class.

 

 

Creating

 

Classes

 

 

{

 

 

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

}

 

 

{

 

 

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( );

 

 

Car  c   =   new    Car( );

 

 

 

 

 

Declaration                      Instantiation        Initialization

 

 

 

 

 

 

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.

 

 

 

 

 

 

 

c

 

 

 

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( );