Jedi slides 2.1 object-oriented concepts

50
Software Engineering 1 Object-oriented Concepts TOPIC ONE Object-oriented Software Engineering

description

OOP

Transcript of Jedi slides 2.1 object-oriented concepts

Page 1: Jedi slides 2.1 object-oriented concepts

Software Engineering 1

Object-orientedConcepts

TOPIC ONE

Object-oriented Software Engineering

Page 2: Jedi slides 2.1 object-oriented concepts

Software Engineering 2

Object-oriented Software Engineering

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

Page 3: Jedi slides 2.1 object-oriented concepts

Software Engineering 3

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.

Taylor, 1997, Object Technology Object technology is a set of principles guiding software construction

together with languages, databases, and other tools that support those principles.

Object Technology

Page 4: Jedi slides 2.1 object-oriented concepts

Software Engineering 4

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.

Benefits of Object Technology

Page 5: Jedi slides 2.1 object-oriented concepts

Software Engineering 5

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

ChemicalProcess

Linked List

Object

Page 6: Jedi slides 2.1 object-oriented concepts

Software Engineering 6

It is an entity with a well-defined boundary and identity that encapsulates state and behavior.

Operations

Attributes

Object

Page 7: Jedi slides 2.1 object-oriented concepts

Software Engineering 7

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: 3556Name: J oel SantosStatus:NEWSquad:None

Object's State

Page 8: Jedi slides 2.1 object-oriented concepts

Software Engineering 8

It determines how an object acts and reacts.

It is represented by the operations that the object can perform.

J oel Santos

Enrolls()

updateSquad()

Object's Behavior

Page 9: Jedi slides 2.1 object-oriented concepts

Software Engineering 9

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

AthleteID: 3556Name J oel SantosStatus:NEWSquad:None

AthleteID: 3557Name: Arjay SolamoStatus:NEWSquad:None

Object's Identity

Page 10: Jedi slides 2.1 object-oriented concepts

Software Engineering 10

Abstraction Encapsulation Modularity Hierarchy

Four Basic Principles of Object-orientation

Page 11: Jedi slides 2.1 object-oriented concepts

Software Engineering 11

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.

Abstraction

Page 12: Jedi slides 2.1 object-oriented concepts

Software Engineering 12

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.

Page 13: Jedi slides 2.1 object-oriented concepts

Software Engineering 13

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.

Encapsulation

Page 14: Jedi slides 2.1 object-oriented concepts

Software Engineering 14

Encapsulation Illustrated Joel Santos is assigned to

the Training Squad. The key is in the message

interface.submitApplication()

UpdateSquad()updateSquad(“Training”)

Page 15: Jedi slides 2.1 object-oriented concepts

Software Engineering 15

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.

Modularity

Page 16: Jedi slides 2.1 object-oriented concepts

Software Engineering 16

Modularity Illustrated

Ang Bulilit LigaSquad and Team System

Club MembershipMaintenance

System

Coach InformationMaintenance

System

Squad and TeamMaintenance

System

Page 17: Jedi slides 2.1 object-oriented concepts

Software Engineering 17

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

Kinds of Hierarchy Aggregation

Class

Containment

Inheritance

Partition

Specialization

Type

Hierarchy

Page 18: Jedi slides 2.1 object-oriented concepts

Software Engineering 18

Hierarchy Illustrated

Squad

TrainingSquad

CompetingSquad

Page 19: Jedi slides 2.1 object-oriented concepts

Software Engineering 19

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.

Generalization

Page 20: Jedi slides 2.1 object-oriented concepts

Software Engineering 20

It is a mechanism by which more-specific elements incorporate the structure and behavior of more-general elements.

A class inherits attributes, operations and relationship.

Inheritance

Squad

NameCoachMemberList

listMembers()changeCoach()

Training Squad Competing Squad

Page 21: Jedi slides 2.1 object-oriented concepts

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.

Page 22: Jedi slides 2.1 object-oriented concepts

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.

Page 23: Jedi slides 2.1 object-oriented concepts

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 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.

Page 24: Jedi slides 2.1 object-oriented concepts

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.

public class Person { protected String name; protected String address;

/** * Default constructor */ public Person(){

System.out.println(“Inside Person:Constructor”);

name = ""; address = ""; }. . . .

}

Page 25: Jedi slides 2.1 object-oriented concepts

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 extend

the class Person, so that we can inherit all the properties and methods of the existing class Person.

To do this, we write,

public class Student extends Person { public Student(){

System.out.println(“Inside Student:Constructor”);

}. . . .

}

