BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

OOPs in Java: Encapsulation, Inheritance, Polymorphism, Abstraction

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: java

In the last article we discussed OOPs Concepts. If you have not yet checked it out, I would highly recommend you to read it so that you have a basic overview of all the Object Oriented Programming Concepts. In this guide, we will discuss four important features of OOPs with the help of real life examples.

Object Oriented Approach : An Introduction

Java is an object oriented language because it provides the features to implement an object oriented model. These features includes Abstraction, encapsulation, inheritance and polymorphism.

OOPS is about developing an application around its data, i.e. objects which provides the access to their properties and the possible operations in their own way.

Abstraction

One of the most fundamental concept of OOPs is Abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when you login to your Amazon account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to amazon server, how it gets verified is all abstracted away from the you.

Another example of abstraction: A car in itself is a well-defined object, which is composed of several other smaller objects like a gearing system, steering mechanism, engine, which are again have their own subsystems. But for humans car is a one single object, which can be managed by the help of its subsystems, even if their inner details are unknown.

Encapsulation

This post provides the theoretical explanation of Encapsulation with real-life examples. For detailed explanation on this topic with java programs refer encapsulation in java with example.

Encapsulation is:

  • Binding the data with the code that manipulates it.
  • It keeps the data and the code safe from external interference

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:

  • Everyone knows how to access it.
  • Can be easily used regardless of implementation details.
  • There shouldn’t any side effects of the code, to the rest of the application.

The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other.

A example of encapsulation is the class of java.util.Hashtable. User only knows that he can store data in the form of key/value pair in a Hashtable and that he can retrieve that data in the various ways. But the actual implementation like, how and where this data is actually stored, is hidden from the user. User can simply use Hashtable wherever he wants to store Key/Value pairs without bothering about its implementation.

Inheritance

This post provides the theoretical explanation of inheritance with real-life examples. For detailed explanation on this topic with java programs refer inheritance with examples and types of inheritance in java.

  • Inheritance is the mechanism by which an object acquires the some/all properties of another object.
  • It supports the concept of hierarchical classification.

For example: Car is a four wheeler vehicle so assume that we have a class FourWheeler and a sub class of it named Car. Here Car acquires the properties of a class FourWheeler. Other classifications could be a jeep, tempo, van etc. FourWheeler 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 FourWheeler, and has some specific properties, which are different from other classifications of FourWheeler, such as luxury, comfort, shape, size, usage etc.

A car can have further classification such as an open car, small car, big car etc, which will acquire the properties from both Four Wheeler and Car, but will still have some specific properties. This way the level of hierarchy can be extended to any level.

Java Swing and Awt classes represent best examples for inheritance.

Polymorphism

This post provides the theoretical explanation of polymorphism with real-life examples. For detailed explanation on this topic with java programs refer polymorphism in java and runtime & compile time polymorphism.

  • Polymorphism means to process objects differently based on their data type.
  • In other words it means, one method with multiple implementation, for a certain class of action. And which implementation to be used is decided at runtime depending upon the situation (i.e., data type of the object)
  • This can be implemented by designing a generic interface, which provides generic methods for a certain class of action and there can be multiple classes, which provides the implementation of these generic methods.

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. The action is same applying gear but based on the type of gear the action behaves differently or you can say that it shows many forms (polymorphism means many forms)

Polymorphism could be static and dynamic both. Method Overloading is static polymorphism while, Method overriding is dynamic polymorphism.

  • Overloading in simple words means more than one method having the same method name that behaves differently based on the arguments passed while calling the method. This called static because, which method to be invoked is decided at the time of compilation
  • Overriding means a derived class is implementing a method of its super class. The call to overriden method is resolved at runtime, thus called runtime polymorphism
❮ PreviousNext ❯

Top Related Articles:

  1. Java – Static Class, Block, Methods and Variables
  2. Multithreading in java with examples
  3. Constructor Overloading in Java with examples
  4. Encapsulation in Java with example
  5. Java date difference

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Juan Arango says

    May 4, 2013 at 1:20 AM

    Great analogies. Great work!

    Reply
  2. Arun Kurup says

    July 11, 2014 at 7:45 AM

    Thanks for explaining the concepts with real time examples which helps us too understand them more easily.

    Reply
  3. A Sinhalese says

    March 14, 2015 at 4:56 AM

    Great article serious !! well defined.
    Thanks for the contribution..

    Reply
  4. kalyani says

    June 4, 2015 at 9:25 AM

    Great Explanation. Will never forget about definitions.Thanks a lot.Now got unforgettable picture of OOPs Concepts.

    Reply
  5. bohr says

    July 10, 2015 at 7:11 PM

    Great explanation. However, I am a bit confused when you mentioned that polymorphism can be static by method overloading. How we can achieved polymorphism through overloading? Can you give an example? thank you

    Reply
  6. Jides Okpalaeke says

    August 20, 2015 at 9:00 AM

    I love your clarity of terms..

    Reply
  7. Udhayakumar says

    September 15, 2015 at 12:27 PM

    Great Work! The way you explain the concepts are very clear and understandable. But I want know the difference between Abstraction and Encapsulation. I read lot of theory regarding Abstraction and Encapsulation.However Still I am confusing myself. Could you please explain the difference between Abstraction and Encapsulation?

    Reply
  8. Ajit says

    October 4, 2015 at 3:51 PM

    Where is abstraction article

    Reply
  9. Rahul says

    May 26, 2016 at 12:36 PM

    UI is very attractive!Simple and Lucid language!Keep it going!

    Reply
  10. Indeevar Bharadwaj says

    June 27, 2016 at 6:23 AM

    Does abstraction hides the implementation completely or some implementations are shown?

    Reply
    • Lama says

      July 29, 2017 at 8:23 PM

      abstraction concept means hiding, but it can be either full or partial ( hiding all or part of the data)

      Reply
  11. Fabiano says

    August 28, 2016 at 4:38 AM

    The most comprehensive and to the point explanations about OOPS ever! I was tired of silly analogies I read from most books I bought. If I ever knew about this website it would have save lots of money and time. Thank you!

    Reply
  12. Gowtham M says

    July 10, 2017 at 1:47 PM

    Thanks for Simple words and real time examples. It helps me to co-relate easily.
    Kudos to Creator of this Article!!!!!!!!

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap