Friday, January 25, 2013

Introduction to Object Oriented Programming



Instantiation

To create an object, you need to instantiate a class. The resulting object now has the protocols or the Type of that class. You can begin using the object according to the protocols that was defined by its class. The protocols needs to be followed strictly, if the method defined is dialNumber(String args), you need to call the method exactly as it was defined. The spelling needs to be right, the casing of the letters needs to be right and if the method expects an argument or parameter to be passed, you need to pass the expected parameter---in the case of the dialNumber method, it expects a String parameter.
The PhoneTest program defines the setPhoneNumber() method and the phonenumber variable. The class initially defines its own phone number via the phonenumber variable, but it also offers a way to change this phone number. The setPhoneNumber method was declared to facilitate a way to change an internal detail of the class. If the setPhoneNumber method is called passing a new phone number, the object's state will change---it will have a new phone number.

Encapsulation

Classes provides a way to package data and behavior in a single construction. The phonenumbervariable is not foreign or external to Phone Type, it is an instrinsic property of the Phone. ThesetPhoneNumber method is also intrinsic to the Phone. If you invoke the setPhoneNumber method, it will act on a data that belongs to the Phone object, not something alien to the Phone. Java enforces this kind of encapsulation in a strict fashion. There is no way to define a stand-alone method or a stand-alone variable, every method and every variable needs to be associated with aType.
The concept of objects, classes and types are intertwined. Understanding these concepts individually and collectively forms one of the basis of effective Java programming.
Objects are created from classes, you cannot create an object without a class definition. When you create an object, the process is known as instantiation 2. An instantiated class (object) can respond to messages or methods as long as the methods are within the defined protocols of the class.
Type is a collection of methods. One way to define a Type is to define a class---there are two other ways, they will be discussed later in this chapter. It is not correct to say that a class has a type. It is correct to say that a class is a Type, just as it is correct to say that an object has a Type---the Type that was defined by the class.
To illustrate the relationship between a class and an object, try imagining a house plan, a blueprint if you may---the kinds of drawings that architects produce before building a house. If we build one house out of an architect's blueprint, we can say that the physical house that was built is an instance of the blueprint.
If we build another house using the same architect's blueprint, we can say that the second house is another instance of the architect's blueprint. Now there are two houses built from the same blueprint. Even if the two houses will be identical because of their structural similarities, they are actually two distinct houses, they are not one and the same. They will be inhabited by different people, they may differ in roof colors or interior paints etc. That is exactly what's going on as well between Classes and Objects. Classes are the blueprints from whence objects are made from.
Going back to the Phone example, if we wanted to create other Phone objects, we simply need to instantiate the Phone class again.
import static java.lang.System.out;

class Phone {

String phonenumber="6321234567";

void answerCall() {
    out.println("Answering call...");
}

void dialNumber(String args) {
    out.println("Calling " + args);
}

    void setPhoneNumber(String args) {
    phonenumber = args;
}

}

class PhoneTest {

   public static void main (String [] args)
{
    Phone p1 = new Phone();
    p1.answerCall();
    p1.dialNumber("6329876543");

    Phone p2 = new Phone();
        p2.setPhoneNumber("6329995555");
    p2.answerCall();
    p2.dialNumber("6327778888");

}

}

State encapsulation

The variables p1 and p2 are both of type Phone but they are two distinct objects. They have two different states (phone numbers). You can manipulate one phone object without impacting the state of the other. This characteristic of objects allows for a more sane and rational programming environment.



Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678






No comments:

Post a Comment