Basic Concepts of OOP #2






















In our previous lesson we took a look at OOPS - Object Oriented Programming Concepts and we specifically reviewed the process of Data Abstraction and plus we also took the look at Data Encapsulation aka Data Hiding. Today in this lesson we took a look at other OOPS concepts such as Modularity, Inheritance and Polymorphism. So let's get started!!

Related post.

Basic Concepts of OOP #1 


3. Modularity - The act of partitioning a program into individual components is called modularity. Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

To understand this let us take an example of music system. Let us assume that our program represents the music system. The music system comprises of speakers, casette-player, record-player, cd-player, tuner etc. Now, these parts are complete unit in themselves, yet they are a subpart of music system. This is modularity.

Similarly, we can achieve modularity in our programs also. A program can be divided into various modules each representing speakers, tuner etc. Each module is a complete unit in itself but yet it works in accordance with other modules in order to achieve one single goal i.e., music.

In object oriented programming language, classes and objects form the logical structure of a system. We place them in modules to produce the system's physical architecture.

4. Inheritance - Inheritance is the capability of one class of things to derive capabilities or properties from another class. For example, we are humans, we inherit from the class 'Human' certain properties, such as ability to speak, breathe, drink etc. But these properties are not unique to humans. The class 'Humans' inherits these properties from the class 'Mammal' which again inherits some of its properties from another class 'Animal'. Thus, it allows one class to inherit properties of other classes, this module is much closer to the real world. The principle behind this sort of division is that each subclass shares common characteristics with the class from which it is derived.

















There are few major reasons behind introducing of inheritance in OOPS.
  • The idea of reusability. It allows the addition of additional features to an existing class without modifying it. One can derive a new class(sub-class) from an existing one and add new features to it. Suppose we have a class 'student' and then all we really need to add the extra features to 'graduate student' that describe the differences between students and graduate student. Notice the reduction in amount of typing and efforts.
  • The transitive nature. For example, if a new class 'graduate student' has been declared as a sub-class of 'student' which itself is a subclass of 'person' then 'graduate student' must also be a 'person' i.e., inheritance is transitive. If a class A inherits properties of another class B, then all subclasses of A will automatically inherit the properties of B. This property is called transitive nature of inheritance. 






Suppose if a base class 'person' has any bug that must be corrected. After correcting a bug in base class 'person' , it will automatically will be reflected across all classes that inherit from base class 'person' i.e., 'student' and 'graduate student', if the base class 'person' has been inherited without changes. See the reduction in amount of efforts that one would have done if each class inherited from base class 'person' was to be modified separately, a gifted benifit of being transitive.

Note: A subclass defines only those features that are unique in it.

Although 'student' is a 'person' yet the reverse is not true. A 'person' need not to be 'student'. All members of 'student' are not members of class 'person'. The class student has properties it does not share with class 'person' i.e., unique properties, for e.g. 'student' has a 'marks-percentage' but 'person' does not have.

5. Polymorphism - Polymorphism is the ability for a message or data to be processed in more than one form. Polymorphism is the property by which the same message can be sent to objects of several different classes, and each object can respond in a different way depending on its class. It is so important that languages that don't support polymorphism can't advertise themselves as OO languages. Languages that support classes but not polymorphism are called object based languages. for e.g. Ada

It is the concept that supports the capability  of an object of a class to behave differently in response to a message or action. To understand this let us take an example, 'Human' is a subclass of 'Mammal'. Similarly 'dog' , 'cat' are also subclasses of 'mammal'. If a message 'see through darkness' is passed to all mammals then they all behave alike. Then 'humans' and 'dogs' will not be able to view at night whereas 'cats' will be able to view during night also. Here cats(mammals) can behave differently than other mammals in response to a message or action. This is polymorphism.

Following figure illustrates that a single function name can be used to handle different number and different types of arguments. This is something similar to a particular word having several different meanings depending on the context.












In object oriented programming languages polymorphism is achieved through overloading like function overloading, operator overloading, constructor overloading etc.

Done!!

Like I always say if you like this post share it, like it, follow us , give your feedback and mention us in comments. Thank You!!
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment