Week 3
Access Modifiers, Class Constructors,
UML, Data Hiding(Encapsulation)
Dr. Nehad Ramaha,
Computer Engineering Department
Karabük Universities These Slides mainly adopted from Assist. Prof. Dr. Ozacar Kasim lecture notes
1
The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
2
3
Types in Java are divided into two categories: primitive types and
reference types.
The primitive types are boolean, byte, char, short, int, long, float and
double.
All other types are reference types.
A primitive-type:
◦ variable can store exactly one value of its declared type at a time.
◦ instance variables are initialized by default.
◦ Variables of types byte, char, short, int, long, float and double are initialized to 0.
◦ Variables of type boolean are initialized to false.
Reference-type:
◦ variables (called references) store the location of an object in the computer’s
memory.
◦ The object that’s referenced may contain many instance variables and methods.
◦ Reference-type instance variables are initialized by default to the value null.
◦ A reference to an object is required to call an object’s methods.
4
Java provides some access modifiers to set
access levels for classes, variables, methods, and
constructors.
For classes, you can use either public or default:
Modifier Description
public The class is accessible by any other class
default The class is only accessible by classes in the same package. This is
used when you don't specify a modifier.
5
For attributes, methods and constructors, you
can use the one of the following:
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you
don't specify a modifier.
protected The code is accessible in the same package and subclasses. You will learn
more about subclasses and superclasses in the Inheritance chapter.
6
A variable or method declared without any access control modifier is
available to any other class in the same package.
String version = "1.5.1";
boolean processOrder() {
return true;
}
7
A class, method, constructor, interface, etc. declared public can be
accessed from anywhere.
Therefore, attributes, methods, blocks declared inside a public class
can be accessed from any class belonging to the Java Universe.
public static void main(String[] arguments)
{
// ...
}
The main() method has to be public. Otherwise, it could not be
called by a Java interpreter to run the class.
8
Methods, variables, and constructors that are declared private can
only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class
cannot be private.
Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
class Animal {
private String name;
}
public class MainClass{
public static void main(String[] arguments) {
Animal animal = new Animal();
animal.name; This is will cause a compilation
} error. Because name is not
} accessible due to its protection
level. 9
10
A constructor is a method which is used to
initialize an object
public class MyClass
Constructor method of a class has the {
same name as that of the class, they are
called when an object of a class is created.
//constructor
When Attributes of an object are not public MyClass()
available while creating objects, the {
default constructor is called. ...
}
It is optional to write constructor ...
method(s) in a class but due to their utility }
they are used.
constructor doesn’t have a return type !!!
11
Default Constructor
If a class does not define constructors, the compiler provides a
default constructor with no parameters, and the class’s instance
variables are initialized to their default values.
There’s No Default Constructor in a Class That Declares a Constructor
If you declare a constructor for a class, the compiler will not create a
default constructor for that class.
12
class Student{
private int id;
private String name;
private int age;
Constructor with Student(){
no parameters id = 100;
name = "New Student";
age = 18;
Overloaded }
constructor Student(int id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
}
Now, Let’s create a student object… 13
The Unified Modeling Language (UML) is a general-purpose,
developmental, modeling language in the software engineering field.
It provides a standard way to visualize the design of a system.
14
Adding the Constructor to Class Account’s UML Class Diagram
The UML models constructors in the third compartment of a class
diagram.
To distinguish a constructor from a class’s operations, the UML
places the word “constructor” between guillemets (« and ») before the
constructor’s name.
15
Top Compartment
In the UML, each class is modeled in a class diagram as a rectangle
with three compartments. The top one contains the class’s name
centered horizontally in boldface.
16
It contains the class’s attributes, which correspond to instance
variables in Java. Here minus (-) means that the attribute is private
17
Bottom Compartment
It contains the class’s operations, which correspond to methods and
constructors in Java.
The UML represents instance variables as an attribute name, followed by a
colon and the type.
Private attributes are preceded by a minus sign (–) in the UML.
The UML models operations by listing the operation name followed by a set
of parentheses.
A plus sign (+) in front of the operation name indicates that the operation is
a public one in the UML (i.e., a public method in Java).
18
Return Types Parameters
The UML indicates an operation’s return The UML models a parameter
type by placing a colon and the return of an operation by listing the
type after the parentheses following the parameter name, followed by
operation name. a colon(:) and the parameter
UML class diagrams do not specify return type between the
types for operations that do not return parentheses after the
values. operation name
Declaring instance variables private is
known as data hiding or encapsulation.
19
20
An Example : Lets create an Account Class using UML
CME225 OOP- Week 3 21
Account Class
CME225 OOP- Week 3 22
CME225 OOP- Week 3 23
AccountTest Class to Use Class Account
CME225 OOP- Week 3 24
CME225 OOP- Week 3 25
CME225 OOP- Week 3 26