4 Chapter 3 - Basic Concept of Classes & Encapsulation
4 Chapter 3 - Basic Concept of Classes & Encapsulation
Basic
Concept of
Classes ﻣﺤﻤﺪ ﺻﻮﻓﯿﺎن ﺳﻠﯿﻤﺎن
Dr. Mohd Suffian Sulaiman
College of Computing, Informatics & Mathematics
Universiti Teknologi MARA
Learning Outcome
Import Statements
Class Comment
class {
class {
Class Name
Data Members
Methods
(incl. Constructor)
}
}
Modifiers
Modifiers in Java are keywords that are used to define the access and scope of
classes, variables, methods, and other program elements in Java. They are used to
control the visibility and accessibility of the program elements.
Java provides four access modifiers: public, protected, private, and the default (no
keyword).
private - restricts access to only the class in which the program element is
declared, for example:
private int num1, num2;
public - allows other classes and objects to access a program element from
anywhere in the program, for example:
public int area(){
return width*length;}
protected - allows a class member to be accessed by any subclass of the class in
which it is declared, as well as any other class in the same package, for example:
protected String name;
Example : class, attribute and method
Create a class called “Myclass” with two attributes, x and y with private
modifiers. Initialize x as 3 and y as 5 :
In Java, dot notation refers to the use of the dot (.) operator to access
members of an object or class.
When you create an object from a class, you can use the dot notation to
access the members (variables and methods) of that object.
E.g., An object myObj used dot notation to access the next() method in
Scanner class.
Object creation from a class (1st approach)
‘new’ constructor
class object
keyword method
object creation
Invoking data member
Output:
5
Object creation from a class (2nd approach)
class object
MyClass myObj;
myObj = new MyClass( );
‘new’ constructor
keyword method
object creation
Invoking data
member Output:
5
Basic types of methods
Constructor
Default
Normal / Parameterized
Copy
Setter / Mutator
Getter/ Accessor / Retriever
Processor
Printer
toString() method (also type of printing method)
Constructor
Output:
0.0
null
Normal/parameterized constructor
//normal constructor
public Rectangle (double w, double l){
width = w;
length = l;
}
Output:
0.0
0.0
Example: normal/parameterized constructor
//copy constructor
public Rectangle (Rectangle rect) {
width = rect.width;
length = rect.length;
}
Output:
10.0 and 15.0
10.0 and 15.0
Output:
Setter / Mutator
Output:
10.0 and 15.0
Getter / Accessor / Retriever
Output:
10.0
15.0
Processor
Output:
150.0
Printer
A function used to display information of a class. Usually
the word display or print had being used.
For example to display all information about the
object of a rectangle:
publicvoid
public voiddisplayArea(){
displayArea(){
System.out.println(“width:“+getWidth());
System.out.println(“Width“+getWidth());
System.out.println(“length:“+getLength());
System.out.println(“Length“+getLength());
System.out.println(“the area is:“+calculateArea();
} System.out.println(“The area is“+calculateArea();}
Output:
width: 10.0
length: 15.0
the area is: 150.0
toString()
Method that returns a string representation of an object.
For example:
Output:
Width 10.0
Length 15.0
The area is 150.0
Basic types of methods application:
Example 1
• Create two classes, Bicycle (class that has all the attributes and methods)
and BicycleAPP (class that contain the main() method (code to be executed))
• The relationship of BicycleApp class to Bicycle class is dependency.
Relationships: