Content Analysis
Object Oriented Design and Programming
Week 9
Emran Salahuddin, Anchal Shrestha, Chaitalia Samani (Sydney)
Hanspreet, Allan Ng(Melbourne)
Kent Institute Australia Pty. Ltd.
ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051
Version 2 – 18th December 2015
1
SLIDE TITLE
Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning
2
2
Programming Logic and Design Ninth Edition
Principles of Object Oriented Design and programming 1- definition and creation of classes and objects, constructors and destructors
3
Programming Logic and Design, Ninth Edition
Objectives
4
In this chapter, you will learn about:
Classes and Objects
Constructors
Destructors
Setter and Getter methods
Programming Logic and Design, Ninth Edition
4
Recap
By now, you have learnt about
Programming structures
Arrays
Programming Standards
Test plans
You have basic knowledge of class in which you were writing your program
Let’s discuss in detail about classes and objects.
5
Programming Logic and Design, Ninth Edition
Object Oriented Programming
Object-oriented programming (OOP) is a style of programming which
Focuses on an application’s data and the methods you need to manipulate that data.
Object-oriented programming adds several new concepts to programming and involves a different way of thinking.
6
Programming Logic and Design, Ninth Edition
Objects
Objects both in the real world and in object-oriented programming are made up of attributes and methods.
Attributes are the characteristics that define an object.
For example, some of your automobile’s attributes are its make, model, year and purchase price.
Other attributes include whether the automobile is currently running, its gear, its speed and whether it is dirty.
All automobiles possess the same attributes, but not, of course, the same values for those attributes
7
Programming Logic and Design, Ninth Edition
Classes
A group or collection of objects with common properties.
An instance of a class is an existing object of a class.
Therefore, your black Range Rover Automobile with the dent can be considered an instance of the class that is made up of all automobiles.
So, a class is a category of things; an object is a specific instance of a class.
8
Programming Logic and Design, Ninth Edition
Classes and Objects
A class definition is a set of program statements that tell you
Characteristics of the class’s objects
The methods that can be applied to its objects.
9
Programming Logic and Design, Ninth Edition
Classes and Objects
Another example
Dish is a class.
When you know an object is a Dish, you know it can be held in your hand and you can eat from it.
The specific object myBlueDinnerPlateWithTheChipOnTheEdge is an instance of the Dish class
So is auntJanesAntiquePunchBowl and myCatsFoodBowl
10
Programming Logic and Design, Ninth Edition
Classes and Objects
A class can contain three parts:
Every class has a name.
Most classes contain data, although this is not required.
Most classes contain methods, although this is not required.
Programmers often use a class diagram to illustrate class features.
A class diagram consists of a rectangle divided into three sections
11
Programming Logic and Design, Ninth Edition
Class Diagram
A class diagram consists of a rectangle divided into three sections.
The top section contains the name of the class
The middle section contains the names and data types of the attributes
The bottom section contains the methods
12
Programming Logic and Design, Ninth Edition
Activity
Think about an Employee
What are features that an employee class can have?
What are the functions that can be done with this class?
13
Programming Logic and Design, Ninth Edition
Employee Class Diagram without access specifiers
14
Programming Logic and Design, Ninth Edition
Access Specifiers
Let’s think about one scenario:
If your computer has a warranty and something goes wrong with its operation.
You cannot open the CPU yourself, remove and replace parts, and then expect to get your money back for a device that does not work properly.
Instead, when something goes wrong with your computer, you must take the device to the manufacturer.
The manufacturer guarantees that your machine will work properly only if the manufacturer can control how the internal mechanisms of the machine are modified.
15
Programming Logic and Design, Ninth Edition
Access Specifiers
Like last scenario:
You might design a class that performs a complicated statistical analysis on some data and stores the result.
You would not want others to be able to alter your carefully crafted product.
You can keep it private by using access specifiers.
An access specifier (or access modifier) is an adjective that defines the type of access outside classes will have to the attribute or method
16
Programming Logic and Design, Ninth Edition
Access Specifiers
There are four types of access specifiers in java
Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
17
Programming Logic and Design, Ninth Edition
Class Diagram with Access Specifier
Use symbols as follows:
Public +
Private –
Protected #
Default ~
18
Programming Logic and Design, Ninth Edition
Classes can contain public data and private methods, but it is common for most data to be private and most methods to be public.
Static and Instance variables
The second section of class diagram represents data or features or variables of class.
These variables can be static or instance variables.
Watch this video to understand the static and instance variables:
https://www.youtube.com/watch?v=jYNUsgHV_EU
19
Programming Logic and Design, Ninth Edition
Methods
Third section of class diagram has methods which can
Constructor
Getters
Setters
Other methods to perform specific tasks related to class
20
Programming Logic and Design, Ninth Edition
Complete class diagram
21
Programming Logic and Design, Ninth Edition
Class name
Constructor
Setters
Getters
Other methods
List of variables
Understanding Constructors
Creating objects of class
Do you remember that line:
Scanner Sc= new Scanner(System.in);
Sc is name of object of Scanner class.
new is keyword that we use to create objects.
Writing Scanner(System.in) means that we are using Constructor of scanner class to create objects.
Similarly, you can create objects of your designed classes with the help of constructor.
22
Programming Logic and Design, Ninth Edition
Understanding Constructors
23
Constructor
A method that has the same name as the class
Establishes an object
Constructors fall into two categories:
Default constructor
Requires no arguments
Non-default or parameterized constructor
Requires arguments
Programming Logic and Design, Ninth Edition
23
Understanding Constructors (continued)
24
A class can have three types of constructors:
Default constructor –
automatically-created default constructor exists in a class in which the programmer has not explicitly written any constructors
Programmer-written default constructor can reside in any class and replaces the automatically-created one
Non-default or parameterized constructor
Written by programmer with one of more parameters
Programming Logic and Design, Ninth Edition
24
Default Constructors
25
Default constructor for the Employee class
Establishes one Employee object with the identifier provided
Declare constructors to be public so that other classes can instantiate objects that belong to the class
Write any statement you want in a constructor
Place the constructor anywhere inside the class
Often, programmers list the constructor first
Programming Logic and Design, Ninth Edition
25
Default Constructors (continued -1)
26
Programming Logic and Design, Ninth Edition
26
27
Non-default Constructors
Choose to create Employee objects with values that differ for each employee
Initialize each Employee with a unique hourlyWage
Write constructors that receive arguments
Employee partTimeWorker(8.81)
Employee partTimeWorker(valueEnteredByUser)
When the constructor executes
Numeric value within the constructor call is passed to Employee()
Programming Logic and Design, Ninth Edition
27
Nondefault Constructors (continued)
Once you write a constructor for a class, you no longer receive the automatically written default constructor
If a class’s only constructor requires an argument, you must provide an argument for every object of that class you create
28
Programming Logic and Design, Ninth Edition
28
Understanding Destructors
29
Destructor
A method that contains the actions you require when an instance of a class is destroyed.
Instance destroyed
When the object goes out of scope.
If you do not explicitly create a destructor for a class, one is provided automatically
Declare a destructor
Identifier consists of a tilde (˜) followed by the class name
Programming Logic and Design, Ninth Edition
29
Understanding Destructors (continued -1)
30
Cannot provide any parameters to a destructor
Empty parameter list
Only one destructor per class
A destructor has no return type
Programs never explicitly call a destructor
Invoked automatically
The last object created is the first object destroyed.
Programming Logic and Design, Ninth Edition
30
Setters
A method which is used for updating or setting the value of a variable is called setter method in Java.
This method is also known as mutator method.
By using the setter method, you can modify the value of a variable.
Naming convention for setter method:
setXyz() where Xyz is the name of the variable.
31
Programming Logic and Design, Ninth Edition
Setters
For setting the value, you need to pass the value that you want to set.
Method signature is as follows:
AccessSpecifier returntype setXyz(datatype variable)
AccessSpecifier will be depending upon the need of program.
Return type is mostly void because it does not return any value except in some cases where you want to return Boolean value to make sure that value has been set.
It receive variable that need to be set as parameters and need to mention data type of it.
32
Programming Logic and Design, Ninth Edition
Setters
Consider a Rectangle class to create various rectangles of different dimensions.
It can have length and width as private variables.
To set their values, you should have setter methods in class.
33
Programming Logic and Design, Ninth Edition
Setters
34
Programming Logic and Design, Ninth Edition
Getters
A method which is used to retrieve/get the value of a variable or return the value of the private member variable is called getter method in Java.
This method is also known as an accessor method.
For every private variable, you should create a getter method.
Depending on the access level you want to give to the variable, you can set the access modifier of its getter method.
If you declare instance variables as private, you add public getter methods for each one.
Naming convention for getter method:
getXyz() where Xyz is the name of the variable.
35
Programming Logic and Design, Ninth Edition
Getters
To get the value, getter should return the value. Method signature is as follows:
AccessSpecifier returntype getXyz()
AccessSpecifier will be depending upon the need of program.
Return type is according to the value you are returning.
Next slide, same example of rectangle with getter methods.
36
Programming Logic and Design, Ninth Edition
Getters
37
Programming Logic and Design, Ninth Edition
Other methods
Methods that perform some functions on the data.
You can write as many as you want.
Have a look on file Rectangle.java to get an idea of other methods.
One of the most common method is toString()
Implementing toString method in java is done by overriding the Object’s toString method.
Keep this in mind, we will discuss this next week after inheritance.
38
Programming Logic and Design, Ninth Edition
Testing the classes
You must be wondering what is the purpose of class.
Whatever we have created is just a dummy class.
How can we use this class?
How to create object of this class?
Ideal way is to create a new class called test class and write main method in it.
Let us do it for Rectangle Class.
We will create a new test class.
39
Programming Logic and Design, Ninth Edition
Creating objects
Recall from slide no 20
Follow the same to create an object
Rectangle R1= new Rectangle();
Rectangle R2= new Rectangle(6.5,9.5);
First line will create an object and give it default values that you have entered in constructor.
Second line will create an object and give it the values as you have passed.
We can use print statements to print the values of this Rectangle using getter method.
40
Programming Logic and Design, Ninth Edition
Using methods of class
You can use objects to use methods of a class.
The (.) operator is also known as member operator it is used to access the member of a package or a class.
Now to access setLength() method of class, you will write R1.setLength(5.5)
It will set the length of object named R1 to 5.5
41
Programming Logic and Design, Ninth Edition
“this” operator
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.
You can refer to any member of the current object from within an instance method or a constructor by using this.
Watch this following video to understand:
https://www.youtube.com/watch?v=kZdwXMzMwYY
42
Programming Logic and Design, Ninth Edition
Tutorial Activity
Rectangle class is provided on moodle.
You need to test it by creating a different class.
Instructions are given in tutorial document.
43
Programming Logic and Design, Ninth Edition
kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051
44
44