Topic 3 - Basic Concepts of Classes
Topic 3 - Basic Concepts of Classes
CLASS CONCEPT
CLASS DEFINITION
DATA MEMBERS
BASIC TYPES OF METHODS
METHODS DEFINITION
STATIC FIELDS
PREDIFINED CLASSES AND WRAPPER CLASS
INTRODUCTION
In OOP, the central modules are classes where data and the procedure to manipulate the data are encapsulated
A Java program is composed of one or more classes:
CLASSES
For this sample program, we have created two (2) classes — BicycleRegistration (the main class)
and Bicycle. So there are two source files for this program.
CLASS CONCEPT
(+)
public
modifier
CLASS DEFINITION
CLASS DEFINITION: SYNTAX
Modifier (s)
classMembers
-Used to alter the behavior of the class
- Consists of named constants, variable
- type of modifiers
declarations, methods, other classes
(public, private and static)
CLASS DEFINITION: EXAMPLE
//methods
…
}
BASIC TYPE OF METHODS:
1. Constructor
2. Accessor/ getter
4. Printer
5. Processor
METHOD DEFINITION: 1. CONSTRUCTOR
❖ If no Normal Constructor
constructor is Set all data members based on arguments.
provided, Will set data members based on what user
compiler uses specify using parameters.
default
constructor
that initializes:
METHOD DEFINITION: : 1. CONSTRUCTOR
Modifier className ()
{
constructor body
}
public Student ()
{
name = “”;
}
METHOD DEFINITION: 1. CONSTRUCTOR
Normal Constructor: Syntax
It is a constructor that receives several values from the main method
through parameters and initialize the data members of an object with
the given values
❖ Retrieve/read data members and return their values through the function type.
❖ Clients cannot directly access private instance variables, so classes will provide
public accessor/getter methods with this standard form:
❖ Also known as getter method
❖ Used to read / access the values stored in object’s variables
❖ Returns a value read
❖ No arguments (parameter list)
METHOD DEFINITION: 2. ACCESSOR @ GETTER
Syntax
Example
public double calculate
Area()
{
double area;
area = width * length;
return area;
}
EXAMPLE
public double calculateNewBalance(double withdrawal)
{
double newBalance;
newBalance = balance – withdrawal;
return newBalance;
}
OR
public void calculateNewBalance(double withdrawal)
{
double newBalance;
newBalance = balance – withdrawal;
System.out.println (“New balance: ” + newBalance);
}
STATIC METHOD
A method that does not operate on an object but receives all of its data as
arguments.
Independent function
If the method has public modifier, it is referred as a general-purpose method which
means it is constructed to be used to perform a general purpose task.
It uses the keyword static
public static void main(String[] args) {…..}
STATIC FIELDS
❖ Static field is also known as class field, where data shared by all instances of a class
❖ Static data belongs to the entire class not to individual object
❖ All objects of the class share the static data
Object 1 Object 2
showMessageDialog(null, “Good
showMessageDialog(String) Morning!”);
Java provides routine for converting String to a primitive data types or vice versa
It is called wrapper classes because the classes constructed by wrapping a class structure around the
primitive data types
WRAPPER CLASSES
Wrapper Method Description Example
Class
Integer parseInt(String) Converts a string to int int x = Integer.parseInt(s);
Class header
Attributes/ data
members/ variables
Default constructor :
initialize the object
variables to zero or null
Normal constructor :
Set all data members
based on arguments
JAVA PROGRAM
Setter/Mutator
JAVA PROGRAM
Accessor/Getter
Processor
Print
JAVA PROGRAM
Main method
Method