- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
In simple terms, hiding out how something is done and showing only what is done to the outside. This concept is very popular to construct low coupling components and system.
For instance, Lets say starting an automobile; Although mechanism for starting an automobile could be different from not only the type of the automobile but the manufacturer. User does not bother about that provided that the vehicle starts with the key. He only needs to to know there is a key hole in the vehicle and the matching key is provided to start with it. The internal vehicle starting procedure and process which he does not want to know.
The importance of abstraction comes into action when maintenance takes place. In case of replacements of the vehicle, user does not want to change the key only the required part has to be replaced.
In Java, Interface and Abstract class are used to implement abstraction.
Here the bike, car and bus have own start procedure and components but they all need a key which must be used by the driver(client) to start the vehicle. The only need to know is to the driver is the key and the key hole.
Abstraction is used to implement Design Principles and Patterns which will be discussed later.
Encapsulation
In Object Oriented Paradigm wrapping up state(fields) and behavior(methods) into a self contained entity called Encapsulation. This differs from another close going and often misunderstanding concept called information(data) hiding.
Information hiding: Restricting access, reading and writing to fields with use of methods and access control. For instance take into account following Person object in java.
Here the developer restricts the user setting a minus value as age to the Person object. Also, the developer can set the upper boundary according to the requirement.
Encapsulation goes beyond information hiding that permits the underlying data storage mechanism as well as the methods that operate upon that data to be altered without affecting the object's existing consumers. In other words, the underlying implementations may change quite drastically, but their effects are limited to the class of object itself.
Inheritance
Children inherits from parent in common tongue is called as inheritance. In object oriented, When a class(Super class) extends another class(Sub class), sub class receives features(attributes) and behavior(methods) from its parent class. Inheritance provides re usability and extendability in Object Oriented design. The inheritance can be controlled by use of information hiding which will subsequently not appear in sub classes.
Polymorphism
Ability to take more than one form. For instance + sign in Java can be used for many instances. According to the situation it provides different outputs.
E.g. With 2 Strings it performs concatenation.
"Hello "+"Java" = Hello Java
With 2 ints it performs addition
10 + 15 = 25
With two plus signs in performs increment
10++ = 11
Another regular use of polymorphism is method overloading and dynamic binding.
No comments:
Post a Comment