20220620113652D6421 Session2 Class and Object
20220620113652D6421 Session2 Class and Object
Development
Effective Period : June 2022
Session 2
Learning Outcomes
Outline
• Object oriented programming
• Class VS Object
• Access modifiers
• The scope of variables
• Class’s components
• Object’s components
• Primitive types VS Reference types
• Inner class
Object Oriented Programming
(OOP)
1) Class’ name
A name represents what class it is
2) Attribute
Attribute represents element that contain in the
class
3) Method
Method represent the behavior / ability of the
class
10
How to construct a class? (1)
Class Diagram Code in Java
•
• Initialize class’ name public class Circle {
public static void main(String[]
args) {
Circle
}
}
11
How to construct a class? (2)
Class Diagram Code in Java
•
• Initialize attribute public class Circle {
private double radius;
12
How to construct a class? (3)
Class Diagram Code in Java
•
• Initialize method public class Circle {
private double radius;
double getArea(){
Circle return radius * radius * Math.PI;
}
- radius : double
double getPerimeter(){
+ getArea() : double return 2 * radius * Math.PI;
}
+ getPerimeter() : double
double setRadius(double newRadius){
+setRadius() : double }
radius = newRadius;
}
}
13
How to construct an object? (1)
• These are steps in order to create an object from a class
1) Declaration
A variable declaration with a variable name with
an object type
2) Instantiation
The ‘new’ keyword is used to create an object
3) Initialization
The ‘new’ keyword is followed by constructor.
This step aiming at initializing the new object.
14
How to construct an object?
(2)
• Constructor
• Constructors are a special kind of method. They
have three peculiarities:
1) A constructor must have the same name as the
class itself
2) Constructors do not have a return type
3) Constructors are invoked using the ’new’
operator when an object is created.
Constructors play the role of initializing objects.
15
How to construct an object? (3)
Class Diagram Code in Java
• public class Circle {
private double radius;
Circle
Circle(double newRadius){
- radius : double }
radius = newRadius;
}
}
16
How to destruct an object?
• Destructor
• Because Java is a garbage collected language
you can not predict when (or even if) an object
will be destroyed. Hence there is no direct
equivalent of a destructor
• There is an inherited method so called finalize,
but this is called entirely at the discretion of the
garbage collector
17
The scope of variables (1)
declaration and
continues to the end
of the block that
contains the variable
The scope of variables (3)
Instance Variables Example
•
• Instance variables are created public class Employee{
private String name;
when an object is created with private double salary;
emp.setName(“Budi”);
public static void main(String []args){
emp.getName(); Employee emp = new Employee("Hanry");
emp.setSalary(3500000);
}
}
The scope of variables (4)
Class / Static
Variables Example
•
• Class variables are public class Employee{
private String name;
private static final String Department =
also known as static "Human Capital";
}
Primitive types VS Reference types (1)
26