The document discusses object-oriented programming concepts including encapsulation, inheritance, polymorphism, and relationships. Encapsulation involves enclosing attributes and methods within a class. Inheritance allows code reuse by establishing subtypes that inherit characteristics from a parent class. Polymorphism can be achieved through method overloading and overriding. Relationships in object-oriented programming include inheritance for "is-a" relationships and aggregation/composition for "has-a" relationships.
Agenda
The following topics shall be discussed in this presentation:
Encapsulation
Inheritance
Polymorphism
Association
Aggregation
Composition
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.
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.
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.
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.
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.
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.
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.
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)
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.
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.
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.
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.