The key points of Java-Class-related concepts are listed and an example is provided as well to help you better understand their use in an object-oriented program.
A Java program consists of one or more classes
. Java represents, in a class
, a group of objects of the same type. For example, a program is needed to generate five triangles. In this case, a class Triangle
can be declared for being a blueprint of the five custom triangles.
Class
A class may consist of the following components:
- package declaration
- package
imports
- a class
name
- zero or more
properties
(fields) - zero or more
methods
(a series of statements)
Package
A package
is a namespace
that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. They can be compiled separately and imported into other programs.
Access modifier
- ( + )
public
class or member : accessible anywhere - ( - )
private
member : only accessible within the class - ( # )
protected
member : accessible within the package or the subclasses/descendants - ( ~ ) (default) : accessible within the package (package mypkg; class MyClass{} )
Instantiation
- Creating a custom object/instance from a class is referred to as
instantiation
. - In order to instantiate new
instances
from a class, the class must provide one or moreconstructors
, whose name must be the same as the class name. - A constructor is a special method that customizes a new instance by assigning specific values to some properties.
- The constructor method is always called with the keyword
new
together.
Example
In the example, a geometric shape, triangle, is implemented in a Java class Triangle. Take a look at the source code below for the class Triangle
to get an idea about how a class should be structured.
1package mytools;
2
3public class Triangle {
4
5 private int a, b, c; // Three property members represent the length of three edges in a triangle, respectively
6
7 public Triangle(int a, int b, int c) {
8 // Constructor which initializes a, b and c
9 this.a = a;
10 this.b = b;
11 this.c = c;
12 }
13
14 public int getA() {// getter for a
15 return a;
16 }
17
18 public int getB() {// getter for b
19 return b;
20 }
21
22 public int getC() {// getter for c
23 return c;
24 }
25
26 boolean allPositive() {
27 // Examine if all three lengths in a, b and c have a positive value
28 // The method returns true if every length is positive and false otherwise
29 return a > 0 && b > 0 && c > 0;
30 }
31
32 public boolean isValid() {
33 // Examine if a, b and c make a valid triangle
34 // The method returns true if a, b and c make a triangle and false otherwise.
35 return this.allPositive() && (a + b) > c && (b + c) > a && (c + a) > b;
36 }
37}
Property members
Triangle class is in the package
mytools
. It has three private property members, a, b and c, for storing lengths of its three sides.private int a, b, c;
Method members
Three getters are provided for external access to these three private properties.
public int getA public int getB public int getC
In addition,
Triangle
is able to perform two basic tasks,allPositive
andisValid
. Both tasks return a boolean.boolean allPositive() public boolean isValid()
Triangle.allPositive
is accessible within the package mytools. Triangle.isValid
is accessible globally.
Both methods utilize logical operations for validation. Triangle.allPositive
evaluates a conditional expression which combines three conditions with two logical-AND (&&)
operators. The method tells the caller if all sides has a positive length. Triangle.isValid
evaluates a conditional expression which joins one method call and three conditions by using three logical-AND (&&)
operators. The method tells the caller if three sides in a, b and c will make a triangle shape.
Last but not the least, in order to access all the members in a Java class, either user-defined or Java built-in, a new instance must be created for accessing services from a class.
Thus, Triangle
needs a constructor to initialize new Triangle instances
. A constructor
has been defined in the following:
public Triangle(int a, int b, int c) {
// Constructor which initializes a, b and c
this.a = a;
this.b = b;
this.c = c;
}
The keyword this
In the constructor above, the keyword this is a reference to
the current containing class object, which is Triangle
. Prefixing this and a dot sign before the property name a,
this.a
will prevent the property a
is shadowed by the constructor parameter a
. By doing so, they can be distinguished from the other in the same scope to avoid ambiguity.
Mutable w/ setter methods
The current Triangle
is immutable because it has no interface available for users to change its state
i.e. the shape in this case, once it has been initialized e. Thus, a, b and c can be final.
private final int a, b, c;
To make Triangle
mutable, write the setter method
for each property which requires updates according to the requirements. The setter method opens an interface through which a user can write a new value into the property. The typical format of a setter
method is given below, based on the property a:
public void setA(int a) {
this.a = a;
}
Instantiation w/ the keyword new
To access services from a Java class, an instance object must be instantiated.
The following post has a discussion about instantiating new instances.
Java Review: Instantiation w/ the keyword new
Exercise:
Write a Java class Person
.
As Person
has been designed in a specific way so that the following requirements should be met:
- It must be public and have the following private property members: firstName, lastName, gender, email, bDate
- Person must have a constructor that initializes lastName, firstName and gender for each new Person instance.
- Person allows outsiders to read its property members.
Share this post
Twitter
Facebook
LinkedIn
Email