Object Oriented Programming (OOPs)

Pillars of Object Oriented programming

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

What Is Abstraction?

  • Abstraction is showing only the essential details.
  • In real life we have example like mobile, TV where we show only what is required. For mobile there is screen, port for charging and headset and buttons. For TV we have Screen, On-off button and ports for connection.
  • In terms of Application, We only expose the API’s that other user needs. Internally which other API is used, what is sequence that is hidden from user.
  • Abstraction is achieved using Abstract class and Interface.

What is Encapsulation?

  • Encapsulation is hiding the unessential details. Encapsulation is also called data hiding.
  • Encapsulation is a process of binding or wrapping the data and the functions that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.
  • One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
  • One example of Encapsulation is private methods; clients don’t care about it, You can change, amend or even remove that method  if that method is not encapsulated and it were public all your clients would have been affected.
  • In term of real life example, for mobile how the button functionality is implemented, how validation is done and where data is stored is hidden from user.
  • In term of Application we decide how that API works internally.
  • Encapsulation is achieved using Private modifier.

Example of Abstraction and Encapsulation

Calculator

Add method

  • ValidateNumbers
  • SaveNumbers
  • AddNumbers
  • Result

Abstraction

  • Now you need to decide which API to expose. Whether 1 API is sufficient or multiple API’s will be required.

Encapsulation

  • How that API works is hidden from user.
  • Whether that API validates records first or not
  • How database connection is done
  • How saving of those records is done

What is Inheritance?

  • Inheritance is a way by which user can create a new class called derived class from Base class.
  • Derived class inherits the properties and functions of base class.
  • Inheritance is a way by which we achieve code reusability.
  • For example in real word we have Vehicle class or Shapes class.
  • Vehicle will have Body, Engine, Seats, wheels etc. Common functionality can be kept in base class and derived class will have required functionality.
  • It is moving from generalization to Specialization.

What is Polymorphism?

  • Polymorphism means one name many forms.
  • In other words, “Many forms of a single object is called Polymorphism.”
  • There are 2 types of polymorphism:
    • Static Polymorphism
      • Function Overloading can be done by having
        • Different number of parameters.
        • Different sequence of parameters.
        • Different type of parameters.
      • Operator Overloading
        • Giving different meaning to operator. + operator is used for addition. Same can be used for concatenation.
  • Run-time Polymorphism
    • Achieved using virtual and override keyword.
    • The base class method is overridden in derived class using override keyword.

Polymorphism examples:

Example-1:

  • A Teacher behaves to student.
  • A Teacher behaves to his/her seniors.

Example-3:

Your mobile phone, one name but many forms

  • As phone
  • As camera
  • As mp3 player
  • As radio
It's only fair to share...Email this to someoneShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *