Beruflich Dokumente
Kultur Dokumente
5 hrs)
Chapter Objectives:
Define what objects are and demonstrate how to create them.
Recognize the states of object creation before and after the execution of the constructor.
State the difference between objects, classes, methods, and data attribute.
Describe when and how to make use of constructors.
Point out the rules that describes the use of default constructors.
Compare the difference between public and private access modifiers.
Define what is encapsulation.
Show the proper way of implementing encapsulation to a class.
Design a Java source file layout
Create and deploy a Jar file.
Objects vs Classes
What are Objects?
Objects are anything that is real. It can be something that is conceptual or something that is physical.
Anything around you right now are considered as objects; the chairs, tables, books, and even your papers. All
these are objects. But objects are not just limited to the physical things around us. By looking into a desktop
application, or by using your favorite word processor you will see objects in them. Buttons, toolbars and even the
work area or the dialog boxes are objects. Physical objects can be tangible or intangible, but they are all visible.
Objects can also be something that is conceptual. Conceptual objects are objects that are not seen but
are actually there. These types of objects are also useful and they represent a set of data as well. An example of
conceptual objects are errors, exceptions or events. Errors and exceptions will be discussed in a separate
chapter as well as events. To give us a little idea about these conceptual objects. Errors and exceptions are
objects that is being thrown by your application every time that something goes wrong with it. While events are
the things that happens when you trigger a GUI component, an example of which is a button press or a mouse
drag.
What are Classes?
A class is the blue-print of an object. It is the declaration of data attributes and methods (also known as
behavior) that each object will have every time an object is being instantiated. The term instantiate is
synonymous with the term create. So, every time you instantiate an object, you are actually creating an object
from a class and the said object will reside on the heap memory. You can virtually create an unlimited number of
objects from a class. Each object has its own set of identity. This means that what ever changes takes place in
one object, the other objects remains unaffected.
Within this book, we will use UML (Unified Modeling Language) diagrams to graphically represent a
class. Consider this example:
Person
+ name : String
+ age : int
+ showDetails() : void
Page 1 of 10
public class
{
public
public
public
}
Person
String name;
int age;
void showDetails() {...}
p1.name = Jose;
p2.name = Pedro;
13
14
p1.age = 30;
p2.age = -5;
Will there be any programmatic problem? Will the compiler have a problem with line 14? No. This one
will compile and run. The compiler should not have any problems on line 14 right? But there is still a problem
with it.
Java is said to be an object oriented programming language, which means it has the capability to mimic
the behavior of real life objects. In this case, a real person cannot have a negative value for age. In our real
world, a person's age must be greater than or equal to zero.
So, how are we going to solve this? This is where encapsulation comes in. Encapsulation is an object
oriented feature that hides or protects data from direct access. So instead of declaring all attributes as public,
we can declare them as private to disable the accessibility of the attributes to other classes directly.
Person
- name : String
- age : int
+ showDetails() : void
Now, the name and the age attributes are declared as private, this means these attributes can only be
accessed within the class Person and cannot be accessed outside of this class including the TestPerson
class.
So if by declaring the name and age attributes as private, how are we supposed to assign and
retrieve values to them?
By providing a pair of setters and getters. Setters and getters are conventions used to create Java
Beans, it is now possible to assign values to the name and age attributes and as we assign values to them we
can also provide verification checks as well.
Java Beans is just a POJO (Plain Old Java Object), a conventional term for a well encapsulated object.
Java Beans is not the same as Enterprise Java Beans (EJB), EJBs are the business component of a Java EE
Application, it is also out of the scope of our topic as well.
Here is an example of a Java Bean.
Person
- name : String
- age : int
// setters
+ setName(String n): void
+ setAge(int a): void
// getters
+ getName() : String
+ getAge() : int
+ showDetails() : void
Translated into a Java Code, it will look like this:
Page 3 of 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Person.java
public class Person
{
private String name;
private int age;
// setters
public void setName(String n)
{
name = n;
}
public void setAge(int a)
{
if (a >= 0)
age = a;
else
age = 0;
}
// getters
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
In this revised Person class, you will notice that even if you try to assign a negative value for the
person's age, it will not be possible. It will first test the passed value, then assign.
Let's now see our revised TestPerson class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Page 4 of 10
// TestPerson.java
public class TestPerson
{
public static void main(String args[])
{
Person p1 = new Person();
Person p2 = new Person();
// assign names
p1.setName("Jose");
p2.setName("Pedro");
// assign age
p1.setAge(30);
p2.setAge(-5);
18
19
20
21
The default constructor is provided so that even if the writer does not provide a constructor we can still
Page 5 of 10
--> 0 or 1 statement
--> 0 to many
--> 0 to many
Packages
Packages in Java refer to subdirectories. A package may contain classes or sub-packages. Take this
diagram as an example. We have MyProj as a package and within MyProj, we have three sub packages
namely: subA, subB and test. And within subA, we have subAA as a sub-package.
A package may also contains classes inside it. The package subA contains A.class, subAA contains
AA.class, package subB contains B.class and package test contains TestMe.class that serves as our
test class.
MyProj
subA
subAA
A
AA
test
TestMe
subB
B
Page 6 of 10
package test;
import subA.*;
import subA.subAA.AA;
//import subB.B;
public class TestMe
{
public static void main(String args[])
{
A a = new A();
AA aa = new AA();
subB.B b = new subB.B();
// fully-qualified name
a.printMe();
aa.printMe();
b.printMe();
}
Line 1 states that once this source file compiles, the TestMe.class that will be generated will be
placed under the test subdirectory.
Lines 3 and 4 states that classes A.java and AA.java are needed inside this application and are
Page 7 of 10
Page 8 of 10
The Java API is composed of three (3) parts, the upper left part are the Java Packages, the lower left
part are the list of Java Classes per package and the center part is the write up for the Java Classes.
Try to click on one of the classes, say the String class, and read on the list of attributes, constructors
and methods that the String class contains.
Page 10 of 10