0% found this document useful (0 votes)
2 views

Lecture 1

1

Uploaded by

dovanminh253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1

1

Uploaded by

dovanminh253
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

61FIT3SE1 - Software Engineering 1

Lecture 1
Review of OOP
Outline
• OOP and Procedural Programming
• Encapsulation
• Inheritance
• Polymorphism

2
References

• Kathy, Bert & Trisha, Head First Java, chapter 2


• Kathy, Bert & ElisaBeth, OCP Java SE8 programmer II Exam Guide,
chapter 2
• Guttag, Liskov, Program development in Java, chapter 7.9

3
OOP and Procedural Programming

4
OOP and Procedural Programming

• Procedural programming uses a list of procedures to tell the computer


what to do step-by-step. The procedures are the functions, routines, or
subroutines that consist of the computational steps required to be
carried.
• OOP uses a set of objects that interacts with each other to achieve the
program’s intended behavior.

5
OOP and Procedural Programming

• Requirement: there will be shapes on GUI, a square, a circle, and a


trigangle. When the user clicks on a shape, the shape will rotate
clockwise 360 degree and play a .flac song file specific to that shape.

Procedural programming : P1 OOP: P2

Write procedures: 3 classes:


Square, Circle, Triangle
rotate(shapeNum) {
// make the shape rotate 360º
}
playSound(shapeNum) {
// use shapeNum to lookup which
// Flac sound to play, and play it
}

6
OOP and Procedural Programming

There is a requirement change: There will be an amoeba shape on


the screen, with the others. When user clicks on the amoeba, it
will rotate like others, and play .mp3 sound file.
Procedural programming : P1 OOP: P2
The rotate procedure would still work; But playSound would have to new class:
change. Amoeba

playSound(shapeNum) {
// if the shape is not an amoeba
// use shapeNum to lookup which
// Flac sound to play, and play it
// else
// play amoeba .mp3 sound}

Touch previously tested code so need to change Don’t have to touch code he’d already tested and delivered. Only test
new added code.
code test and test all code again.

7
OOP and Procedural Programming

• There is a mistake in requirement analysis. That’s


not how the amoeba is supposed to rotate.
O
• Turns out, both programmers had written their
rotate code like this:
• determine the rectangle that surrounds the
shape.
• calculate the center of that rectangle, and O

rotate the shape around that point.


• But the amoeba shape was supposed to rotate
O
around a point on one end, like a clock hand.

8
OOP and Procedural Programming

Procedural programming : P1 OOP: P2

Add rotation point arguments to the rotate procedure. modified the rotate method, but only in the Amoeba class.

rotate(shapeNum, xPt, yPt) {


// if the shape is not an amoeba,
// calculate the center point
// based on a rectangle,
// then rotate
// else
// use the xPt and yPt as
// the rotation point offset
// and then rotate
}

A lot of code was affected. So need to test all code again. Don’t touch the tested, working, compiled code for the other parts of
the program. Only test new added code.

9
OOP and Procedural Programming

Benefit:
• OOP are more secure in data.
• OOP can maintain code more easily.
• OOP can extend code more easily.
• OOP increases reuse of code by applying inheritance and polymorphism.

10
Encapsulation

11
Get, Set method and Encapsulation

• Imagine you wrote the code for a class and another dozen programmers
from your company all wrote programs that used your class.

P1 code P2 code

Public class BadOO{ Public class BetterOO{


public int size; private int size;
public int weight; private int weight;
… …
} public void setSize(newSize){
size = newSize;
}
}

12
Encapsulation

Other programmers that use P1 code Other programmers that use P2 code

public class ExploitBaddOO{ public class ExploitBetterOO{


public static void main(String[] public static void main(String[]
args){ args){
BadOO b = new BadOO(); BetterOO b = new
b.size = -5; BetterOO();
} b.setSize(-5);
} }
}

13
Encapsulation

• However, requirement changes:


• Require size > 0, if size <= 0 : not set size.

P1 code P2 code

Public class BadOO{ Public class BadOO{


public int size; //require: private int size; //require:
size>0 size>0
public int weight; private int weight;
… …
} public void setSize(newSize){
size = newSize;
}
}
And now you’re in trouble. Everyone need to You only need to change setSize() method.
change their code. OOP is not flexibility and OOP is flexibility and maintainability.
maintainability???

14
Encapsulation

• Keep instance variables hidden (with an access modifier, often private).


• Make public accessor methods, and force calling code to use those
methods rather than directly accessing the instance variable. These so-
called accessor methods allow users of your class to set a variable’s value
or get a variable’s value.
• For these accessor methods, use the most common naming convention
of set<Property> and get<Property>.

15
Inheritance

16
Inheritance

• Imagine you need to code an app to control an organization. You have


positions: Programmer and Manager. Both of these positions have a
common set of properties, including name, address, phone number. We
have 2 classes here:

Programmer class Manager class

Public class Programmer{ Public class Manager{


private String address; private String address;
private String name; private String name;
private String phone; private String phone;
} }

17
Inheritance

• These positions also have different properties. A programmer may be


concerned with project programming languages, whereas a manager
may be concerned with project status reports.

Programmer class Manager class

Public class Programmer{ Public class Manager{


private String address; private String address;
private String name; private String name;
private String phone; private String phone;
private String[] languages; void reportProjectStatus(){}
void writeCode(){} }
}

18
Problem with the design

19
Inheritance

• Pull out the common properties into a new position (a new class, called
parent class or superclass).

Programmer class Manager class Employee class

Public class Programmer Public class Manager Public class Employee{


extends Employee{ extends Employee{ private String address;
private String[] void private String name;
languages; reportProjectStatus(){} private String phone;}
void writeCode(){} }
}

20
Inheritance

Benefits

Smaller class definition, increase program readability

Ease of modification to common properties and behavior

Extensibility

Enable to reuse code

Eliminate duplicates

21
Inheritance

• In OOP, classes are organized hierarchically to prevent duplication and


reduce redundancy.
• The top-level class is called the superclass, while the lower-level classes
are called subclasses.
• In Java, the term “super-types” and “sub-types” refer to both classes
and interfaces (because it is possible to inherit concrete methods from
interfaces in Java 8). Sub-type can inherit more than one super-types
BUT only one super type can be a class, others must be interfaces.
• Types in Java exist within a hierarchical system, known as an inheritance
tree or type hierarchy.

22
Type hierarchy :
List

23
Type hierarchy :
Exceptions

24
IS-A and Has-A Relationships

• Is-A: The concept of IS-A is based on inheritance (or interface


implementation). IS-A is a way of saying, “This thing is a type of that
thing.”
• Has-A: HAS-A relationships are based on use, rather than inheritance.

Code Explanation

public class Animal {} A Horse Is-A Animal


A Horse Has-A Hat
public class Horse extends Animal {
private Hat myHat;
}

25
Polymorphism

26
Polymorphism

• The word "polymorphism" means "many forms".


• Polymorphism is the ability of an object to exhibit different
behaviors depending on the context.

• What is the “depending on the context” in Java?

27
Reference variable

• The only way to access an object is through a reference variable


• A reference variable can be of only one type, and once declared, that
type can never be changed (although the object it references can
change).
• A reference is a variable, so it can be reassigned to other objects (unless
the reference is declared final).
• A reference variable’s type determines the methods that can be invoked
on the object the variable is referencing.
• A reference variable can refer to any object of the same type as the
declared reference – (can refer to any subtype of the declared type)
• A reference variable can be declared as a class type or an interface type.
If the variable is declared as an interface type, it can reference any
object of any class that implements the interface

28
Substitution Principle

• An objects of a superclass should be replaceable with objects of a


subclass without affecting the correctness of the program.

Code Explanation

public class Animal { Animal ani = new Animal();


int x; // require x > 0 int c = 1/ani.functionA(9);
// requires: a < 10
public int functionA(int a){
return (10-a);
}
}
Animal ani = new Horse();
int c = 1/ani.functionA(9);
public class Horse extends Animal {
public int functionA(int a){
return (9-a);
}
}

29
Substitution Principle

The substitution principle requires that the subtype specification support


reasoning based on the supertype specification. Three properties must be
supported:
• Signature Rule
• Methods Rule
• Properties Rule

30
Signature Rule

The subtype objects must have all the methods of the supertype, and the
signatures of the subtype methods(Overriding) must be compatible with
the signatures of the corresponding supertype methods (Overridden
methods).
• Signature compatition:
• Return type: same type or one of the subtypes
• Exception: a subtype method can have fewer exceptions than the
corresponding supertype method

• The signature rule guarantees that every call that is type correct
according to the supertype’s definition is also type correct for the
subtype. This requirement is enforced by the Java compiler.

31
Methods rule

A subtype method can weaken the precondition and can strengthen the
postcondition. The precondition is what must be guaranteed to hold by
the caller in order to make the call. The postcondition is what is
guaranteed to hold right after the call (assuming that the precondition
held when the call was made).

• Precondition rule: Presuper >= presub


• Postcondition rule: Postsub >= Postsuper

Code Explanation

public class Animal { The precondition refers to the input


// requires: a <= 10 & a >=2 parameters of function. It depends on the
public int functionA(int a){ problem, the requirement.
// effects: output >=0 & output <=8
return (10-a); The postcondition refers to the output of
} function. It depends on the algorithm.
}

32
Properties rule

• The subtype must preserve all properties that can be proved about
supertype objects.
• Example: A supertype named Parent and an attribute B. ( B > 10)
A subtype of Parent named Child. (B > 5 & B < 20)
The following is the correct restriction on B in a subtype:

(Parent’s restriction on B) & (Child’s restriction on B):


(B>10) & (B > 5 & B < 20) --> (B > 10 & B < 20)

33
Java’s dynamic method dispatching

•A run-time mechanism to find the right version of an


overridden method to execute
•It is how Java implements runtime polymorphism
•The implementation from the actual type (runtime type) of
the variable will be chosen.

• actual type (runtime type) vs declared type (compile time type)?

34
Benefits of Polymorphism

• Increases code reusability by allowing objects of different classes to be


treated as objects of a common class.
• Improves readability and maintainability of code by reducing the amount
of code that needs to be written and maintained.
• Supports dynamic binding, enabling the correct method to be called at
runtime, based on the actual class of the object.
• Enables objects to be treated as a single type, making it easier to write
generic code that can handle objects of different types.

35
Summary

• OOP and Procedural Programming


• Encapsulation
• Inheritance
• Polymorphism

36

You might also like