05_Ch5_Introduction_OOP
05_Ch5_Introduction_OOP
ENGINEERING
CO3001
CHAPTER 5 – INTRODUCTION
TO OBJECT ORIENTED Anh Nguyen-Duc
Tho Quan Thanh
PROGRAMMING
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
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
Concept: Classes describe objects
11
Concept: Classes describe objects
Class Person {
private String hairColor;
….
}
12
Notation: How to declare and create objects
13
Notation: How to reference a field or method
14
Inheritance
▪ 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
Encapsulation
22
Kinds of access in Java
23
Encapsulation
24
Abstraction
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
28
Overriding
class Animal {
public static void main(String args[]) { ▪ This is called overriding
Animal animal = new Animal();
Dog dog = new Dog(); a method
animal.print();
dog.print(); ▪ Method print in Dog
} overrides method print
void print() {
System.out.println("Superclass Animal"); in Animal
}
} ▪ A subclass variable can
public class Dog extends Animal { shadow a superclass
void print() { variable, but a subclass
System.out.println("Subclass Dog");
} method can override a
}
superclass method
Superclass Animal
Subclass Dog 29
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