skill level required: novice/medium
experience required: having programmed at least in any 1 language
Perl's object oriented concepts revolve, majorly, around the following 5 things:
- Object - Anything that provides a way to
- locate
- access
- modify
- secure data
- Class - A description of what data is accessible through a particular kind of object and how they are accessible
- Method - Is the means by which the data is accessed, modified or processed
- Inheritance - The way in which the existing classes of objects can be upgraded to provide additional data or methods
- Polymorphism - The way by which different objects can respond distinctively to the same message depending on the class to which they belong.
Evolution:
Note: In earlier days of boring imperative C programming, programmers noticed a recurring pattern when the programs got bigger:
- that of particular data types(structs and primitive types) and associated functions that exclusively operated on these data.
- They noticed themselves grouping these together for obvious reasons(debugging and maintainability among many other reasons).
- Soon they recognized and built upon this pattern, an entity, which provided access to these data through permissions(private, public etc) as well as through methods(functions). This entity was later named 'Object'.
- Of course when making a language object oriented, there were also considered several other criteria but the above point summarizes how the OOP concepts came into being.
Objects and Classes:
Objects store the data as attribute values in attributes. These attributes are nothing but variables.
Classes are blueprints for objects that define attributes and methods that operate on those attributes.
Objects of the same class have the same
- interface(or the same set of exposed methods)
- and the same implementation(actual code for implementing the methods and the variables they access)
Multiple Inheritance:
Perl allows multiple inheritance. Multiple inheritance has merits and demerits:
Merits:
- applying new changes(attributes and methods) to a set of classes is easy
Demerits:
- the problem of diamond inheritance: class D is a child class of class B and class C. Both B and C are subclass of class A. So when inheriting from B and C, D will have multiple copies of the same attributes from A.
- the problem of same method in parent classes: method 'do' is invoked on C who gets it from B as well as C. So which 'do' would be invoked?
Important terminologies:
Inheritance Polymorphism: accounts for the reason why a child class can still respond to a message which is not implemented in it but, is implemented by its parent. If the method is implemented either in the target class or in any of its parent, it can respond to the message.Interface Polymorphism: accounts for the reason why any class with the specified method can respond to this method. Either it or its parents can have the method implemented. This is the dynamic polymorphism implemented in iOS Objective C. It will only be known during runtime as to whether the object has the specified method.
Abstract Class and Abstract Methods: Abstract class is a class nearer to the top hierarchy, which is there just to provide a set of common methods, but is not to be instantiated and the methods of which need to be implemented mandatorily in the target class.
Since developers may instantiate the objects of these classes as well as, forget to implement the methods specified, the engineers of OOPS decided to implement the Abstract Class. So if a class extends this class, the compiler will generate an error in case the developer misses out on the specified methods and will also generate error in case he tries to instantiate the object of the Abstract class.
*Note* - - that the creation of the abstract class objects does not cause the compiler to throw errors but, it is the invalid method call on this object that throws the error. This is because the abstract methods only have definitions and no real implementations.
In general, the subroutines in C and the object methods in any OOP language appear to be the same except for the apparent name-spacing (aMethod() and myObj.aMethod() ). This gives OOP an additional advantage that, while subroutines can be mistakingly called on any data, same is not true about object methods. Only those objects that have the method can act on the data( that is, yourObj.aMethod() will fail if not implemented).
Aggregation: is the process of creating simpler objects as the attributes of more complex ones. Aggregation helps in decoupling the public interface of a data from its private implementation.
Genericity: is the means by which the inherited classes can levy just the code from its parents and not its type. By type we mean the type of the data(attributes and method return values). Consider for e.g.
assume the first <whatever> is the largest <whatever>
for the remaining <whatever>s....
if the next <whatever> is larger than the current largest <whatever>
replace the current largest <whatever> with the next <whatever>
return the current largest <whatever>
The <whatever> in the preceding example could be a string, a number etc but what matters is the algorithm. Genericity in OOPs can thus allow for the algorithm inheritance and not the type inheritance.
Persistance: is the ability of the objects to persists between 2 different executions of the same program. The objects are usually serialized or encoded to files or DB. OOP languages implement 2 types of persistance: fine granular persistance and coarse granular persistance.
Fine granular records the state of the objects at regular intervals. This would mean more disk space. Hypertalk is a language that implements this mode.Coarse granular records the state of the objects at the end when the program exits. One drawback being that the object state could not be reproduced if the program crashes midway.
No comments:
Post a Comment