KEMBAR78
Object Oriented Programming | PPTX
© 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

Object Oriented Programming

  • 1.
    © Tyler Technologies20131 Agenda • Encapsulation • Inheritance • Polymorphism • Association • Aggregation • Composition
  • 2.
    © Tyler Technologies20132 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.
  • 3.
    © Tyler Technologies20133 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.
  • 4.
    © Tyler Technologies20134 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.
  • 5.
    © Tyler Technologies20135 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.
  • 6.
    © Tyler Technologies20136 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)
  • 7.
    © Tyler Technologies20137 How Inheritance works? Vehicle Automobile Car Motor Cycle Aircraft F-16
  • 8.
    © Tyler Technologies20138 What is being “Reused”?
  • 9.
    © Tyler Technologies20139 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)
  • 10.
    © Tyler Technologies201310 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.
  • 11.
    © Tyler Technologies201311 • Poly means “different” while morph means “forms”. • In programming Polymorphism may be achieved through two concepts: – Overloading: – Overriding: Polymorphism
  • 12.
    © Tyler Technologies201312 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
  • 13.
    © Tyler Technologies201313 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
  • 14.
    © Tyler Technologies201314 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

Editor's Notes

  • #2 Agenda The following topics shall be discussed in this presentation: Encapsulation Inheritance Polymorphism Association Aggregation Composition
  • #3 Basic Definition of the term Encapsulation: “Encapsulation refers to the condition of being enclosed, as in a capsule.“ Relating the Encapsulation concept with a capsule really helps in associating the word “encapsulation” with what it actually means, also it benefits in recalling the definition and the idea later on. In essence Capsule is a one word definition for Encapsulation.
  • #4 This is how you achieve Encapsulation by bundling appropriate attributes and methods, i.e. by creating a class. The class diagram shows attributes to be privately accessible while all the functions are public.
  • #5 In X++ you can decorate a method with the static keyword. This keyword instructs the system to create only one instance of the method regardless of how many instances of its class you create. Static methods are generally intended for cases where the following criteria are met: The method has no reason to access the member variables that are declared in the classDeclaration block of the class. The method has no reason to call any instance (non-static) methods of the class. Because static methods do not operate on objects, you do not need to create an instance of the class they belong to. Static methods cannot be called in the same way as nonstatic (instance) methods. Instead, you have to use the scope-operator ( :: ) ClassName::method() For example: Map::create(); If you wanted to create a method that uses a customer account to test whether a particular customer exists, this should be a static method. It would not make sense to create an object before calling this method—the object would already exist.
  • #6 Changes made to the code creates maintainability issues. Usually fields in a class are changed more often than the class’s constructors or methods. Encapsulation makes sure that when fields are changed, necessary changes can comparatively easily be done in the same class, and this change remain local to that class only, eliminating the chances of an external class being modified.
  • #7 Sole purpose of Inheritance is code reusability. Which establishes a subtype from an existing Type. Inheritance represents “is a” type of relationship, not to be confused with Association, Aggregation or Association as they are different concepts altogether.
  • #8 Representing in terms of a diagram, A vehicle is the most generalized Item under this example, so we can extract the common functionalities among all vehicles and put it under this class, Further more Vehicle is categorized under two subclasses one is automobile and the other is Aircraft. Automobile might have vehicles such as Car or Motor Cycle Where has Aircraft has F-16.
  • #9 As it is seen what Automobiles or aircraft have in common are put within Vehicle, for example there will always be a certain number of passengers traveling in a vehicle either it be Automobile or Aircraft. However there is no Altitude Value under Automobile. And there is no Gear value for an aircraft. So along with achieving specialization for the below classes we have reused the code by not defining common things in specialized classes and just putting them in a generalized class which is vehicle.
  • #10 By extending a class new functionality may be easily added up without entering redundant code. Base class’s code is being re used by the extended class without the need of entering it again. 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)
  • #11 The Public access modifier allows any class to access the method. All you have to do is create an instance of the method and for that instance the all public methods are accessible. If a method is defined as protected it can be accessed from within the class or from any derived class however any other class may not be able to access it. Private methods are only limited to be called from within the class. All attributes may always be private.
  • #12 Polymorphism has two concepts, Overloading and Overriding. Overloading is achieved within a class while overriding across different classes. It is important to note that Overloading is not supported in X++, Overloading is basically providing different implementations for a single method name, these implementations are distinguished by the signature of the method, the signature includes the parameters required to call the function, so we can achieve overloading by creating two functions with same name and different number of parameters or even with same number of parameters but with different types or order. For example consider we have an email method sendEmail(), This method sends email with default values, we can override this method and provide a separate implementation which takes a single string parameter as an input and give receiver's Email address in that parameter. We can provide another implementation that gets a list of string as a parameter and then later on in the implementation send the email to all the email addresses listed in that list of string.
  • #13 Overloading is basically providing different implementations for a single method name, these implementations are distinguished by the signature of the method, the signature includes the parameters required to call the function, so we can achieve overloading by creating two functions with same name and different number of parameters or even with same number of parameters but with different types or order. For example consider we have an email method sendEmail(), This method sends email with default values, we can override this method and provide a separate implementation which takes a single string parameter as an input and give receiver's Email address in that parameter. We can provide another implementation that gets a list of string as a parameter and then later on in the implementation send the email to all the email addresses listed in that list of string.
  • #14 Two classes Car and Jet have extended Vehicle. Jet has overridden the implementation provided for public method Accelerate method by Vehicle, hence the overridden implementation would be called for Jet class. Where as Car has provided no functionality for accelerate function so it will continue to use the same functionality as in Vehicle.