0% found this document useful (0 votes)
40 views36 pages

05 Ch5 Introduction OOP 2022

This document provides an introduction to object-oriented programming concepts including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It discusses the history of OOP beginning with Simula in the 1960s and defines key terms like class, object, instantiation, and inheritance. Examples are given of class definitions in Java and how inheritance allows subclasses to extend parent classes. The principles of encapsulation, abstraction, and polymorphism are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views36 pages

05 Ch5 Introduction OOP 2022

This document provides an introduction to object-oriented programming concepts including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It discusses the history of OOP beginning with Simula in the 1960s and defines key terms like class, object, instantiation, and inheritance. Examples are given of class definitions in Java and how inheritance allows subclasses to extend parent classes. The principles of encapsulation, abstraction, and polymorphism are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

SOFTWARE

ENGINEERING
CO3001

CHAPTER 5 – INTRODUCTION TO OBJECT Anh Nguyen-Duc


ORIENTED PROGRAMMING Tho Quan Thanh

Adapted from https://iansommerville.com/software-engineering-book/slides/


Agenda

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

▪ There are different approaches to writing computer


programs.
▫ Procedural programming
▫ Object oriented programming
▪ They all involve decomposing your programs into parts.

“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

▪ Simula 1 (1962 - 1965) and Simula 67 (1967) Norwegian Computing


Center, Oslo, Norway by Ole-Johan Dahl and Kristen Nygaard.

Turing Award Winners - 2001


4 4
OOP … since 1962

▪ Smalltalk (1970s), Alan Kay's group


at Xerox PARC

▪ C++ (early 1980s), Bjarne


Stroustrup, Bell Labs

5 5
OOP Languages

▪ Modula – 3, Oberon, Eiffel, Java, C#, Python


▫ many languages have some Object Oriented version or
capability
▪ One of the dominant styles for implementing complex
programs with large numbers of interacting components
▫ … but not the only programming paradigm and there are
variations on object oriented programming

6
Definition – OOP, Class

▪ Object-oriented programming is a method of programming


based on a hierarchy of classes, and well-defined and
cooperating objects
▪ A class is a structure that defines the data and the methods
to work on that data. When you write programs in the Java
language, all program data is wrapped in a class, whether it
is a class you write or a class you use from the Java platform
API libraries

7 7
Definition – Class, Object

▪ Class: a collection of data (fields/ variables) and methods


that operate on that data
▫ define the contents/capabilities of the instances (objects)
of the class
▫ a class can be viewed as a factory for objects
▫ a class defines a recipe for its objects

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

▪ Object creation: memory is allocated for the object’s fields as


defined in the class
▪ Initialization is specified through a constructor
▪ A special method invoked when objects are created
▪ Different objects have the same attributes but the values of those
attributes can vary
▫ Reminder: The class definition specifies the attributes and
methods for all objects
▪ The current value of an object’s attribute’s determines it’s state.

10 10
11

Concept: Classes describe objects


12

Concept: Classes describe objects

Class Person {
private String hairColor;
….
}
13

Notation: How to declare and create objects

Employee secretary; // declares secretary


secretary = new Employee (); // allocates space
Employee secretary = new Employee(); // does both
But the secretary is still "blank" (null)
secretary.name = "Adele"; // dot notation
secretary.birthday (); // sends a message
14

Notation: How to reference a field or method

Inside a class, no dots are necessary


class Person { ... age = age + 1; ...}
Outside a class, you need to say which object you are
talking to
if (john.age < 75) john.birthday ();
If you don't have an object, you cannot use its fields or
methods!
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

▪ Classes are arranged in a treelike


structure called a hierarchy
▪ The class at the root is named
Object
▪ Every class, except Object, has a
superclass
▪ When you define a class, you
specify its superclass
▫ If you don’t specify a
superclass, Object is
assumed
16
7

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

class Person { class Employee


private String name; extends Person {
private int age; private double salary;
public void birthday () { public void pay () { ...}
age = age + 1; }
}
}
Every Employee has name and age fields and birthday
method as well as a salary field and a pay method.

19
Example: Assignment of subclasses

class Dog { ... }


class Poodle extends Dog { ... }
Dog myDog;
Dog rover = new Dog ();
Poodle yourPoodle;
Poodle fifi = new Poodle ();

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

▪ Also know as separation of concerns and information


hiding
▪ When creating new data types (classes) the details of the
actual data and the way operations work is hidden from
the other programmers who will use those new data types
▫ So they don't have to worry about them
▫ So they can be changed without any ill effects (loose coupling)
▪ Encapsulation makes it easier to be able to use
something
▫ microwave, radio, ipod, the Java String class
21
Encapsulation (A capsule)

22
23

Kinds of access in Java

▪ Java provides four levels of access:


▫ public: available everywhere
▫ protected: available within the package (in the same
subdirectory) and to all subclasses
▫ [default]: available within the package
▫ private: only available within the class itself
▪ The default is called package visibility
▪ In small programs this isn't important...right?
Encapsulation

24
Abstraction

▪ OOP is about abstraction


▪ Abstraction is a method of hiding the implementation detail
and only show the functionalities
▪ Encapsulation and Inheritance are examples of abstraction
Polymorphism

▪ Polymorphism means many (poly) shapes (morph)


▪ In Java, polymorphism refers to the fact that you can have multiple
methods with the same name in the same class
▪ There are two kinds of polymorphism:
▫ Overloading
▫ Two or more methods with different signatures
▫ Overriding
▫ Replacing an inherited method with another having the
same signature
Polymorphism

▪ two methods have to differ in their names or in the number or


types of their parameters
▫ foo(int i) and foo(int i, int j) are different
▫ foo(int i) and foo(int k) are the same
▫ foo(int i, double d) and foo(double d, int i) are different
28

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?

▪ You should overload a method when you want to do


essentially the same thing, but with different parameters
▪ You should override an inherited method if you want to do
something slightly different than in the superclass
▫ It’s almost always a good idea to override public void
toString() -- it’s handy for debugging, and for many other
reasons
▫ To test your own objects for equality, override public void
equals(Object o)
▫ There are special methods (in java.util.Arrays) that you
can use for testing array equality

31
Reuse

▪ Inheritance encourages software reuse


▪ Existing code need not be rewritten
▪ Successful reuse occurs only through careful
planning and design
▫ when defining classes, anticipate future
modifications and extensions
Building Complex Systems

▪ From Software Engineering:


complex systems are difficult to manage
▪ Proper use of OOP aids in managing this complexity
▪ The analysis and design of OO systems require
corresponding modeling techniques
Object-Oriented Modeling

▪ UML: Unified Modeling Language


▫ OO Modeling Standard
▫ Booch, Jacobson, Rumbaugh
▪ What is depicted?
▫ Class details and static relationships
▫ System functionality
▫ Object interaction
▫ State transition within an object
Some UML Modeling Techniques

▪ 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

You might also like