Vous êtes sur la page 1sur 50

Object-oriented Software Engineering

Object-oriented Concepts
TOPIC ONE
Software Engineering 1

Object-oriented Software Engineering

Object-oriented Software Engineering is the use of object technologies in building software.

Software Engineering

Object Technology

Pressman, 1997

Object technologies is often used to encompass all aspects of an object-oriented view and includes analysis, design, and testing methods; programming languages; tools; databases; and applications that are created using object-oriented approach. Object technology is a set of principles guiding software construction together with languages, databases, and other tools that support those principles.

Taylor, 1997, Object Technology

Software Engineering

Benefits of Object Technology

It leads to reuse, and reuse (of program components) leads to faster software development and higher-quality programs. It leads to higher maintainability of software modules because its structure is inherently decoupled. It leads to object-oriented system that are easier to adapt and easier to scale, ie, large systems are created by assembling reusable subsystems.

Software Engineering

Object

It is a representation of an entity either physical, conceptual, or software. It allows software developers to represent real-world concepts in their software design.

Airplane

Chemical Process

Linked List
Software Engineering 5

Object

It is an entity with a welldefined boundary and identity that encapsulates state and behavior.

Operations

Attributes
Software Engineering 6

Object's State

It is one of the possible conditions that an object may exists in. It is implemented by a set of properties called attributes, along with its values and the links it may have on other objects.
AthleteID: 3556 Name: Joel Santos Status:NEW Squad:None

Software Engineering

Object's Behavior

It determines how an object acts and reacts. It is represented by the operations that the object can perform.
Enrolls()

updateSquad()

Joel Santos
Software Engineering 8

Object's Identity

Although two objects may share the same state (attributes and relationships), they are separate, independent objects with their own unique identity.

AthleteID: 3556 Name Joel Santos Status:NEW Squad:None

AthleteID: 3557 Name: Arjay Solamo Status:NEW Squad:None

Software Engineering

Four Basic Principles of Object-orientation


Abstraction Encapsulation Modularity Hierarchy

Software Engineering

10

Abstraction

Abstraction is a kind of representation that includes only the things that are important or interesting from a particular point of view. It is the process of emphasizing the commonalities while removing distinctions. It allows us to manage complexity systems by concentrating on the essential characteristics that distinguish it from all other kinds of systems. It is domain and perspective dependent.

Software Engineering

11

Sample Abstraction

An applicant submits a club membership application to the club staff. A club staff schedules an applicant for the mock try-outs. A coach assigns an athlete to a squad. A squad can be a training or competing squad. Teams are formed from a squad.

Software Engineering

12

Encapsulation

Encapsulation localizes features of an entity into a single blackbox abstraction, and hides the implementation of these features behind a single interface. It is also known as information-hiding; it allows users to use the object without knowing how the implementation fulfils the interface. It offers two kinds of protection: it protects the object's state from being corrupted and client code from changes in the object's implementation.

Software Engineering

13

Encapsulation Illustrated

Joel Santos is assigned to the Training Squad. The key is in the message interface.
submitApplication()

updateSquad(Training)

UpdateSquad()

Software Engineering

14

Modularity

Modularity is the physical and logical decomposition of large and complex things into smaller and manageable components that achieve the software engineering goals. It is about breaking up a large chunk of a system into small and manageable subsystems. The subsystems can be independently developed as long as their interactions are well understood.

Software Engineering

15

Modularity Illustrated
Ang Bulilit Liga Squad and Team System
Club Membership Maintenance System

Coach Information Maintenance System

Squad and Team Maintenance System

Software Engineering

16

Hierarchy

Any ranking or ordering of abstractions into a tree-like structure. Kinds of Hierarchy


Aggregation Class Containment Inheritance Partition Specialization Type

Software Engineering

17

Hierarchy Illustrated
Squad

Training Squad

Competing Squad

Software Engineering

18

Generalization

It is a form of association wherein one class shares the structure and/or behavior of one or more classes. It defines a hierarchy of abstractions in which a subclass inherits from one or more superclasses.

Single Inheritance Multiple Inheritance

It is an is a kind of relationship.

Software Engineering

19

Inheritance
Squad

It is a mechanism by which more-specific elements incorporate the structure and behavior of moregeneral elements. A class inherits attributes, operations and relationship.
Training Squad

Name Coach MemberList listMembers() changeCoach()

Competing Squad

Software Engineering

20

Inheritance

In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass. A sample class hierarchy is shown below.

Superclass

Any class above a specific class in the class hierarchy. Any class below a specific class in the class hierarchy.
Software Engineering 21

Subclass

The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

Inheritance

Superclass

Any class above a specific class in the class hierarchy. Any class below a specific class in the class hierarchy.

Subclass

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

22

Inheritance

Benefits of Inheritance in OOP : Reusability

Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses. Thus, you can encode a method only once and they can be used by all subclasses. A subclass only needs to implement the differences between itself and the parent.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

23

Inheritance

To derive a class, we use the extends keyword. In order to illustrate this, let's create a sample parent class. Suppose we have a parent class called Person. } . } b u p * / i o c r t s l D d m a n y S ; " s e i r = ( t o c . p n r e s n ) e u . l I s n C r )

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

24

Inheritance

Now, we want to create another class named Student. Since a student is also a person, we decide to just extend the class Person, so that we can inherit all the properties and methods of the existing class Person. To do this, we write,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

25

Inheritance

When a Student object is instantiated, the default constructor of its superclass is invoked implicitly to do the necessary initializations. After that, the statements inside the subclass's constructor are executed.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

26

Inheritance:

To illustrate this, consider the following code,

In the code, we create an object of class Student. The output of the program is,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

27

Inheritance

The program flow is shown below.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

28

The super keyword

A subclass can also explicitly call a constructor of its immediate superclass. This is done by using the super constructor call. A super constructor call in the constructor of a subclass will result in the execution of relevant constructor from the superclass, based on the arguments passed.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

29

The super keyword

For example, given our previous example classes Person and Student, we show an example of a super constructor call. Given the following code for Student,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

30

The super keyword

Few things to remember when using the super constructor call:

The super() call MUST OCCUR AS THE FIRST STATEMENT IN A CONSTRUCTOR. The super() call can only be used in a constructor definition. This implies that the this() construct and the super() calls CANNOT BOTH OCCUR IN THE SAME CONSTRUCTOR.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

31

The super keyword

Another use of super is to refer to members of the superclass (just like the this reference ). For example,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

32

Overriding methods

If for some reason a derived class needs to have a different implementation of a certain method from that of the superclass, overriding methods could prove to be very useful. A subclass can override a method defined in its superclass by providing a new implementation for that method.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

33

Example

Suppose we have the following implementation for the getName method in the Person superclass,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

34

Example

To override, the getName method of the superclass Person, we write in the subclass Student,

Now, when we invoke the getName method of an object of the subclass Student, the overridden getName method would be called, and the output would be,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

35

Final Classes

Final Classes

Classes that cannot be extended To declare final classes, we write, fin pub. . .e{ assN al lic am }Cl

Example:

Other examples of final classes are your wrapper classes and Strings.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

36

Final Methods and Classes

Final Methods

Methods that cannot be overridden To declare final methods, we write, fin pub. . .yp] [mt ho etur al lic nT e e dName( [p rm e ] a aet r ] { }[r

Static methods are automatically final.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

37

Example

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

38

Polymorphism

It is the ability to hide many different implementation behind a single interface. It allows the same message to be handled differently by different objects.
Stock getValue();

<<interface>> Asset getValue();

Bond getValue();

Mutual Fund getValue();

Software Engineering

39

Polymorphism

Polymorphism

The ability of a reference variable to change behavior according to what object it is holding. This allows multiple objects of different subclasses to be treated as objects of a single superclass, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.

To illustrate polymorphism, let us discuss an example.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

40

Polymorphism

Given the parent class Person and the subclass Student of the previous examples, we add another subclass of Person which is Employee. Below is the class hierarchy for that,

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

41

Polymorphism

In Java, we can create a reference that is of type superclass to an object of its subclass. For example,
b O n d u t s = f e r c / o a i p e r j b o n e d u t S /

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

42

Polymorphism

E } b u p i s a l m { e y c p o

Now suppose we have a getName method in our superclass Person, and we override this method in both the subclasses Student and Employee.
} b u p i g n r t ) ( e m a l c S e N t e r y S m u s n i ( o l a N + . ) p e a u . l E p m

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

43

Polymorphism

Going back to our main method, when we try to call the getName method of the reference Person ref, the getName method of the Student object will be called. Now, if we assign ref to an Employee object, the getName method of Employee will be called.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

44

Polymorphism

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

45

Polymorphism

Another example that illustrates polymorphism is when we try to pass references to methods. Suppose we have a static method printInformation that takes in a Person reference as parameter.

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

46

Polymorphism

We can actually pass a reference of type Employee and type Student to the printInformation method as long as it is a subclass of the class Person.
r d O ) p i t a m ( n l y e c ; t e n o j

Software Engineering
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.

47

Interface

It formalizes polymorphism. It defines polymorphism in a declarative way, unrelated to implementation. It is the key to the plug-n-play ability of an architecture.

Software Engineering

48

Aggregation

It is a special form of association that models a whole-part relationship between an aggregate (whole) and its parts.

Team

Athletes

Software Engineering

49

Summary

Object Technologies Objects


Object's State Object's Behavior Object's Identity Abstraction Encapsulation Modularity Hierarchy

Four Basic Principles of Object-orientation


Software Engineering

50