Software Engineering
Module: Core of Java
Topic: Classes and Objects
TALENTSPRINT | © Copyright 2012
Classes and Objects
The content in this presentation is aimed at teaching
learners to:
• Define class and object
• Differentiate class and object
• Create simple java classes, construct and use java objects
• Define Constructor
• Explain the need for Constructor
• Create Constructors and Parameterized Constructors
• Use “this” keyword
TALENTSPRINT | © Copyright 2012
2
Classes and Objects
The Dice Game
Dice 2
Dice 1
Player 1
TALENTSPRINT | © Copyright 2012
Player 2
3
Classes and Objects
What is an object?
An object is some real or conceptual thing.
What is interesting about dice?
Its face value
What does a dice do?
It rolls.
What happens when a dice rolls?
Its face value changes.
An object is a self-contained entity that consists of both data
(properties) and methods (behaviour ) to manipulate the data.
TALENTSPRINT | © Copyright 2012
4
Classes and Objects
Properties & behaviors of Dice Objects
Dice 1 has a face value
Dice 2 also has a face value
.
.
.
Dice n also has a face value
Dice 1 rolls
Dice 2 also rolls
.
.
.
Dice n also rolls
TALENTSPRINT | © Copyright 2012
Any dice has: A face value
Any dice: Rolls
In other words,
Any object that is a dice has a
face value and rolls.
In other words,
An object if it is of type dice
has a face value and rolls.
An object if it is of class dice has
a face value and rolls.
5
Classes and Objects
Classes in Dice Game
Player Class
TALENTSPRINT | © Copyright 2012
Dice Class
6
Classes and Objects
What is a Class
A class is a description of a set of objects that share
the same attributes and behaviour (operations)
TALENTSPRINT | © Copyright 2012
7
Classes and Objects
Class vs. abstraction – Class Exercise
Recall the following definitions of abstraction
and explain the connect between abstraction
and class:
Tony Hoare
EXERCISE
“Abstraction arises from a recognition of similarities
between certain objects, situations, or processes
in the real world, and the decision to concentrate
upon those similarities and to ignore for the time
being the differences.”
“An abstraction denotes the essential characteristics
of an object that distinguish it from all other
kinds of objects and thus provide crisply defined
conceptual boundaries, relative to the perspective of
Grady Booch the viewer.”
TALENTSPRINT | © Copyright 2012
8
Classes and Objects
EXERCISE
List out the properties & behaviors of Player class.
Hint :
What is interesting about player?
What does a player do?
TALENTSPRINT | © Copyright 2012
9
Classes and Objects
Properties & Behaviors of Classes in Dice Game
Dice
Player
Properties
Properties
faceValue
name
Behaviors
value
roll
Behaviors
throw
TALENTSPRINT | © Copyright 2012
10
Classes and Objects
Properties & Behaviors of Dice Class
class Dice{
Dice
Properties
faceValue
Behaviors
roll
int faceValue;
void roll(){
//random no. between 1 and 6
faceValue
=(int)((Math.random())*10)%5+1;
}// end of roll
}// end of class
TALENTSPRINT | © Copyright 2012
11
Classes and Objects
Properties & Behaviors of Player Class
class
Player
Properties
name
String name;
int value;
void throwDice(Dice diceOne, Dice
diceTwo){
diceOne.roll();
diceTwo.roll();
value = diceOne.faceValue +
diceTwo.faceValue;
value
Behaviors
throwDice
Player{
}
}// end of class
TALENTSPRINT | © Copyright 2012
12
Classes and Objects
Programmatic definition of Class
A class is a representation for a set of objects that are
data abstractions with an interface of named operations
(methods) and hidden local state (attributes).
TALENTSPRINT | © Copyright 2012
13
Classes and Objects
Behavior in an O-O program
In Object-oriented programming, programs are organized as
cooperative collections of objects.
Each object represents an instance of some class.
Objects communicate by passing messages (by calling methods).
• A message is always given to some object.
• The response to a message depends upon the class of the Object.
All messages have three identifiable parts.
1. Message Receiver
playerOne.throwDice(diceOne,diceTwo)
2. Message
TALENTSPRINT | © Copyright 2012
3. List of arguments
14
Classes and Objects
Making Objects Collaborate
Steps in making objects collaborate with each other:
TALENTSPRINT | © Copyright 2012
Step 1
Define Classes
Step 2
Create Objects
Step 3
Pass Messages
15
Classes and Objects
Dice Game Class and main method
class DiceGame{
public static void main (String args [] ) {
Dice diceOne;
diceOne = new Dice();
// create diceTwo object
EXERCISE
Dice diceTwo;
diceTwo = new Dice();
Player playerOne;
playerOne = new Player();
playerOne.throwDice(diceOne,diceTwo);
// create playerTwo object
Player playerTwo;
playerTwo = new Player();
// ask playerTwo to throwDice
playerTwo.throwDice(diceOne,diceTwo);
if (playerOne.value > playerTwo.value) {
System.out.println("Player1 Wins");
}
else{
System.out.println("Player2 Wins");
}}// end of main()
}//end of class DiceGame
TALENTSPRINT | © Copyright 2012
16
Classes and Objects
Defining a Class
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameterlist) {
// body of method
}
type methodname2(parameterlist) {
// body of method
}
// ...
type methodnameN(parameterlist) {
// body of method
}
}
TALENTSPRINT | © Copyright 2012
Example :
class Dice{
int faceValue;
void roll(){
faceValue =
(int)((Math.random())*10)%5 +
1;
}//
end of roll
}// end of class
17
Classes and Objects
Attributes Declaration
<modifier>
<data type>
Modifiers
Data Type
private
String
TALENTSPRINT | © Copyright 2012
<name>
Name
ownerName
18
Classes and Objects
Method Declaration
<modifier> <return type>
{ <statements>
}
<method name> ( <paramaters> )
Modifiers
Return Type
Method Name
public
void
setOwnerName
TALENTSPRINT | © Copyright 2012
Parameter
Statements
String Name ownerName= name;
19
Classes and Objects
Creating Objects
ClassName refVariable;
refVariable = new
Constructor();
or
ClassName refVariable =
new Constructor();
TALENTSPRINT | © Copyright 2012
Example :
Example :
Dice diceOne ;
diceOne = new Dice();
or
Dice diceTwo = new Dice();
20
Classes and Objects
Using Objects
Dice diceOne = new Dice();
diceOne.roll();
int value = diceOne.faceValue();
TALENTSPRINT | © Copyright 2012
21
Classes and Objects
Using Objects
There is one more object in the game. It is the
diceGame object itself. Because every object in
Java has to belong to some class, let's define
the DiceGame class.
An object is some real or conceptual thing.
TALENTSPRINT | © Copyright 2012
22
Classes and Objects
Properties & Behaviors of Dice Game Classes
Properties
PlayerOne,PlayerTwo,Dice1,Dice2;
Behaviors
Play
class DiceGame{
Player playerOne, playerTwo;
Dice Diceone, DiceTwo;
void play(){
diceOne = new Dice();
diceTwo = new Dice();
playerOne = new Player();
playerTwo = new Player();
playerOne.throwDice(diceOne,diceTwo);
playerTwo.throwDice(diceOne,diceTwo);
if (playerOne.value > playerTwo.value) {
System.out.println("Player One Wins");
}
else{
System.out.println("Player Two Wins");
}
}// end of play()
}//end of class
TALENTSPRINT | © Copyright 2012
23
Classes and Objects
EXERCISE
Create the main method for the DiceGame class
and complete the game.
TALENTSPRINT | © Copyright 2012
24
Classes and Objects
Using Objects
Dice diceOne = new Dice();
diceOne.roll();
int value =
diceOne.getFaceValue();
TALENTSPRINT | © Copyright 2012
Constructor
25
Classes and Objects
Constructor
Is a function that is implicitly invoked when a new object is created.
Performs the necessary actions in order to initialize the object.
Is a special function with the same name as the class.
Example :
class Car{
public Car(){ // this is constructor
. . .
}
}
TALENTSPRINT | © Copyright 2012
26
Classes and Objects
Program
JVM Memory
Constructor of
class Car{
Stack
Heap
the Car Class
String name;
memory
memory
public Car(){
name=“My Car”;
Name My Car
}
}
class MainClass{
public static void main(String
args[]){
Reference variable
(Declaration)
Car c ;
c = new Car();
}
Calls the
}
constructor
Allocates memory for
the object
(for Instantiation)
TALENTSPRINT | © Copyright 2012
27
Classes and Objects
Constructor
Declaration
Associating a variable name with an object type.
Instantiation
Is done by the new keyword, a JAVA operator that
creates the object.
Initialization
A call to the constructor follows this new operator.
This in turn, initializes the new object.
TALENTSPRINT | © Copyright 2012
28
Classes and Objects
Program
class Car{
String name;
public Car(){
name=“My First Car”;
}
}
class MainClass{
public static void main(String
args[]){
Car c1,c2;
c1 = new Car();
c2 = new Car();
}
}
JVM Memory
Stack
memory
Heap
memory
c1
Name My First
Car
c2
Name My First
Car
Does it sound meaningful to have the same
name for both the cars: “My First Car”?
TALENTSPRINT | © Copyright 2012
29
Classes and Objects
Parameterized Constructor
Program
class Car{
String name;
public Car(String carName){
name=carName;
}
}
class MainClass{
public static void main(String
args[]){
Car c1,c2;
c1 = new Car(“My First Car”);
c2 = new Car(“My Second Car”);
}
}
TALENTSPRINT | © Copyright 2012
JVM Memory
Stack
memory
Heap
memory
c1
Name My First
Car
c2
Name My Second
Car
30
Classes and Objects
Program
class Car{
String name;
public Car(){
name=“My First Car”;
}
public Car(String carName ){
name=carName;
}
}
class MainClass{
public static void main(String args[]){
Car c 1,c2;
c1 = new Car();
c 2 = new Car(“My Second Car”);
}
} It is possible for a class to have more
TALENTSPRINT | © Copyright 2012
JVM Memory
Stack
memory
Heap
memory
c1
Name My First
Car
c2
Name My Second
Car
than one constructor?
31
Classes and Objects
Program
class Car{
String name;
public Car(){
name=“My Car”;
}
}
class MainClass{
public static void main(String
args[]){
Car c 1,c2;
c1 = new Car();
c2 = c1;
}
}
TALENTSPRINT | © Copyright 2012
JVM Memory
Stack
memory
Heap
memory
c1
Name My Car
c2
32
Classes and Objects
Program
class Car{
String name;
public Car(){
name=“My Car”;
}
}
class MainClass{
public static void main(String
args[]){
Car c 1,c2;
c 1= new Car();
c 2= new Car();
c2=c1;
}
}
JVM Memory
Stack
memory
Heap
memory
c1
Name My Car
c2
Name My Car
Can a class perform an action on itself?
TALENTSPRINT | © Copyright 2012
33
Classes and Objects
Program
class Car{
String name;
public Car(){
name=“My First Car”;
}
public Car(String name ){
this.name=name;
}
Refers to the
}
current object
class MainClass{
public static void main(String
args[]){
Car c 1,c2;
c1 = new Car();
c 2 = new Car(“My Second Car”);
}
}
TALENTSPRINT | © Copyright 2012
34
Classes and Objects
“this” keyword
• Is a reference to the
current object within
an instance method
or constructor.
• Explicit constructor
invocation -- call
another constructor
in the same class.
TALENTSPRINT | © Copyright 2012
Example for explicit constructor invocation :
public class MyClass {
private int x, y, a, b;
public MyClass() {
this(0, 0, 0, 0);
}
public MyClass(int a, int b) {
this(0, 0, a, b);
}
public MyClass(int x, int y, int a,
int b) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
}
...
}
35
Classes and Objects
Will this work?
Class Test{
// no constructor
int value=10;
void display(){
System.out.println(“Value is : ”+value);
}
}
class MainClass{
public static void main(String args[]){
Test test = new Test(); // creating object
with constructor Test()
test.display();
}
}
TALENTSPRINT | © Copyright 2012
36
Classes and Objects
What if no constructor is defined in a class ?
At the time of compilation the Java Compiler will
insert an empty Definition Of Default Constructor to
the java class and then it will compile the program.
The empty Definition Of Default Constructor of any
class will be as follows:
public class-name(){}
TALENTSPRINT | © Copyright 2012
37
Classes and Objects
Create the following class:
Class Test{
int sum(int valueOne, int valueTwo){
return valueOne + valueTwo;
}
}
TRY IT
EXERCISE
Write a main method that instantiates this class,
calls sum method and prints the return value.
Compile and run the program.
TALENTSPRINT | © Copyright 2012
38
Classes and Objects
TALENTSPRINT | © Copyright 2012
39
© Copyright 2026 Paperzz