Friday, January 25, 2013

Introduction to Object Oriented Programming



A discussion on Types

It might help if you think of a Type as some sort of a contract or a promise from an object. The object is making a committment that it will respond to any other object or program who calls anyone of the methods within this type. Going back to the elevator example, it means that any Java object or program can call elevator.moveDownTo() or elevatorMoveUpTo() and they are guaranteed to get a response.
There are three ways to define a Type in Java, the classabstract class and interface---each has its own strengths and inconveniences.

Objects, Classes and Types

An object is an abstraction of something useful to you (the programmer). An object represents an idealized and generalized model of something of interest. Take for example a Phone object, its protocols could be abstracted as answerCall() and dialNumber(). If we skip all the programming details of type creation, we can start using the object in a Java program.
Phone phone = new Phone();
phone.answerCall();
phone.dialNumber("6321234567");

Using an object

Phone with an uppercase first letter is a type declaration and phone all lowercase letter is variable of type Phone---in the statement int or integer is the type and i is a variable of type integer. The newkeyword is responsible for creating the Phone object and Phone() is actually a special function or method---its called a constructor. To invoke the behavior of an object, you simply need to call its method using the dot notation. That is what we did in the statements phone.answerCall() andphone.dialNumber().
Objects are made by instantiating Classes. Classes are actually what we define as abstraction construct. If we were to codify the Phone program example, it could look like the following:
class Phone {

   String phonenumber="6321234567";

void answerCall() {
}

void dialNumber(String args) {
}

}
Phone.java is a Java class. It is constructed by using class keyword followed by a name of the class 1. The open and close curly brace is the body of the class.
You can define methods and variables inside the class body. The methods as a group constitutes the protocols of the object. It is the set of methods that the Phone object guarantees to respond to.
Java classes are written on source files---ideally, one class definition per source file but you can define more than one class in a source file. Classes are compiled using javac sourcefile.java, in our case, java Phone.java. This will produce a file named Phone.class, it is the compiled version of Phone.java---compiled classes is a requisite before you can create objects out of them.
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.answerCall();
    p2.dialNumber("6327778888");

}

}
The PhoneTest program shows a more complete example of how you might use the Phone object. The PhoneTest class is not necessary because the main() function could have been easily written inside Phone class, but the verbosity of two classes was chosen to make the example more readable and less confusing for beginner programmers.


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