ITEP309 - Object Oriented Programming - Part 3

5
Object-Oriented Programming What Is an Object? An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. An object can be considered a " thing " that can perform a set of related activities. The set of activities that the object performs defines the object 's behavior. For example, the hand can grip something or a Student ( object ) can give the name or address. Objects are thought of as having state. The state of an object is the condition of the object, or a set of circumstances describing the object. It is not uncommon to hear people talk about the "state information" associated with a particular object. o For example, the state of a bank account object would include the current balance, o the state of a clock object would be the current time, o the state of an electric light bulb would be "on" or "off." In pure OOP terms an object is an instance of a class. Real-world objects share two characteristics: They all have state and behavior. o Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). o Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: 1. "What possible states can this object be in?" 2. "What possible behavior can this object perform?". As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming. 1 | ITEP309 Object Oriented Programming Percival A. Fernandez

description

ITEP309 - Object Oriented Programming

Transcript of ITEP309 - Object Oriented Programming - Part 3

Page 1: ITEP309 - Object Oriented Programming - Part 3

Object-Oriented Programming

What Is an Object? An object is a software bundle of related state and behavior. Software objects are often used to model the real-world

objects that you find in everyday life. An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object

performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.

Objects are thought of as having state. The state of an object is the condition of the object, or a set of circumstances describing the object. It is not uncommon to hear people talk about the "state information" associated with a particular object.

o For example, the state of a bank account object would include the current balance, o the state of a clock object would be the current time,o the state of an electric light bulb would be "on" or "off."

In pure OOP terms an object is an instance of a class. Real-world objects share two characteristics: They all have state and behavior.

o Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). o Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear,

changing pedal cadence, applying brakes).

Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions:

1. "What possible states can this object be in?"2. "What possible behavior can this object perform?".

As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune).

You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

A software object.

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).

Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

1 | I T E P 3 0 9 Object Oriented ProgrammingP e r c i v a l A . F e r n a n d e z

Page 2: ITEP309 - Object Oriented Programming - Part 3

Object-Oriented Programming

Consider a bicycle, for example:

A bicycle modeled as a software object.

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Bundling code into individual software objects provides a number of benefits, including:1. Modularity: The source code for an object can be written and maintained independently of the source code for

other objects. Once created, an object can be easily passed around inside the system.2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation

remain hidden from the outside world.3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object

in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.

4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

What Is a Class? A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an

object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations. A class is a pattern, template, or blueprint for a category of structurally identical items. The items created using the

class are called instances. This is often referred to as the "class as a `cookie cutter'" view. As you might guess, the instances are the "cookies.".

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model.

Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

2 | I T E P 3 0 9 Object Oriented ProgrammingP e r c i v a l A . F e r n a n d e z

Page 3: ITEP309 - Object Oriented Programming - Part 3

Object-Oriented Programming

class Bicycle {

int cadence = 0; int speed = 0; int gear = 1;

void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } The fields cadence, speed, and gear represent the object's state, and the methods (changeCadence, changeGear,

speedUp etc.) define its interaction with the outside world. The following Bicycle class is one possible implementation of a bicycle:

What Is Encapsulation? Encapsulation is the inclusion within a program object of all the resources need for the object to function - basically,

the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties.

The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes.

Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist.

It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

A language mechanism for restricting access to some of the object's components. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that

data.

public class Employee { private BigDecimal salary = new BigDecimal(50000.00); public BigDecimal getSalary() { return salary; } public static void main() { Employee e = new Employee(); BigDecimal sal = e.getSalary(); } }

3 | I T E P 3 0 9 Object Oriented ProgrammingP e r c i v a l A . F e r n a n d e z

Page 4: ITEP309 - Object Oriented Programming - Part 3

Object-Oriented Programming

What Is Association? Association is a (*a*) relationship between two classes. It allows one object instance to cause another to perform an

action on its behalf. Association is the more general term that define the relationship between two classes, whereas the aggregation and

composition are relatively special.

What Is Inheritance? Inheritance provides a powerful and natural mechanism for organizing and structuring your software. Ability of a new class to be created, from an existing class by extending it, is called inheritance.What Is Interface? An interface is a contract between a class and the outside world. When a class implements an interface, it promises

to provide the behavior published by that interface. Interface can be used to define a generic template and then one or more abstract classes to define partial

implementations of the interface. Interfaces just specify the method declaration (implicitly public and abstract) and can contain properties (which are

also implicitly public and abstract). Interface definition begins with the keyword interface. An interface like that of an abstract class cannot be instantiated.

What is Polymorphism? Polymorphism is a generic term that means 'many shapes'. More precisely Polymorphisms mean the ability to request

that the same operations be performed by a wide range of different types of things. In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator

overloading and method overriding.

What Is a Package? A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage.

4 | I T E P 3 0 9 Object Oriented ProgrammingP e r c i v a l A . F e r n a n d e z