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!

14 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
  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. Great post. keep sharing such a worthy information.
    Data Science Training in Chennai

    ReplyDelete
  5. 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
  6. 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
  7. 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
  8. Accelerate Your Career with Our Software testing training online. Learn the fundamentals of Quality Assurance and Software Testing in this comprehensive program. Master various testing methodologies, tools, and best practices to ensure top-notch software quality. From manual testing to test automation, our expert-led course covers it all. Gain hands-on experience with real-world projects, honing your problem-solving skills. Prepare confidently for QA certification exams and become a sought-after QA professional in the IT industry. Enroll now and unlock a world of opportunities in software testing and quality assurance.

    ReplyDelete
  9. "Here I saw several articles and posts published on this site, I am more interested in some of them, will provide more information on these topics in future articles.TaxFillingIndiaGST Registration Near meitr filing near meghmc trade license in HyderabadFood License Registration in HyderabadTrademark Registration in Hyderabad"

    ReplyDelete