Chapter 3 PDF
Chapter 3 PDF
Class concept:
Object is a variable. Class is a type of data. We declared class BUT manipulate objects
Used to group data and function/method
Object combines data and the operation on data in a single unit
A collection of a fixed number of components called member
Import Statements
Class Comment
Data Members
Methods
(incl. Constructor)
}
}
Data members
Can be private, public, protected as modifier
private (means data locally used within the class only) usually for declaring variables
for example:
public (means these methods can be called within class and also in main class)
usually as method definition for example:
Protected referred to data members that strictly can be accessed by its super class and
subclass (inheritance) for example:
1. Constructor
A member function that is used to initialized the object.
Same name as the class name.
Does not associate with any return type and void.
It will be invoked automatically each time an object of the class is created (instantiated).
It can be overloaded to provide a variety of means for initializing objects of a class.
Always define a constructor and initialize data members fully in the constructor so an
object will be created in a valid state
Declaring a constructor
A constructor is written similar to a function, but with the following differences:
No return type
No return statement
Copy constructor
Allow one object to copy the value of another object’s data members.
For example:
public Rectangle (Rectangle rect)
{ width = rect.width;
length = rect.length;
}
2. Mutator (setter)
A Mutator - Sets a property (data, attribute) of an object. Usually the word set is being
used.
• Example: - setkan data
- setkan as private (kelas lain x bole kacau)
a) void setData (int w, int l)
{ width = w;
length = l; }
b) void setLength(int l )
{ length = l; }
c) void setWidth(int w)
{ width = w; }
3. Accessor / retriever (getter)
• Return information about an object - mesti public (kelas lain bole pkai)
• Example:
//returns the length of a //rectangle
public double getLength( )
{ return length; }
• getLength()– return information about the length of a rectangle
4. Processor
A function used to perform calculation / operation to update specific data members or
might return a specific value.
• For example to calculate the area of a rectangle:
public double calculateArea()
{ return (width * length); }
5. 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:
- nk print
- print class info (attribute(name, ic, address))
public void displayArea()
{
System.out.println(“Width “ + getWidth());
System.out.println(“Length “ + getLength());
System.out.println(“The area is “ + calculateArea();
}
6. toString()
Method that returns a string representation of an object.
• For example a rectangle:
public String toString() - kena set public
{ - result masuk dlm println
return “Width “+ getWidth() +”\n” - + “Length “ + getLength()
+ “\n” + “The area is “ + calculateArea();
}
class Bicycle {
// Data Member
private String ownerName;
class BicycleApp {
public static void main(String[] args) {
class Students
{
private String name; // Data Members
private long id;
// Default Constructor
public Students()
{
name = "Unassigned";
id = 0;
}
Class StuApp
/*
Purpose: This programs tests the class Students
*/
import java.util.*;
public class StuApp
{
public static void main( String[] args )
{
Scanner in = new Scanner(System.in);
Students stu1; // declare an object reference
stu1 = new Students( ); //create an object of type Students
System.out.println(" Enter name "); //input name
String nm = in.next();
System.out.println(" Enter id");// input id
long id = in.nextLong();
stu1.setData(nm,id); // set data through mutator
System.out.println(" The student's name:” +
stu1.getName()));
System.out.println(" The student's id: " + stu1.getId( ));
System.out.println(stu1.toString( ));
}// end main
}// end class
Method Definition
Method Declaration
• public : makes the classes, data or methods are accessible from any class
• private : makes the data or methods are accessible ONLY within its own class
Constants can (should) be declared public if they are meant to be used directly by
the outside methods
Local variables
variables are declared within a method declaration and used for temporary services,
such as storing intermediate computation results.
public double convert(int num)
{
double result; //local variable
result = Math.sqrt(num * num);
return result;
}
Local, parameter & data member
member.
member.
no matching declaration.
A static method
Class method that does not operate on an object
Defined inside a class
Can be called preceded by the class name
No object need to be created to call it
Syntax:
className.methodName(parameters)
Examples:
a) public static void main(String args[])
b) Math.sqrt(16);
Note:
Math is a class, not an object
sqrt()is a static method in Math class.
A static field
A class constant that is shared by all methods of the class.
One copy per class is shared by all objects.
Constant should be made final static.
Example:
private static final float pi = 3.142;
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object
into primitive automatically. The automatic conversion of primitive into object is known and
autoboxing and vice-versa unboxing.
One of the eight classes of java.lang package are known as wrapper class in java. The list of
eight wrapper classes are given below:
Output:
20 20 20
Source: http://www.javatpoint.com/wrapper-class-in-java
Example of program using static method