Classes - Objects
Classes - Objects
CSE Department
Faculty of Engineering
University of Mauritius
Overview
2
Objects
An object is an abstraction because it represents
only those relevant features of a thing in the
problem-domain that are deemed relevant to the
current purpose and hides those features that are
not relevant.
Objects = state+behaviour+Identity
3
State
The state of an object represents the condition
that an object is in at a given moment i.e. the
values of all its properties (attributes).
e.g. The state of an employee object could be:
6
Class
A class describes a group of objects with similar
properties (attributes),common behaviour
(operations/methods), and common relationships to
other objects.
7
Class structure
<modifier/s> class ClassName{
}
Modifiers can be: public, abstract | final
E.g.
class Car{
//other codes
}
8
Properties
<modifier/s> <dataType>
propertyName [=var_initializer ]
9
Methods/Behaviours
<modifier/s> <return_dataType> methodName(parameters){
}
E.g.
10
Class with attribute and method
class Student{ //className = ‘Student’
private name = “Roushdat”; // propertyName = ‘name’
private id; // propertyName = ‘id’
public void setId(String id){ //methodName=‘setId’
this.id =id; // setting value classwide variable ‘id’ to value of parameter
‘Id’
}
}
11
Main Class
The main class allows the execution of a java
program.
A java program can have many classes, but it
should have at least 1 main class to be
executable
The main class should contain the main method.
E.g of a main class
class StudentMain{ //main class for student program
Public static void main(String[] args){ //main method of
the class
Student uomStudent= new student(); // instantiates an
object uomStudent from class Student
}
12
Constructor (1) – Default Constructor
The purpose of a constructor is to create an object based on a
class’s blueprint.
13
Constructor (2) – Defined Constructor
Constructors can also take parameters.
15
Accessors
Accessor methods read property
values and are conventionally named
get<FieldName>()
E.g.
public int getMark() // accessor method for field mark
{ return Mark; // mark is declared: private int Mark;
}
16
Mutators
Mutator methods set property values and are often
named set<FieldName>() etc.
Mutators can be used to ensure that the property's value
is valid in both range and type.
E.g.
public int setMark(int myMark) //mutator method
{ if (myMark >= 0)
this.mark = myMark
}else
{
this.mark = 0;
}
17
Specimen - Exercise 1a
Create a class Person that has classwide
attributes name and age.
Include Accessor and mutator methods for
every field.
Include a default constructor that initialises
name to ‘Person’ and age to18
Include a defined constructor that takes in
values for the two fields (name and age) and
initialises the Person object.
Include a method display to display the name
and age
18
Answer to Exercise 1a
public class Person { public int getAge(){
private String name; return age;
private int age; }
19
Specimen - Exercise 1b
Create a class PersonMain.
20
Answer to Exercise 1b
public class PersonMain {
public static void main(String[] args){
Person Person1 = new Person();
Person1.setName("Jeeten");
Person1.setAge(40);
System.out.println("Name: "+Person1.getName());
System.out.println("Age: "+Person1.getAge());
Person Person2 = new Person("Roushdat",25);
Person2.display();
}
}
21
Output from Exercise 1
22
Specimen - Exercise 2
You are required to write a program that will
display car details as follows.
Hint:
Create a class Car and the main class CarMain.
In CarMain create an instance of Car called
myCar, then invoke display() on myCar.
23
Specimen - Answer to Ex 2 - i
public class Car {
public static final double VAT = 0.15;
private String CarNo;
private String make;
private String model;
private double price;
public Car(){
CarNo = "27JL11";
make = "Nissan";
model = "Skyline R34";
price = 1950000.00;
}
25
Static – Without instance
Static variable
Sometimes it is desirable to have a
variable that is shared among all
instances of a class. You can achieve
this effect by marking the variable with
the keyword static. Such a variable is
sometimes called a class variable to
distinguish it from a member or
instance variable which is not shared.
26
Static variable example
class Count { public class CountObject {
private int serialNum; public static void main (String
args[]) {
private static int counter =0;
Count product1 = new Count();
Count() {
product1.display();
counter ++;
Count product2 = new Count();
serialNum = counter;
product2.display();
}
Count product3 = new Count();
void display() {
product3.display();
System.out.println(“Serial number:
“ }
+serialNum); } }
}
27
Static Methods
Sometimes you need to access program code when you do
not have an instance of a particular object available.
28
Static methods example
class GeneralFunction { class StaticMethods {
static int addUp(int x, int y) { public static void display () {
return x+y; System.out.println(“This is a static
method.”);
}
}
}
public static void main(String
class UseGeneral {
args[]) {
public void useAdd() {
UseGeneral us = new
int a = 9, b = 10; UseGeneral();
int c = //call the useAdd() to call static
GeneralFunction.addUp(a,b); addUp()
System.out.println(“addUp() gives “ us.useAdd();
+c);
display();
}
}
}
}
29
Representing Classes In UML
30
Some Common Mistakes
Case sensitivity
Java is case-sensitive. Among other things, this means that, in a Java
program, the letter P isn’t the same as the letter p.
31