CPE207 Object Oriented Programming (Week 3)
CPE207 Object Oriented Programming (Week 3)
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
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.
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.
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;
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