Sunday, February 22, 2015

Objection Orientation: Polymorphism, Abstraction, Encapsulation, and Inheritance

Developers who are new to object-oriented programming often struggle to understand the long-winded explanations that are scattered across the interwebs.  The intent of this post is to ease the pain (hopefully) by summarizing the main concepts of object orientation in a concise and precise format by providing a definition, two examples, and a brief explanation of its benefits.

Polymorphism

Definition:  Polymorphism is the concept where a parent type can have many sub-types.

Example #1:  ArrayList and LinkedList are sub-types of the parent type List.

Example #2:  Car, Motorcyle, and Bus are sub-types of the parent type Vehicle.

Benefits:  Polymorphism allows you to use a single parent type to interact with all possible sub-types.  It also helps to decouple your code.  When you are working with a List object, you can freely swap out the underlying implementation from an ArrayList to a LinkedList and vice-versa.

Abstraction

Definition:  Abstraction is the generalization of common functionality across different objects.

Example #1:  List is an abstraction of ArrayList and LinkedList because it generalizes the common functionality of adding and removing elements.

Example #2:  Vehicle is an abstraction of Car, Motorcyle, and Bus because it generalizes the common functionality of accelerating and braking.

Benefits:  Abstraction allows you to interact with different objects via a common interface.

Encapsulation

Definition:  Encapsulation is the packing (hiding) of data and implementation details into a class or function.

Example #1:  Data and implementation details about a car are encapsulated (packed, hidden) into a Car class.  Access to its data can be controlled via public, protected, and private modifiers.

Example #2:  The functionality to accelerate and brake a car is encapsulated (packed, hidden) into functions.  Access to these functions can be controlled via public, protected, and private modifiers.

Benefits:  Encapsulation allows you to hide implementation details and protect against mis-use by other classes.  It also helps to decouple your code.  For example, I could modify the formula in the accelerate function without breaking any code dependencies on that function.

Inheritance

Definition:  Inheritance is the re-use of a base class's functionality by a sub-class.

Example #1:  Car is a sub-class that inherits the functionality of its base class Vehicle, including the data (currentHeading, velocity, weight) and functions (turn, accelerate, brake).

Example #2:  A Car can turn because it inherited the functionality from its base class Vehicle.

Benefits:  Inheritance allows you to re-use functionality and reduce duplicate code.

Polymorphism, abstraction, encapsulation, and inheritance are distinctly different concepts, but together they support the paradigm of object orientation.  I hope this post cleared up any misunderstandings of these four pillars object orientation!

21 comments:


  1. Once extensive comprehensive extensive wide range has used an company, it will end and quit and trend trader software efficiency. Therefore, any extensive comprehensive extensive wide range can only be used once. Business accessibility rules like stop-loss or take-profit levels, accessibility lot size and falling can be pre-specified before business is used.

    ReplyDelete
    Replies
    1. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. Final Year Project Domains for IT

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cse. Angular Training

      Delete
  2. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    data analytics courses in hyderabad

    ReplyDelete
  3. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Start your journey with Best SAP Course and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete
  4. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    Oracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore

    ReplyDelete
  5. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.

    data science institute in bangalore

    ReplyDelete

  6. Great post. keep sharing such a worthy information.
    Data Science Training in Chennai

    ReplyDelete
  7. The NoteZilla Crack is a highly functional calendar and task manager as well. Once you've organized them, they can be categorized.Notezilla Portable

    ReplyDelete
  8. Adobe Master Collection CC 2022 is a collection of applications from the Creative Cloud 2022 line and several junior version programs combined by a single .Adobe CC 2022 System Requirements

    ReplyDelete
  9. March 20, 2022 by Admin The wishes we have provided here for Christmas and new year are very new and are updated very recently.Formal Christmas Wishes

    ReplyDelete