05 Ch5 Introduction OOP 2022
05 Ch5 Introduction OOP 2022
ENGINEERING
CO3001
o History
o Key OOP Concepts
o Object, Class
o Instantiation, Constructors
o Encapsulation
o Inheritance and Subclasses
o Abstraction
o Reuse
o Polymorphism, Dynamic Binding
o Object-Oriented Design and Modeling
2 2
Agenda
“And so, from Europe, we get things such ... object-oriented analysis and
design (a clever way of breaking up software programming instructions
and data into small, reusable objects, based on certain abstraction
principles and design hierarchies.)”
-Michael A. Cusumano, The Business Of Software
3 3
OOP … since 1962
5 5
OOP Languages
6
Definition – OOP, Class
7 7
Definition – Class, Object
8 8
Example of a class (Java)
class Customer {
// Fields/ variables/ Data
private String name; //Can get but not change
Customer a = new Customer(«Anh», 500);
private double salary; // Cannot get or set
a.pay();
// Constructor
Customer b = new Customer(«Tho», 600);
Customer(String n, double s) { String anh_name = a.getName();
name = n; order = s; b.pay();
}
// Methods
void pay () {
System.out.println("Pay to the order of " +
name + " $" + order);
}
public String getName() { return name; } // getter
}
9
Definition – Class, Object
10 10
11
Class Person {
private String hairColor;
….
}
13
▪ Inheritance:
▫ programming language feature that allows for the implicit
definition of variables/methods for a class through an
existing class
▪ An object also inherits:
▫ the fields described in the class's superclasses
▫ the methods described in the class's superclasses
▪ A class is not a complete description of its objects!
Concept: Classes form a hierarchy
▪ Subclass relationship
▫ B is a subclass of A
▫ B inherits all definitions
(variables/methods) in A
▪ A class may have several
ancestors, up to Object
▪ Every class may have one or
more subclasses
Example of (part of) a hierarchy
18
Example of inheritance
19
Example: Assignment of subclasses
myDog = rover; // ok
yourPoodle = fifi; // ok
myDog = fifi; //ok
yourPoodle = rover; // illegal
yourPoodle = (Poodle) rover; //runtime check
20
CS 307 Fundamentals
Implementing Classes of Computer Science
Encapsulation
22
23
24
Abstraction
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
29
Overriding
class Animal {
public static void main(String args[]) {
Animal animal = new Animal();
▪ This is called overriding a
Dog dog = new Dog(); method
animal.print();
dog.print(); ▪ Method print in Dog
}
void print() {
overrides method print in
System.out.println("Superclass Animal"); Animal
}
} ▪ A subclass variable can
public class Dog extends Animal { shadow a superclass variable,
void print() {
System.out.println("Subclass Dog");
but a subclass method can
} override a superclass method
}
Superclass Animal
Subclass Dog
Another examples
30
When to do?
31
Reuse
▪ Class Diagrams
▪ Use Cases/Use Case Diagrams
▪ Interaction Diagrams
▪ State Diagrams
Object-Oriented Design Models
▪ Static Model
▫ Class Diagrams
▪ Dynamic Model
▫ Use Cases, Interaction Diagrams, State Diagrams,
others