Object-Oriented Programming Concepts
Hello and welcome to the Object-Oriented Programming session. In this session, you’ll learn a few basic concepts of Java programming including Objects, Classes, Inheritance, Interfaces, and Packages. Each discussion and example shows throughout this session are intended to focuses on how they related to the real world. For more information on how you can obtain a free license of Netbeans 6.5 tools kits visit Sun Development Center at http://download.netbeans.org/netbeans/6.5/beta/ .
Quick Links:
What is an Object?
What is a Class?
What is Inheritance?
What is an Interface?
What is a Package?
An Object is a structure that contains data and methods to manipulate data. It is a key to understand object-oriented technology. When you think of an object, you would think of something usable, like a chair, table, sofa, or even your pets. Each object contains an instance variables and methods that use to identify its behavior. For example, a Dog object, what do we knows about our dog? Its name, size, and breed… these are the instance variables. An instance variable is a variable that doesn’t change its value. Now, what can the dog do? Bark, eat, sleep, and play right…? These are the methods. From the example below, you can see how a Dog object is really like.

Now that we understand the concepts of an object but how could it be written? From the example above we can analyze the data given to create a new class called “MyDog”:
package exampledog;
/**
*
* @author William Nguyen
*/
public class MyDog {
int Size; //this is the size of the dog, int – means integer
String Breed; //declaring dog Breed as String
String name; //declaring name as String
void Bark() { //method Bark
System.out.println("Ruff!!!"); //the system will print “Ruff!!” on the screen without the quote
} //close Bark method
void Eat() { //method Eat
System.out.println("Yummmy!"); //the system will print “Yummmy!” on the screen without the quote
} //close Eat method
void Sleep() { //method Sleep
System.out.println("Zzzzzzz!.."); //the system will print “Zzzzzzz!” on the screen without the quote
} //close method Sleep
void Play() { //method Play
System.out.println("Playing ball!"); //the system will print “Playing ball!” on the screen without the quote
} //close the method Play
} //close class
To access the object variable and methods from the above class we have to create another class called “TestObject”. This TestObject class contains Main method which triggers the JVM to run.
Example;
Class TestObject { //class TestObject
Public static void main (String[] args) { //main method
MyDog x = new MyDog(); //making a new dog object
x.Size = 10; //a dog size
x.Bark(); //calling the dog to bark
x.Eat(); //calling the dog to eat
x.Sleep(); //calling a dog to sleep
x.Play(); //calling a dog to play
} //close main method
} //end class
Output:
run:
Ruff!!!
Yummmy!
Zzzzzzz!..
Playing ball!
BUILD SUCCESSFUL (total time: 1 second)
Now, let’s take a look at a class. What is a class by the way?
A class is a blueprint for an object. It tells the virtual machine “VM” how to create an object of that particular type. Each object from a particular class can have its own values for the instance variables. For example, you might use a dog class to make hundreds of different dogs, and each dog might have its own color, size, and so on… You may notice that the MyDog class does not contain a main method. That’s because it’s not a complete application; it’s just the blueprint for Dog that might be used in an application. The responsibility of creating and using new MyDog objects belongs to some other class in your application like the TestObject class from the previous discussion.
Here is an ExampleDog class which creates two separate Dog objects and invokes their methods:
package exampleDog;
/**
*
* @author William Nguyen
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Create two different Dog objects
int size;
MyDog dog1 = new MyDog(); //create new object for dog1
MyDog dog2 = new MyDog(); //create new object for dog2
//Methods uses on these two objects are same as the above example
dog1.Size = 10; //assign the size value to dog1
dog1.Bark(); //assigning dog1 to bark when called
dog1.Eat(); //assigning dog1 to eat when called
dog1.Sleep(); //assigning dog1 to go to sleep when called
dog1.Play(); //assigning dog1 to play when called
dog2.Size = 5; //assign the size value to dog2
dog2.Bark(); //assigning dog2 to bark when called
dog2.Eat(); //assigning dog2 to eat when called
dog2.Sleep(); //assigning dog2 to go to sleep when called
dog2.Play(); //assigning dog2 to play when called
}
}
The output of this ExampleDog prints out the dog1 and dog2 methods:
run:
Ruff!!!
Yummmy!
Zzzzzzz!..
Playing ball!
Ruff!!!
Yummmy!
Zzzzzzz!..
Playing ball!
BUILD SUCCESSFUL (total time: 1 second)
Different kinds of objects often have a certain amount in common with each other. Canine, Hippo, Elephants, and Lion, for example, all share the characteristics of animals (make noise, eat, roam). Yet each also defines additional features that make them different. For example: Canine eats dog foods and can be placed as a pet; Hippo eats grass and cannot be placed as a home pet; Elephants eats vegetable and also cannot be placed as a pet.
Object-oriented programming allows two or more classes to inherit commonly used instance variables and methods from other classes. For this example, MyDog now becomes the super class of dog1, and dog2. Each class is allowed to have one direct super class, and each super class has the potential for an unlimited number of subclasses, also called a "Child class."

The syntax for creating a child class is very simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from.
For example:
Class Dog1 extend MyDog {
//the instance variables and methods of Dog1 go here
}
This gives Dog1 instance variables and methods to be the same as MyDog class.
At this point, you probably already know that objects define their interaction with the outside world through the methods that they expose. Methods form the object’s interface with the outside world; for example, a TV remote is an interface between you and the television set. When you pressed a power button, it turns the TV on/off.
In its most common form, an interface is a group of related methods with empty bodies. For example, the Dog’s behavior, if specified as an interface, it might appear as follows:
Interface Dog {
void Bark (int newValue); //Creating new Value for Barking
void Play (int newValue); //Creating new Value for Play
}
In addition, to implement this interface we would have to change the class name to something like TestDog, and we would have to use the implements keyword in the class declaration.
For example:
Class TestDog implements Dog {
//Then place the rest of the class code here
}
The reason for implementing an interface is to allow a class to become more formal about the behavior it promises to provide. In addition, if a class claims to implement an interface, all of its methods has to defined by that interface must appear in its source code before the class will successfully compile.
At this point, many of you probably wondering what is a Package and how it could be use in Java platform… Well, a Package is a namespace that organizes a set of related classes and interfaces. Think about when we designing a website, all of the HTML pages is in one folder, and images in another folder. This is to keep things more organized and for easier to maintains. In programming, classes are composed of hundreds or thousands of individual classes, therefore, it makes more sense to keep things organized by placing related classes and an interfaces into packages.
April 22, 2009
By: William Nguyen
|
|


