Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

CSC186

OBJECT ORIENTED
PROGRAMMING
Topic 3:
Basic Concepts Of Classes
(Part 1)
TOPIC COVERED

 Class concept
 Class definition
 Data members
 Constant
 Access specifiers (private and public)
 Basic types of methods
 Methods definition (methods in a concrete class)
JAVA PROGRAM ANATOMY

Java Classes (Programs)

CLASS OBJECT
Class Program App Program

-Data member (fields)


-Methods -public static void main( )
-static methods
- Constructors
- Mutators
- Accessors //how these methods can be
- Printer applied in App Program?
- Processor
CLASS CONCEPT
CLASS CONCEPT

 The dependency diagram between the two classes is as follows:

 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

 Bicycle is the programmer-defined classes.


CLASS DEFINITION

 Class is the object definition

 Itacts as a template or blueprint of an object or a


plan

 A class definition contains


1. Data member (usually specified as private modifier)
2. Methods (usually specified as public modifier)
EXAMPLE OF CLASS DIAGRAM

Account

-name : String
(-) data members
-ID : long
private
-balance : double
modifier
+Account()
+Account(String, int, double)
+Account (Account)
(+) +setAccount(String, int, double) : void
public +getName() : String methods
modifier +getID() : long
+getBalance() : double
+calculateNewBalance(double) : double
+toString() : String
JAVA CLASS

 Class header contains


 Modifierpublic
 Reserved word class
 Name of a class

 Class members contained in curly braces {…}


1. Data members
2. Methods
 May have constructor, accessor, mutator, processor or toString
(printer)
JAVA CLASS

public class Bicycle Class header

{
private String name; //Data member

public Bicycle( ) //Method


{
name = “”;
}

…….
}
DATA MEMBERS
DATA MEMBER

 Data members is the object attributes – also known as


attributes, fields, variables, or properties

 Data members can be any data type:


1. primitive or
2. object (class)

private String name;


private long ID;
Private double balance;
DATA MEMBER

 The syntax for the data member declaration is

<modifier-list> <data type> <name> ;


CONSTANT FIELD

 Their values cannot be changed.

 Use final and static modifiers.

public class Account


{
private String name;
private long ID;
private double balance;
static final double minimumBalance = 10.0;

//methods
}
MODIFIERS
MODIFIER

 Modifier is used to control access to data, methods and


classes.

 Modifier is called an accessibility modifier, or a


visibility modifier.

 Itrestricts who can have a direct access to the data


member
MODIFIER
 There are common modifiers used :
◦ public
 All program can access them
◦ private
 Can only access by the declaring class
◦ protected
 Any class in the same package or subclasses of the class can
access them even from different package
◦ default (no need to code)
 If any of the above modifiers omitted, compiler will use default
modifier which means can be accessed by any class in the same
package
EXAMPLE CLASS WITH DATA
MEMBERS AND MODIFIERS

public class Account //class name


{
//data member
private String name;
private long ID;
private double balance;

//methods

}
METHODS
METHOD

 Method represents the operation on the attributes.

 Different types of method :


 Constructor method
 Mutator – set method (setter)
 Accessor – get method (getter/retriever)
 Printer
 Processor
METHODS -
CONSTRUCTOR
CONSTRUCTOR

o Any method that has the same name as its class

o Does not have return type or void

o Is automatically called whenever object is created

o Purpose is to initialize an object’s variables

o A class can have multiple constructors with


different parameter list
CONSTRUCTOR
CONSTRUCTOR

3 types of constructors
1. Default constructor (without parameters)
2. Normal constructor (with parameters )
3. Copy constructor
METHODS –
CONSTRUCTOR
(DEFAULT)
DEFAULT CONSTRUCTOR

 Is automatically created by Java if you don’t


define a constructor for a class.

 Initializesall instance variables to default value


(zero for numeric types, null for object
references and false for boolean).

 Is created only if there is no constructor defined


in the class definition.
EXAMPLE DEFAULT CONSTRUCTOR

public Account ()
{
name = null;
ID = 0;
balance = 0.0;
}
METHODS –
CONSTRUCTOR
(NORMAL)
NORMAL CONSTRUCTOR

 Receives several values from the main method through


parameters

 Initialize the data members of an object with the given


value.

 Can be overloaded as long as the number of parameters


and/or the type of parameters are different.
EXAMPLE NORMAL CONSTRUCTOR

public Account (String n, long i, double b)


{
name = n;
ID = i ;
balance = b;
}
METHODS –
CONSTRUCTOR (COPY)
COPY CONSTRUCTOR

 A constructor that initializes a new object by using the


values of an existing object

 It is used to copy an object into another object

 The parameter of a copy constructor is an object from


the same class

 Copy constructor makes a member-by-member copy


from the argument to the object being created
EXAMPLE COPY CONSTRUCTOR

public Account (Account oldAcc)


{
name = oldAcc.name;
ID = oldAcc.ID;
balance = oldAcc.balance;
}
METHODS –
MUTATOR
MUTATOR

set method
 Is to change one or more of an object’s
values after the object is created and
initialized
 It does not return any value, void method.

 Has arguments (parameter list) to set the

variables
MUTATOR
EXAMPLE 1

public void setAccount (String n, long i, double b)


{
name = n;
ID = i ;
balance = b;
}
EXAMPLE 2

public void setName (String n)


{
name = n;
}
public void setID (long i)
{
ID = i ;
}
public void setBalance (double b)
{
balance = b ;
}
METHODS –
ACCESSOR
ACCESSOR

get method
 Is to retrieve the values stored in object’s
variables
 Returns a value

 No arguments (parameter list)


ACCESSOR
EXAMPLE

public String getName ()


{
return name;
}
public long getID ()
{
return ID;
}
public double getBalance ()
{
return balance ;
}
METHODS –
PRINTER
PRINTER

1. toString() - is a method that returns a string


representation of an object attributes

Example 1 (toString)
public String toString()
{
return (“Name :”+ name + “\nID:” + ID
+ “\nBalance :” + balance);
}
2. display() - is a method that display a string
representation of an object attributes

Example 2 (display) – Variation 1


public void display()
{
System.out.println(“Name :”+ name +
“\nID:” + ID + “\nBalance :” +
balance);
}
Example 2 (display) – Variation 2
public void display()
{
System.out.println(“Name :”+ name);
System.out.println(“ID:” + ID);
System.out.println(“Balance :” +
balance);
}
METHODS –
PROCESSOR
PROCESSOR
Method used for calculation purpose
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);
}
TEST YOURSELF
 Complete your Account class with previous example.

Account

-name : String
-ID : long data members
-balance : double
+Account()
+Account(String, int, double)
+Account (Account)
+setAccount(String, int, double) : void
+getName() : String methods
+getID() : long
+getBalance() : double
+calculateNewBalance(double) : double
+toString() : String
TEST YOURSELF

Choose ONE (1) object of a real world and complete the


class with following information:
1. Class name

2. Data members

3. Constructors
 Default
 Normal
 Copy

4. Methods
 Mutator
 Accessor
 Printer
MORE READING

 Wu C. Thomas, “An Introduction to Object-Oriented


Programming with Java.”
 Chapter 4
 Chapter 7
END.
Thank you.

You might also like