Upload
meljun-cortes-mbampa
View
220
Download
0
Embed Size (px)
Citation preview
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
1/50
Software Engineering 1
Object-orientedConcepts
TOPIC ONE
Object-oriented Software
Engineering
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
2/50
Software Engineering 2
Object-oriented Software
Engineering Object-oriented Software Engineering is the use of object
technologies in building software.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
3/50
Software Engineering 3
Pressman, 1997
Object technologies is often used to encompass all aspects of anobject-oriented view and includes analysis, design, and testingmethods; programming languages; tools; databases; and
applications that are created using object-oriented approach.
Taylor, 1997, Object Technology
Object technology is a set of principles guiding software constructiontogether with languages, databases, and other tools that supportthose principles.
Object Technology
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
4/50
Software Engineering 4
It leads to reuse, and reuse (of program components) leadsto 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 byassembling reusable subsystems.
Benefits of Object
Technology
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
5/50
Software Engineering 5
It is a representation of anentity either physical,conceptual, or software.
It allows softwaredevelopers to representreal-world concepts in theirsoftware design.
Airplane
ChemicalProcess
Linked List
Object
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
6/50
Software Engineering 6
It is an entity with a well-defined boundary andidentity that encapsulatesstate and behavior.
Operations
Attributes
Object
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
7/50
Software Engineering 7
It is one of the possibleconditions that an objectmay exists in.
It is implemented by a setof properties calledattributes, along with itsvalues and the links it mayhave on other objects.
AthleteID: 3556
Name: Joel Santos
Status:NEW
Squad:None
Object's State
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
8/50
Software Engineering 8
It determines how an objectacts and reacts.
It is represented by theoperations that the objectcan perform.
Joel Santos
Enrolls()
updateSquad()
Object's Behavior
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
9/50
Software Engineering 9
Although two objects may share the same state (attributesand relationships), they are separate, independent objectswith their own unique identity.
AthleteID: 3556
Name Joel SantosStatus:NEW
Squad:None
AthleteID: 3557
Name: Arjay SolamoStatus:NEW
Squad:None
Object's Identity
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
10/50
Software Engineering 10
Abstraction
Encapsulation
Modularity
Hierarchy
Four Basic Principles of
Object-orientation
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
11/50
Software Engineering 11
Abstraction is a kind of representation that includes only thethings that are important or interesting from a particularpoint of view.
It is the process of emphasizing the commonalities whileremoving distinctions.
It allows us to manage complexity systems by concentratingon the essential characteristics that distinguish it from allother kinds of systems.
It is domain and perspective dependent.
Abstraction
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
12/50
Software Engineering 12
Sample Abstraction
An applicant submits a club membership application to theclub 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.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
13/50
Software Engineering 13
Encapsulation localizes features of an entity into a singleblackbox abstraction, and hides the implementation of thesefeatures behind a single interface.
It is also known as information-hiding; it allows users touse the object without knowing how the implementationfulfils the interface.
It offers two kinds of protection: it protects the object's statefrom being corrupted and client code from changes in the
object's implementation.
Encapsulation
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
14/50
Software Engineering 14
Encapsulation Illustrated
Joel Santos is assigned tothe Training Squad.
The key is in the message
interface.
submitApplication()
UpdateSquad()
updateSquad(Training)
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
15/50
Software Engineering 15
Modularity is the physical and logical decomposition of largeand complex things into smaller and manageablecomponents that achieve the software engineering goals.
It is about breaking up a large chunk of a system into smalland manageable subsystems. The subsystems can beindependently developed as long as their interactions arewell understood.
Modularity
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
16/50
Software Engineering 16
Modularity Illustrated
Ang Bulilit LigaSquad and Team System
Club MembershipMaintenance
System
Coach InformationMaintenance
System
Squad and TeamMaintenanceSystem
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
17/50
Software Engineering 17
Any ranking or ordering of abstractions into a tree-likestructure.
Kinds of Hierarchy
Aggregation Class
Containment
Inheritance
Partition
Specialization
Type
Hierarchy
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
18/50
Software Engineering 18
Hierarchy Illustrated
Squad
TrainingSquad
CompetingSquad
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
19/50
Software Engineering 19
It is a form of association wherein one class shares thestructure and/or behavior of one or more classes.
It defines a hierarchy of abstractions in which a subclassinherits from one or more superclasses.
Single Inheritance
Multiple Inheritance
It is an is a kind ofrelationship.
Generalization
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
20/50
Software Engineering 20
It is a mechanism by whichmore-specific elementsincorporate the structureand behavior of more-
general elements. A class inherits attributes,
operations and relationship.
InheritanceSquad
NameCoachMemberList
listMembers()changeCoach()
Training Squad CompetingSquad
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
21/50
Software Engineering 21The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
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.
Subclass
Any class below a specific class in the class hierarchy.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
22/50
Software Engineering 22The 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.
Subclass
Any class below a specific class in the class hierarchy.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
23/50
Software Engineering 23The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance
Benefits of Inheritance in OOP : Reusability
Once a behavior (method) is defined in a superclass, that behavioris 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 itselfand the parent.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
24/50
Software Engineering 24The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
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./***D nstructor*/publi esn)Sys em. u.prin ln( Iside ers nConst uct r);nam"";adress="";}....}
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
25/50
Software Engineering 25The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance
Now, we want to create another class named Student.
Since a student is also a person, we decide to just extendthe class Person, so that we can inherit all the propertiesand methods of the existing class Person.
To do this, we write,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
26/50
Software Engineering 26The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance
When a Student object is instantiated, the defaultconstructor of its superclass is invoked implicitly to do thenecessary initializations.
After that, the statements inside the subclass's constructor
are executed.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
27/50
Software Engineering 27The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance:
To illustrate this, consider the following code,
In the code, we create an object of class Student. Theoutput of the program is,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
28/50
Software Engineering 28The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance
The program flow is shown below.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
29/50
Software Engineering 29The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
The super keyword
A subclass can also explicitly call a constructor of itsimmediate superclass.
This is done by using the superconstructor call.
A superconstructor call in the constructor of a subclass willresult in the execution of relevant constructor from thesuperclass, based on the arguments passed.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
30/50
Software Engineering 30The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
The super keyword
For example, given our previous example classes Personand Student, we show an example of a super constructorcall.
Given the following code for Student,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
31/50
Software Engineering 31The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
The super keyword
Few things to remember when using the super constructorcall:
The super() call MUST OCCUR AS THE FIRST STATEMENT IN ACONSTRUCTOR.
The super() call can only be used in a constructor definition.
This implies that the this() construct and the super() calls CANNOTBOTH OCCUR IN THE SAME CONSTRUCTOR.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
32/50
Software Engineering 32The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
The super keyword
Another use of super is to refer to members of thesuperclass (just like the this reference ).
For example,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
33/50
Software Engineering 33The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Overriding methods
If for some reason a derived class needs to have a differentimplementation of a certain method from that of thesuperclass, overriding methods could prove to be veryuseful.
A subclass can override a method defined in its superclassby providing a new implementation for that method.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
34/50
Software Engineering 34The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Example
Suppose we have the following implementation for thegetName method in the Person superclass,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
35/50
Software Engineering 35The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
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 ofthe subclass Student, the overridden getName methodwould be called, and the output would be,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
36/50
Software Engineering 36The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Final Classes Final Classes
Classes that cannot be extended
To declare final classes, we write,publicfinalClassName{. . .}
Example:
Other examples of final classes are your wrapper classes
and Strings.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
37/50
Software Engineering 37The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Final Methods and Classes
Final Methods
Methods that cannot be overridden
To declare final methods, we write,publicfinal[returnType] [met hodName]([parameter]{. . .}
Static methods are automatically final.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
38/50
Software Engineering 38The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Example
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
39/50
Software Engineering39
It is the ability to hide manydifferent implementationbehind a single interface.
It allows the same
message to be handleddifferently by differentobjects.
Polymorphism
Asset
getValue();
Stock
getValue();
Bond
getValue();
Mutual Fund
getValue();
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
40/50
Software Engineering 40The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
Polymorphism
The ability of a reference variable to change behavior according towhat object it is holding.
This allows multiple objects of different subclasses to be treated as
objects of a single superclass, while automatically selecting theproper methods to apply to a particular object based on the subclassit belongs to.
To illustrate polymorphism, let us discuss an example.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
41/50
Software Engineering41
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
Given the parent class Person and the subclass Student ofthe previous examples, we add another subclass of Personwhich is Employee.
Below is the class hierarchy for that,
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
42/50
Software Engineering 42
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
In Java, we can create a reference that is of type superclassto an object of its subclass. For example,
ref=studentOb ect// erson referencepointstoa //Studentobje}
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
43/50
Software Engineering 43
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
Now suppose we have a getName method in our superclassPerson, and we override this method in both the subclassesStudent and Employee.
publicclassEmployee{ publicStringgetName() Sys em. u.prin ln( EployeNa e+n me)retur ae}}
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
44/50
Software Engineering 44
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
Going back to our main method, when we try to call thegetName method of the reference Person ref, the getNamemethod of the Student object will be called.
Now, if we assign ref to an Employee object, the getNamemethod of Employee will be called.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
45/50
Software Engineering 45
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
46/50
Software Engineering 46
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
Another example that illustrates polymorphism is when wetry to pass references to methods.
Suppose we have a static method printInformation thattakes in a Person reference as parameter.
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
47/50
Software Engineering 47
The use of the slide has prior approval from the original author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Polymorphism
We can actually pass a reference of type Employee andtype Student to the printInformation method as long as it is asubclass of the class Person.
pri tnormatin(tdnO ect)pri tnormatin(mlye jec;}
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
48/50
Software Engineering 48
It formalizes polymorphism. It defines polymorphism in adeclarative way, unrelated to implementation.
It is the key to theplug-n-playability of an architecture.
Interface
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
49/50
Software Engineering 49
It is a special form of association that models a whole-partrelationship between an aggregate (whole) and its parts.
Aggregation
Team Athletes
7/29/2019 MELJUN CORTES JEDI Slides-2.1 Object-Oriented Concepts
50/50
Software Engineering 50
Summary
Object Technologies
Objects
Object's State
Object's Behavior
Object's Identity
Four Basic Principles of Object-orientation
Abstraction
Encapsulation
Modularity
Hierarchy