Object-Oriented Programming

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

Object-Oriented

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”

• This is private data which belongs to the object.


Objects
• In addition to data items an object also has
operations (methods) which define the behaviour of
the object.
• The methods are able to act upon the objects data by
changing it or querying its value.
• What methods (i.e. behaviour) might a person object
have?
Ask their name
Change their age
Objects
• What do objects do?
• Nothing!
• Objects must communicate with other objects
Objects
• Objects, either in the real world or in a computer
program, are of little use of they stand alone and
cannot interact with other objects.
• Objects communicate by sending messages
Objects
• Let’s consider a person, say ‘John’, telling a dog, say
‘Fido’, who is currently standing up to sit down.
• In this case John might give the command sit to Fido.
• Let’s see how to go about modelling that with our
objects

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

• The class name is shown in the top part of the box.


• Beneath that is a list of attributes and their types.
Classes
• As well as attributes methods can also be shown in a
class diagram.
• These appear in another box beneath the attributes.

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() {}

public String toString() {


String output;

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) {

Person p1 = new Person();


p1.displayDetails();

}
}
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();

Declare Assign to new


variable person object
Get and Set Methods
• Get and Set methods play a specific role in class.
They allow the class to query an instance variable
(the get method) or change the value of an instance
variable (the set method).
• You will often (but by no means always) find a get
and set method for each of the instance variables in
a class.
• By convention the get and set methods have the
same name as the instance variable preceded by the
word 'get' or the word 'set'. For example,
getAge()
setAge(int)
Get and Set Methods
• Here are the get and set methods for the instance
variable age

public int getAge() {


return this.age;
}

public void setAge(int age) {


this.age = age;
}
Get and Set Methods
• Now we can use the set methods to change the value
of the properties.
• Can you predict the output from this program?
public class DemoGetterAndSetter {
public static void main(String[] args) {

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:

 It cannot return a value


 It has the same name as the class
 It is called automatically when an instance
of the class is created.
Constructors Without Parameters
• Let's have a look at the definition of the constructor

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.

public class DemoConstructor {


public static void main(String[] args) {

Person p1 = new Person();

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

public class DemoConstructorWithParameters {


public static void main(String[] args) {

Person p1 = new Person("John", "Edinburgh", 21);


p1.displayDetails();

}
}
Multiple Constructors
• You can have any number of Constructors provided
that the parameter type list is unique.

public Person() {}

public Person(String name, String address, int age) {


setName(name);
setAddress(address);
setAge(age);}

public Person(String name, int age) {


this(name, "unkown", age);
}
Tutorial Time
• Now it’s your turn ...

You might also like