(OOP) OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING
You might have heard of OOPS many times. To keep it simple, Object oriented programming is the basic way of thinking or comparing programming with real life objects. ie; thinking programming in the real life way. We have been using this from late 1970’s. Let’s see how they actually work.
Classes are a fundamental part of object-oriented programming. They allow variables and methods to be isolated to specific objects instead of being accessible by all parts of the program. This encapsulation of data protects each class from changes in other parts of the program. By using classes, developers can create structured programs with source code that can be easily modified.
The class is one of the defining ideas of object-oriented programming. Among the important ideas about classes are:
A class can have subclasses that can inherit all or some of the characteristics of the class. In relation to each subclass, the class becomes the superclass.
Subclasses can also define their own methods and variables that are not part of their superclass.
The structure of a class and its subclasses is called the class hierarchy.
Principles of OOPs :
All the programming languages supporting object oriented Programming will be supporting these three main concepts:
Inheritance :
When a class acquire the property of another class is known as?inheritance. Inheritance is process of object reusability. Through inheritance, an object acquires some/all properties of another object.
For example:?Car is a classification of Four Wheeler. Here Car acquires the properties of a four-wheeler. Other classifications could be a jeep, tempo, van etc. Four Wheeler defines a class of vehicles that have four wheels, and specific range of engine power, load carrying capacity etc. Car (termed as a sub-class) acquires these properties from Four Wheeler (termed as a super-class), and has some specific properties, which are different from other classifications of Four Wheeler, such as luxury, comfort, shape, size, usage etc.
领英推荐
Encapsulation :
Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation. Encapsulation means hiding the internal details of an object, i.e. how an object does something. Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
Looking at the example of a power steering mechanism of a car. Power steering of a car is a complex system, which internally have lots of components tightly coupled together, they work synchronously to turn the car in the desired direction. It even controls the power delivered by the engine to the steering wheel. But to the external world there is only one interface is available and rest of the complexity is hidden. Moreover, the steering unit in itself is complete and independent. It does not affect the functioning of any other mechanism.
Similarly, same concept of encapsulation can be applied to code. Encapsulated code should have following characteristics:
Polymorphism :
Polymorphism?means to process objects differently based on their data type.?Polymorphism is where the method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method. However, in a normal inheritance tree, you don’t have to override any methods and therefore not all method calls have to be polymorphic.
Lets us look at same example of a car. A car have a gear transmission system. It has four front gears and one backward gear. When the engine is accelerated then depending upon which gear is engaged different amount power and movement is delivered to the car.
Polymorphism could be static and dynamic both. Overloading is static polymorphism while, overriding is dynamic polymorphism.
If in an interview if someone ask you to explain polymorphism, better explain it with some example.
“Imagine there is a class called Shape. You have to calculate the area of the shape.But you don’t know which shape will be passed on to calculate the area on run time. It can be a triangle, a square or a rectangle. All these shapes have a common property called area but the way this is calculated is different. So, we create a calculate area function and pass in to it two values.