School of Computer Science
Lesson 2: Objects and Classes
Introduction to Programming
Academic year 2025-2026
Objects and classes. Review
Elements in the model are the objects that appear in the
problem domain
Objects contain properties (attributes) and behavior
(methods).
Object Properties Behavior
• Color • Stop
• Speed • Turn right
• Size • Turn left
• Fuel • Start
•… •…
2
Objects and classes. Review
Objects are organized into categories
A class defines the structure
(attributes) and behavior
(methods) of each and every
object from that class.
A class describes, in an abstract way, all the objects of
a given type.
3
Creating objects
Once a class is available, we can create objects (or
instances) of that class.
Example:
Convention
Class Objects Class names begin
with upper case
Person ann: while object names
Person begin with lower
case.
john:
Person
4
Status of an object
Objects have a state.
The state is represented by the values stored in their
attributes (or properties).
Example: Object
Class Attributes ann: Person
Person Age
Passport Age 18
Telephone Passport 23159846-L
Address Telephone 655123356
… Address Rosal 21, 3ºA
5
Data types
Types specify the nature of a piece of data.
int type refers to integer numbers.
String type refers to text fragments (a word or a phrase).
boolean (logical type) has only two values: true and false.
There are other types: double, float, char, …
6
Multiple instances
We can create many similar objects from one single
class (as many as we want).
All objects from the same class have the same
attributes (or properties).
The number, data type, and names of the attributes are the
same for all objects of a given class.
The attribute values can be different for each object.
7
Example of multiple instances
Person
int age
String passport
is an instance of… String telephone is an instance of…
String address
boolean smoker
john: Person ann: Person
age age 18
19
passport passport “23151246-L”
“15237823-P”
telephone “6553412999” telephone “655123356”
address “Quintana 16, 4ºB” address “Rosal 21, 3ºA”
smoker smoker false
true 8
Multiple instances
The number, data type and name of the
attributes are defined in the class, not in the
objects.
The Person class defines that every person object will
have 5 attributes with the previously mentioned names.
It also defines the data types for each of those
attributes.
When an object from the person class is created, it is
created with all of those attributes.
Attribute values are stored in each object.
For instance, each person will have an age different
from that of other people.
9
Methods (I)
They provide the operations that we can rely on to manipulate
an object.
We can communicate with objects calling their methods.
Usually, objects do something when calling one of their
methods.
10
Methods (II)
Methods are defined in the object’s class. All objects from a
given class have the same methods.
Methods are called on objects. The object whose method is
called is the one that will probably change.
Example c1: Square
Square c1 = new Square();
Square colour red
String colour isVisible true
boolean isVisible
makeInvisible() c1: Square
c1. makeInvisible()
colour red
isVisible false 11
Methods (III)
There are some actions which is wise to perform when
creating an object (e.g. initialization of attributes).
That is why there is one method which is called
automatically when creating an object: the
constructor.
So, we do not forget to ask the object to carry out those
actions.
12
Constructor method
Responsible of creating and initializing the object
Invoked automatically
Signature
Without parameters
public Square()
It can have parameters
public Square(String colour, boolean visible)
13
Parameters (I)
Methods may have parameters to receive additional
information to perform an action.
A method must define the data type for its parameters.
public void changeColour(String colour)
14
Parameters (II)
The header of a method is called signature and provides all the
information needed to call that method.
public void change(String colour, int size)
The fragment between brackets (…) is the information given
about the required parameter(s).
Every parameter needs a data type and a name.
15
Return values
Methods can return some information by means of a return
value.
The method’s signature indicates if the method returns or not
a value and its data type.
String getName()
String before the method’s name is the return type.
void changeColour(String colour)
The void keyword means that this method does not return any
value.
16
Set and get methods
Used to obtain and modify atributes’s values
A get method returns the value of an attribute. It is
used to obtain information from an object. Its
signature is:
public String getName()
A set method changes the value of an attribute. It is
used to modify the state of an object. Its signature is
public void setName(String newName)
17
Creating objects in a program
A Java program could need hundreds or thousands of objects.
How can we create an object within a program?
Person p1 = new Person();
18
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
What is “Person” doing ?
It allocates some memory to store
references to objects belonging to the
Person class (references, not objects).
19
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
How can I know that a reference is being used?
Because Person is not a primitive data type
like int, boolean, char.
20
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
What’s the purpose of p1?
p1
It provides a name to the memory allocated by
Person. This way, we can use that memory area
every time we need it.
21
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
What’s “new” doing?
p1
It creates a basic object belonging to the
Object class. Besides, it creates a
reference to that object.
Object
reference 22
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
What’s “Person()”?
p1
• It is a method signature.
• It is a special method: the constructor.
Object
reference 23
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
How can I know it is a constructor?
p1
Because it has the same name as the class
definition: Person.
Object
reference 24
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program
What’s “Person()” doing?
p1 It provides a wrapping around the object
created with new so it turns into a Person
object.
Person
Object
reference 25
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program What’s “=“ doing ?
p1
It is not the math symbol !
Person It copies the reference to the just created Person
object into the memory area named p1.
Object
reference 26
Creating objects in a program
Person p1 = new Person();
Inside the “memory”
My program What happens to the Person object if we
execute the following sentence?
p1
p1 = null ;
null
Person
The object still exists but the reference is lost and,
thus, we cannot have access to it.
Object
27
Exercise (homework)
Represent in a graphical way the meaning of the following
blocks.
BLOCK 1 BLOCK 2
Person ann;
Person p1 = new Person();
Person john = new Person();
Person p2 = new Person();
Person peter = new Person();
p1 = p2;
Person mary = new Person();
ann = john;
john = null;
28
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program What is “int” doing ?
p1
It allocates some memory to store
an integer number.
Person
Object
29
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program What’s “age” doing?
age
p1
It assigns a name to the memory space allocated to
store an int value so it can be used every time we
need it.
Person
Object
30
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program What’s “p1.” doing ?
age
p1
We are accessing the Person object by means of
the reference stored in the variable p1.
The dot . is an operator to have access to the
Person
methods (and attributes of the object).
Object
31
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program What’s “getAge()” ?
age
p1 It’s an invocation to a method (or method call).
Person
Object
32
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program
age Is “getAge()” a constructor?
p1
No. Constructors are called when creating new
objects. We are not creating any new object here.
Person
Object
33
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program
age Where is the method “getAge()?
p1
Methods are defined in classes.
Because p1 refers to objects belonging to the
Person class, the method getAge defined within
Person getAge() that class is called.
Object
34
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program
age What happens when “[Link]()” is executed?
p1
The method getAge() returns an integer.
Person getAge()
Object
35
Calling methods in a program
int age = [Link]();
Inside the “memory”
My program
age What’s = doing ?
p1
It is copying the integer returned by getAge()
Person getAge() into the memory allocated to the variable age
(which, by the way, was declared to store integer
Object values).
36
Exercise (I)
Considering the following class definition and objects…
Square c1 =new Square();
c1: Square
Square
String colour colour null
boolean isVisible isVisible false
int side size 0
makeVisible()
changeSize(int s) Square c2 =new Square();
changeColor(String c)
makeInvisible() c2: Square
colour null
isVisible false
size 0 37
Exercise (II)
Represent the status of the objects after executing the following
sentences.
Use double
quotation marks
[Link] (60); when writing a
[Link](); String literal value.
[Link](“blue”);
[Link] (80);
[Link]();
[Link](“green”);
[Link]();
38