object oriented programming

14
© Tyler Technologies 2013 1 Agenda Encapsulation Inheritance Polymorphism Association Aggregation Composition

Upload: haris-bin-zahid

Post on 06-Aug-2015

25 views

Category:

Software


0 download

TRANSCRIPT

© Tyler Technologies 20131

Agenda

• Encapsulation

• Inheritance

• Polymorphism

• Association

• Aggregation

• Composition

© Tyler Technologies 20132

What is Encapsulation?

• Encapsulation is the condition of being enclosed. (as in a capsule)

• With programming perspective, Enclosing “attributes” and “methods” within a class would be called Encapsulation.

© Tyler Technologies 20133

Encapsulation Example

• Attributes are set to private.

• Methods defined are usually set to public.

• Methods may also be set to private, these methods would only be called from with in the class.

© Tyler Technologies 20134

Static Methods• Static Methods are class level methods that are called without creating

an instance of a class.

• The Static keyword instructs the system to create only one instance of the method regardless of how many instances of its class you create.

• Declaring a Static Method:

• Calling a Static Method:

• Static methods are generally intended for cases where the following criteria are met:

– The method has no reason to access the attributes that are declared in the Class Declaration block of the class.

– The method has no reason to call any instance (non-static) methods of the class.

© Tyler Technologies 20135

Encapsulation - Benefits

• Encapsulation hides the implementation details.

• Ensures that structural change remains local:– Changing the class internal code doesn’t effect any code on the

outside.– Changing the implementation inside a method does not effect the

class which is calling it.

• Encapsulation allows adding some logic while accessing any attribute.– E.g. in Accessor methods

• Hiding implementation details reduces complexity implying easier maintenance.

© Tyler Technologies 20136

Inheritance

In Object Oriented Programming Inheritance is a way to:

• Reuse code of Existing Objects.

• Establish a subtype from an existing type.

Use inheritance for buidling is-a relationships– E.g. cat is-an animal (cats are kind of animals)

Don't use it to build has-a relationship– E.g. cat has-a name (cat is not kind of name)

© Tyler Technologies 20137

How Inheritance works?

Vehicle

Automobile

Car Motor Cycle

Aircraft

F-16

© Tyler Technologies 20138

What is being “Reused”?

© Tyler Technologies 20139

Inheritance - Benefits

• Inheritance has a lot of benefits– Extensibility – Reusability– Provides abstraction– Eliminates redundant code

• Inheritance allows a generalization of multiple child classes which inherit the similar characteristics from a common parent class.– Attributes (fields and properties)– Operations (methods)

• Child class can extend the parent class– Add new fields and methods– Redefine methods (modify existing behavior)

© Tyler Technologies 201310

Method Access Controls

• public : Access is not restricted.

• protected : Access is limited to the containing class or types derived from the containing class.

• private : Access is limited to the containing class.

© Tyler Technologies 201311

• Poly means “different” while morph means “forms”.

• In programming Polymorphism may be achieved through two concepts:– Overloading:– Overriding:

Polymorphism

© Tyler Technologies 201312

Overloading Concept

• Overloading is simply defined as the ability of one method to perform different tasks.

• Creating multiple methods with *same name which differ from each other by the change of parameter types or quantity

© Tyler Technologies 201313

Overriding Accelerate for Aircraft

Jet

Aircraft needs to alter the accelerate functionality provided by vehicle so it would override that method.

Vehicle

Car will use accelerate from the base class

Car

© Tyler Technologies 201314

Types of Relationships

Inheritance:

• “Is A” sort of relationship

For example – Car is a vehicle– Square is a shape– Motorcycle is a vehicle– Cat is a mammal– Mammal is an Animal