0% found this document useful (0 votes)
5 views26 pages

20220620113652D6421 Session2 Class and Object

The document outlines a course on Business Application Development focusing on Object Oriented Programming (OOP) concepts. It covers key topics such as classes, objects, access modifiers, variable scope, and the construction of classes and objects in Java. The learning outcomes include explaining OOP concepts, solving algorithm problems using OOP, and constructing simple applications.

Uploaded by

jnrius.alxnder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

20220620113652D6421 Session2 Class and Object

The document outlines a course on Business Application Development focusing on Object Oriented Programming (OOP) concepts. It covers key topics such as classes, objects, access modifiers, variable scope, and the construction of classes and objects in Java. The learning outcomes include explaining OOP concepts, solving algorithm problems using OOP, and constructing simple applications.

Uploaded by

jnrius.alxnder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Course : Business Application

Development
Effective Period : June 2022

Class and Object

Session 2
Learning Outcomes

• EXPLAIN Object Oriented concept


• SOLVE the algorithm problem using Object
Oriented concept
• CONSTRUCT a simple application with Object
Oriented concept
Contents

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)

• OOP involves programming using objects


• An object represents an entity in the real world
that can be distinctly identified
• An object has a unique identity, state, and
behaviour
Class VS Object
Class Object
• Collection of data • A representation of a
definitions and class
methods in the unit Circle1 : Circle
for a particular - radius = 1
purpose. A template is
a blueprint that Circle2 : Circle
defines what data
Circle - radius = 25
objects and methods
- radius : double
+ getArea() : double Circle3 : Circle
+ getPerimeter() : double - radius = 125
+setRadius() : double
5
Access Modifiers

Access Same class Same Subclass Other


Modifiers package packages
public Y Y Y Y
protected Y Y Y N
no access Y Y N N
modifier
private Y N N N
Access Same class Same Subclass Other
Click icon to add picture
Modifiers package packages
public Y Y Y Y

What does ‘public’ means?


• A class, method, constructor, interface, etc declared as public can be
accessed from any other class
• If the public class we are trying to access is in a different package,
then the public class still needs to be imported
Access Same class Same Subclass Other
Click icon to add picture
Modifiers package packages
protected Y Y Y N

What does ‘protected’ means?


• Variables, methods, and constructors declared as protected in a
superclass can be accessed only by the subclasses in other package or
any class within the package of the protected members’ class
• The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared as protected, however
methods and fields in a interface cannot be declared protected
Access Same class Same Subclass Other
Modifiers package packages
private Y N N N

What does ‘private’ means?


• Methods, variables, and constructors that are declared priate can only
be accessed within the declared class itself
• Private access modifier is the most restrictive access level. Class and
interfaces cannot be private
• Variables that are declared private can be accessed outside the class,
if public getter methods are present in the class
• Using the private modifier is the main way than an object encapsulates
itself and hides data from the outside worlds
Class’ Components
• These are components that required to build a class

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;

Circle public static void main(String[]


args) {
- radius : double
}
}

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;

public static void main(String[] args)


{

}
}

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;

+ getArea() : double double getArea(){


return radius * radius * Math.PI;
+ getPerimeter() : double }

+setRadius() : double double getPerimeter(){


return 2 * radius * Math.PI;
}

double setRadius(double newRadius){


radius = newRadius;
}

public static void main(String[] args) {

}
}

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)

• The scope of instance and static variables is the


entire class, regardless of where the variables
are declared
• Instance and static variables in a class are
referred to as the class’s variables or data fields
The scope of variables (2)
Local Variable Example

• A variable defined public static void method1(){

inside a method is for(int i = 1; i < 10; i++){


referred to as a local The scope of i
variable int j;

• The scope of a local The scope of j

variable starts from its }


}

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;

the use of the keyword ‘new’ public Employee(String name){


this.name = name;
and destroyted when the object }

is destroyed public void setName(String name){


this.name = name;
• Instance variables can be }

accessed directly by calling the public String getName(){


return this.name;
variable name inside the class. }

If the instance variable is called public void setSalary(double salary){


from outside the class, then we }
this.salary = salary;

have to use this format: public void getSalary(){


ObjectReference.Variable name }
return this.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";

variables. It is public Employee(String name){


this.name = name;

declared with the }

static keyword in a public void setName(String name){


this.name = name;
}
class public String getName(){
• Static variable are }
return this.name;

created when the public void getDepartment(){


return this.getDepartment();
program starts and }

destroyed when the public static void main(String []args){


Employee emp = new Employee("Hanry");
program stops }
emp.setSalary(3500000);

}
Primitive types VS Reference types (1)

• Every variable represents a memory location


that holds a value
• When you declare a variable, you are telling the
compiler what type of value the variable can
hold
Primitive types VS Reference
types (2)
Primitive Reference
• The value is of the • The value is a
primitive type reference to where an
• Example : object is located
int i = 1; • Example:
float j = 2.5; Circle circle1 = new
String name = Circle();
“William”;
Inner Class (1)

• Known as nested class.


• Defined in a class called Outer Class.
• Can access all class members which has been
defined in the Outer Classes
• Create a program to be simple and concise
• Can be declared with access modifier
• Can be declared with static variables.
• A static inner class could not be accessed by
non-static member from Outer Class
Inner Class (2)

• public class OuterClass{


private int data;
public void m(){ //functions that exist in the outer class
//statement
}
//an inner class which exist in the outer class
class InnerClass{
public void mi(){ //function that exist in inner class
data++; //could directly access the outer class member
m();
}
}
}
References
• Introduction to Jawa Programming. 10ed. Liang.
Chapter 9
• https://docs.oracle.com/javase/tutorial/java/java
OO/accesscontrol.html

26

You might also like