Page 26: Jedi slides 2.1 object-oriented concepts

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 default constructor of its superclass is invoked implicitly to do the necessary initializations.

After that, the statements inside the subclass's constructor are executed.

Page 27: Jedi slides 2.1 object-oriented concepts

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. The output of the program is,

public static void main( String[] args ){ Student anna = new Student();

}

Inside Person:Constructor Inside Student:Constructor

Page 28: Jedi slides 2.1 object-oriented concepts

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.

Page 29: Jedi slides 2.1 object-oriented concepts

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 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.

Page 30: Jedi slides 2.1 object-oriented concepts

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 Person

and Student, we show an example of a super constructor call.

Given the following code for Student, public Student(){

super( "SomeName", "SomeAddress" ); System.out.println("Inside Student:Constructor");}

Page 31: Jedi slides 2.1 object-oriented concepts

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 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.

Page 32: Jedi slides 2.1 object-oriented concepts

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 the

superclass (just like the this reference ). For example,

public Student() { super.name = “somename”; super.address = “some address”;

}

Page 33: Jedi slides 2.1 object-oriented concepts

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 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.

Page 34: Jedi slides 2.1 object-oriented concepts

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 the

getName method in the Person superclass,

public class Person { : : public String getName(){

System.out.println("Parent: getName"); return name;

} }

Page 35: Jedi slides 2.1 object-oriented concepts

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 of the subclass Student, the overridden getName method would be called, and the output would be,

public class Student extends Person{: : public String getName(){

System.out.println("Student: getName");

return name; } :

}

Student: getName

Page 36: Jedi slides 2.1 object-oriented concepts

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,public final ClassName{

. . . }

Example:

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

public final class Person { . . .

}

Page 37: Jedi slides 2.1 object-oriented concepts

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,public final [returnType] [methodName]([parameters]){

. . .}

Static methods are automatically final.

Page 38: Jedi slides 2.1 object-oriented concepts

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

public final String getName(){ return name; }

Page 39: Jedi slides 2.1 object-oriented concepts

Software Engineering 39

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.

Polymorphism

<<interface>>Asset

getValue();

Stock

getValue();

Bond

getValue();

Mutual Fund

getValue();

Page 40: Jedi slides 2.1 object-oriented concepts

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 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.

Page 41: Jedi slides 2.1 object-oriented concepts

Software Engineering 41The 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 of

the previous examples, we add another subclass of Person which is Employee.

Below is the class hierarchy for that,

Page 42: Jedi slides 2.1 object-oriented concepts

Software Engineering 42The 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 superclass

to an object of its subclass. For example,

public static main( String[] args ) {

Person ref; Student studentObject = new Student(); Employee employeeObject = new Employee();

ref = studentObject; //Person reference points to a // Student object }

Page 43: Jedi slides 2.1 object-oriented concepts

Software Engineering 43The 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 superclass

Person, and we override this method in both the subclasses Student and Employee.

public class Student { public String getName(){

System.out.println(“Student Name:” + name); return name;

}}

public class Employee { public String getName(){

System.out.println(“Employee Name:” + name); return name;

} }

Page 44: Jedi slides 2.1 object-oriented concepts

Software Engineering 44The 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 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.

Page 45: Jedi slides 2.1 object-oriented concepts

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

Polymorphism1 public static main( String[] args ) {2 Person ref; 3 Student studentObject = new Student(); 4 Employee employeeObject = new Employee();

5 ref = studentObject; //Person ref. points to a 6 // Student

object

7 //getName of Student class is called 8 String temp=ref.getName();9 System.out.println( temp );

10 ref = employeeObject; //Person ref. points to an 11 // Employee

object 1213 //getName of Employee class is called 14 String temp = ref.getName();15 System.out.println( temp ); 16 }

Page 46: Jedi slides 2.1 object-oriented concepts

Software Engineering 46The 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 we

try to pass references to methods.

Suppose we have a static method printInformation that takes in a Person reference as parameter.

public static printInformation( Person p ){. . . .

}

Page 47: Jedi slides 2.1 object-oriented concepts

Software Engineering 47The 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 and

type Student to the printInformation method as long as it is a subclass of the class Person.

public static main( String[] args ){

Student studentObject = new Student();Employee employeeObject = new Employee();

printInformation( studentObject );

printInformation( employeeObject );}

Page 48: Jedi slides 2.1 object-oriented concepts

Software Engineering 48

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.

Interface

Page 49: Jedi slides 2.1 object-oriented concepts

Software Engineering 49

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

Aggregation

Team Athletes

Page 50: Jedi slides 2.1 object-oriented concepts

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