Oops

Encapsulation:

means grouping up of related members (variables and functions) into a single unit called class. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. 

RealWorldExample:

In our everyday lives, most of us use a video recorder via either the controls on the front of it or via a remote control. We all know which buttons to press in order to watch a program or record a program, this is done via the interface we have with the video recorder. The manufacturer can change the internal workings of the hardware, but this would not often affect us as a user because the interface we have would be the same. That is a play button, a record button, fast forward, rewind, stop, pause, etc.

Coding Encapsulation

The following code demonstrates a class containing some data and a method that acts upon the data (without data hiding):

 Collapse | Copy Code

  class Counter {   public int Count;     public Counter()   {     Count = 0;   }     public void incCount()   {     Count++;   } }    

Although the member variable can be directly accessed, the data and its method is encapsulated in a class.

DataHidding:

Data hiding is linked to encapsulation; however data hiding is not encapsulation as it is sometimes described in this way.

Data hiding is simply the means to remove direct access to an object’s information, by providing operations that perform actions on the data. This way any changes to the value of the data must come through the interface to the data, which is an operation. Thus we use access operations or properties.

An Example

 

In our person object, access to the data forename is supplied through the access operations set forename() and get forename().

Coding Data Hiding

The following code demonstrates a class containing some data that is hidden:

 Collapse | Copy Code

class Counter {   private int Count;     public Counter()   {     Count = 0;   }     public void setCount( int newVal )   {     Count = newVal;   }     public int getCount()   {     return Count;   } } 

 This piece of code is also encapsulated, showing that you can have encapsulation without data hiding, but you cannot have data hiding without encapsulation.

Abstraction:

Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.

What is Polymorphism?

Polymorphism means one name many forms. Polymorphism means one object behaving as multiple forms. One function behaves in different forms. In other words, "Many forms of a single object is called Polymorphism."

Example : 

Your mobile phone, one name but many forms:

With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.

There are two types of polymorphism:

In which scenario we use Abstract Classes and Interfaces?

Interface:

–> If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.

Abstract Classes

–> When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. 

Use an abstract class

Use an interface