Object-Oriented Programming
Object-Oriented Programming
Object-Oriented Programming
Programming
Object-Oriented Programming
• In recent years, people have been trying to provide
software solutions to increasingly complex problems.
• Object-Oriented programming encourages code
re-use.
• You must learn to be lazy!
• In object-oriented programming the emphasis is
taken away from the functionality of the program
and placed on describing the objects state and
behaviour and how the objects can interact with
each other.
Objects
• Objects are the principal component of object-
oriented programming.
• An object represents an entity (i.e. an object) in the
real world that is being modelled.
• The main features of an object are that any data
associated with the object is stored with the object
and procedures (known as methods) can also be
stored with the object.
Objects
• Let's consider objects that represent people.
• What data items can we use to represent people?
person1
name
age
address
Objects
• Each individual person will have their own values
associated with these attributes.
person1 person2
name = “John” name = “Stuart”
age = 21 age = 22
address = “High St” address = “Up Town”
person1 dog1
name = “John” name = “Fido”
age = 21 position = “Standing”
address = “High St”
Objects
• Now we need the person object to send the message
“sit” to the dog object
person1 dog1
name = “John” sit name = “Fido”
age = 21 position = “Standing”
address = “High St”
Objects
• The dog receives the message “sit” and executes a
method.
• The method changes the position of the dog to
sitting
• The diagram below shows the state of the person
and dog objects after sending the message sit
person1 dog1
name = “John” name = “Fido”
age = 21 position = “Sitting”
address = “High St”
Sending Messages
• In Java the format is always
• Object send the message Message
• This gets shortened
• Object sendTheMessage Message
• Object stm Message
• Object . Message
• Object.Message
Classes
• The class provides a definition of (or a template for)
an object.
• The information about an object (i.e. the data items
and the methods), are defined in the class.
• This means that no matter how many objects of one
particular type there might be, we only need to
define the data items and methods once.
Classes
• The diagram to represent a class is shown below
Person
name : String
age : int
address : String
Person
name : String
age : int
address : String
toString() : String
printDetails()
Classes
• The behaviour (i.e. the methods) which applies to all
of the objects that belong to a particular class is
defined only once in the class.
• All instances of a class have their own private copy of
the data items and also have access to the methods
defined in the class.
Classes
• Classes are the template from which objects are
created.
• Before we can create objects we must write the code
for the class.
• In this section we will start by developing a simple
class Person.
• Gradually, as we work through the module we will
add things to the class until we have created
complete class that demonstrates many useful
features.
Classes
• Let's start with a diagram of the class we are going to
develop.
Person
name: String
age : int
address : String
Person()
toString() : String
printDetails()
Classes
import javax.swing.JOptionPane;
public class Person {
private String name;
private String address;
private int age;
public Person() {}
output = "My name is " +this.name + " I am " +this.age +" years old \nand a I live in
" +this.address;
return output;
}
//continued on next slide
Classes
//continued from previous slide
public void displayDetails() {
String output;
output = toString();
// display message
JOptionPane.showMessageDialog(null, output, "Details of Person",
JOptionPane.INFORMATION_MESSAGE);
}
}//end class
Classes
• Notice how the attributes in the class diagram were
translated into instance variables in the code
• See how the three methods in the diagram appear as
methods in the code
• Both the declaration of the instance variables and
the methods are contained inside the declaration of
the class.
public class Person {
//code goes here
}
Objects
• Once we have declared this class we can use it.
• Here is a sample program that creates a person
object and sends it the message printDetails().
public class DemoPerson {
public static void main(String[] args) {
}
}
Objects
• Notice that to create a Person object, there are two
things involved.
• Firstly, we create a variable of type Person
• Secondly we assign that variable to a new Person
object.
Person p1 = new Person();
p1.setName("John");
p1.setAddress("Edinburgh");
p1.setAge(21);
p1.displayDetails();
p1.setAge(22);
p1.displayDetails();
}
}//end class
Get and Set Methods
• The program produces the following output
Constructors
The constructor is a special method for three reasons:
No return
type Same name as the class
public Person() {
// code goes here
}
Constructors Without Parameters
• Let's modify the constructor of the class Person so
that a simple message is printed when the
constructor is called.
public Person() {
String output = "Person constructor called";
// display message
JOptionPane.showMessageDialog(null, output, "A Constructor",
JOptionPane.INFORMATION_MESSAGE);
}//end constructor
Constructors Without Parameters
• We can now use the following demo program to see
if the constructor is called automatically.
p1.setName("John");
p1.setAddress("Edinburgh");
p1.setAge(21);
p1.displayDetails();
}
} //end class
Constructors Without Parameters
Here is the output from running the previous program
Constructors With Parameters
• We can add another constructor to the class person.
• In fact we can add as many constructors as we want
provided that the parameter type list is unique.
• This time we will add a constructor that takes three
parameters.
• The job of the constructor is to assign the instance
variables to the parameters that have been passed
in.
Constructors With Parameters
public Person(String name, String address, int age) {
setName(name);
setAddress(address);
setAge(age);
}
Constructors With Parameters
• Here is a sample program showing how we can use
the new constructor
}
}
Multiple Constructors
• You can have any number of Constructors provided
that the parameter type list is unique.
public Person() {}