Java Programming Tutorial: Object-Oriented Programming (OOP) Basics
Java Programming Tutorial: Object-Oriented Programming (OOP) Basics
Pgina 1 de 9
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 2 de 9
Traditional
procedural-oriented
reusable
software
components:
1. The programs are made up of
functions. Functions are often not
reusable. It is very difficult to copy
a function from one program and
reuse in another program because
the
the
reference
function
the
is likely
headers,
to
global
programs
uses
array,
method,
pointer,
Relationship
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 3 de 9
As an example, suppose you wish to write a computer soccer games (which I consider as a
complex application). It is quite difficult to model the game in procedural-oriented
languages. But using OOP languages, you can easily model the program accordingly to the
"real things" appear in the soccer games.
Player: attributes include name, number, location in the field, and etc; operations include
run, jump, kick-the-ball, and etc.
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 4 de 9
Ball:
Reference:
Field:
Audience:
Weather:
Most importantly, some of these classes (such as Ball and Audience) can be reused in
another application, e.g., computer basketball game, with little or no modification.
Benefits of OOP
The procedural-oriented languages focus on procedures, with function as the basic unit. You
need to first figure out all the functions and then think about how to represent data.
The object-oriented languages focus on components that the user perceives, with objects as
the basic unit. You figure out all the objects by putting all the data and operations that
describe the user's interaction with the data.
Object-Oriented technology has many benefits:
Ease in software design as you could think in the problem space rather than the
machine's bits and bytes. You are dealing with high-level concepts and abstractions. Ease
in design leads to more productive software development.
Ease in software maintenance: object-oriented software are easier to understand,
therefore easier to test, debug, and maintain.
Reusable software: you don't need to keep re-inventing the wheels and re-write the same
functions for different situations. The fastest and safest way of developing a new
application is to reuse existing codes - fully tested and proven codes.
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 5 de 9
2.OOP in Java
2.1Class & Instances
In Java, a class is a definition of objects of the same kind. In other words, a class is a blueprint,
template, or prototype that defines and describes the static attributes and dynamic behaviors
common to all objects of the same kind.
An instance is a realization of a particular item of a class. In other words, an instance is an
instantiation of a class. All the instances of a class have similar properties, as described in the
class definition. For example, you can define a class called "Student" and create three
instances of the class "Student" for "Peter", "Paul" and "Pauline".
The term "object" usually refers to instance. But it is often used loosely, which may refer to a
class or an instance.
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 6 de 9
The following figure shows two instances of the class Student, identified as "paul" and
"peter".
Brief Summary
1. A class is a programmer-defined, abstract, self-contained, reusable software entity that
mimics a real-world thing.
2. A class is a 3-compartment box containing the name, variables and the methods.
3. A class encapsulates the data structures (in variables) and algorithms (methods). The
values of the variables constitute its state. The methods constitute its behaviors.
4. An instance is an instantiation (or realization) of a particular item of a class.
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 7 de 9
2.3Class Definition
In Java, we use the keyword class to define a class. For examples:
public class Circle {
double radius;
String color;
double getRadius() {...}
double getArea() {...}
// class name
// variables
// methods
}
public class SoccerPlayer {
int number;
String name;
int x, y;
void run() {...}
void kickBall() {...}
// class name
// variables
// methods
We shall explain the access control modifier, such as public and private, later.
Class Naming Convention: A class name shall be a noun or a noun phrase made up of
several words. All the words shall be initial-capitalized (camel-case). Use a singular noun for
class name. Choose
a meaningful and
self-descriptive
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 8 de 9
2.5Dot Operator
The variables and methods belonging to a class are formally called member variables and
member methods. To reference a member variable or method, you must:
1. first identify the instance you are interested in, and then
2. Use the dot operator (.) to reference the member (variable or method).
For example, suppose that we have a class called Circle, with two variables (radius and
color) and two methods (getRadius() and getArea()). We have created three instances of
the class Circle, namely, c1, c2 and c3. To invoke the method getArea(), you must first
identity the instance of interest, says c2, then use the dot operator, in the form of
c2.getArea(), to invoke the getArea() method of instance c2.
For example,
// Declare and construct instances c1 and c2 of the class Circle
Circle c1 = new Circle ();
Circle c2 = new Circle ();
// Invoke member methods for the instance c1 via dot operator
System.out.println(c1.getArea());
System.out.println(c1.getRadius());
// Reference member variables for instance c2 via dot operator
c2.radius = 5.0;
c2.color = "blue";
Calling getArea() without identifying the instance is meaningless, as the radius is unknown
(there could be many instances of Circle - each maintaining its own radius).
In general, suppose there is a class called AClass with a member variable called aVariable
and a member method called aMethod(). An instance called anInstance is constructed for
AClass. You use anInstance.aVariable and anInstance.aMethod().
2.6Member Variables
A member variable has a name (or identifier) and a type; and holds a value of that particular
type (as descried in the earlier chapter). A member variable can also be an instance of a
certain class (to be discussed later).
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014
Pgina 9 de 9
For example,
private double radius;
public int length = 1, width = 1;
2.7Member Methods
A method (as described in the earlier chapter):
1. receives parameters from the caller,
2. performs the operations defined in the method body, and
3. returns a piece of result (or void) to the caller.
The syntax for method declaration in Java is as follows:
[AccessControlModifier] returnType methodName ([argumentList]) {
// method body or implementation
......
}
For examples:
public double getArea() {
return radius*radius*Math.PI;
}
Method Naming Convention: A method name shall be a verb, or a verb phrase made
up of several words. The first word is in lowercase and the rest of the words are initialcapitalized (camel-case). For example, getRadius(), getParameterValues().
Take note that variable name is a noun (denoting a static attribute), while method name is a
verb (denoting an action). They have the same naming convention. Nevertheless, you can
easily distinguish them from the context. Methods take arguments in parentheses (possibly
zero argument with empty parentheses), but variables do not. In this writing, methods are
denoted with a pair of parentheses, e.g., println(), getArea() for clarity.
http://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html
24/04/2014