Lab4
Lab4
OBJECT-ORIENTED PROGRAMMING
LAB 4: CLASS, OBJECT, ENCAPSULATION IN OOP
I. Objective
After completing this tutorial, you can:
• Understand how to program an OOP program in Java,
• Understand object and class concepts,
• Understand encapsulation in OOP.
III. Object
In the real world, we can find many objects/entities around us, e.g., a chair, a bike, a dog, animals. All
these objects have state(s) and behavior(s). If we consider a dog, then its states are name, breed, and
color, and the behaviors are baking, wagging its tail, and running. That’s it, an object has two
characteristics.
• State: represents data (value) of an object,
• Behavior: represents the behavior (functionality) of an object.
IV. Class
In the real world, you will often find many individual objects all of the same kind. There may be
thousands of other bicycles in existence, all with the same materials 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. A class is a
blueprint/template from which individual objects are created.
To define a class in Java, the least you need to determine:
• Class’s name: By convention, the first letter of a class’s name is uppercase, and subsequent
characters are lowercase. If a name consists of multiple words, the first letter of each word is
uppercase.
• Attributes/Properties (States)
• Methods (Behaviors)
• Constructors
• Getter & setter (we will discuss this later in this tutorial)
1. Example program
Following is an example of a class:
• Name: Student
• Attributes: name, gender, age
• Methods: studying, reading
void studying() {
System.out.println("studying...");
}
void reading() {
System.out.println("reading...");
}
}
public Student() {
this.name = "";
this.gender = "male";
this.age = 0;
}
public Student(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Student(Student st) {
this.name = st.name;
this.gender = st.gender;
this.age = st.age;
}
void studying() {
System.out.println("studying...");
}
void reading() {
System.out.println("reading...");
}
}
In the above program, we use the “this” keyword to access instance variables. The “this” keyword
is useful in the case of a parameterized constructor, we can clearly distinguish the instance variables and
input parameters.
3. Java Access Modifiers
Java provides several access modifiers to set access levels for classes, variables, methods, and
constructors. The four access levels:
• Default: Visible to the package, no modifiers are needed.
• Private: Visible to the class only.
• Public: Visible to the world.
• Protected: Visible to the package and all sub-classes (discuss later).
4. Overloading
Java supports the overloading method. It can distinguish between methods based on the method
signatures. This means in the same class you can define two or more methods that have the same name
if they have different parameter lists.
Overloading methods are differentiated by the number of arguments and the type of arguments passed
into that method.
Example:
5. Encapsulation
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance,
polymorphism, and abstraction (which we will discuss later).
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data
(methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other
classes and can be accessed only through the methods of their current class. Therefore, it is also known
as data hiding.
To achieve encapsulation in Java:
• Declare the variables of a class as private/protected.
• Provide public getter and setter methods to modify and view the variable’s values.
The following program illustrates how to achieve encapsulation in the Java OOP program.
public class Student {
private String name;
private String gender;
private int age;
public Student() {
this.name = "";
this.gender = "male";
this.age = 0;
}
void studying() {
System.out.println("studying...");
}
void reading() {
System.out.println("reading...");
}
System.out.println("Name:" + student.getName());
System.out.println("Gender:" + student.getGender());
System.out.println("Age:" + student.getAge());
student.studying();
student.reading();
System.out.println("Name:" + student1.getName());
System.out.println("Gender:" + student1.getGender());
System.out.println("Age:" + student1.getAge());
}
}
System.out.println(student);
}
}
To print the information of the object, you need to define the toString() method in your class. In the
class Student above, let's define the toString() method and return the information of the student.
@Override
public String toString() {
return "Student[" + name + ", " + gender + ", " + age + "]";
}
}
After defining this method, you can re-print the object student and observe the result.
System.out.println(student);
}
}
V. Exercises
1. A class called Point is designed as shown in the following class diagram. It contains:
• Two private instance variables: x (of the type float) and y (of the type float), with default 0.0
and 0.0, respectively.
• Two overloaded constructors: a default constructor with no argument, and a constructor that
takes 2 float arguments for the x coordinate and y coordinate.
• Two public methods: getX() and getY(), which return the x coordinate and the y coordinate
of this instance, respectively.
-- END --