Skip to main content

Command Palette

Search for a command to run...

Master OOPS in Java in 10 Minutes – Simple Examples Every Developer Should Know

Master the 5 core OOPS concepts in Java—Abstraction, Inheritance, Polymorphism, and Encapsulation—to drastically reduce code complexity

Published
3 min read
Master OOPS in Java in 10 Minutes – Simple Examples Every Developer Should Know
P

Software Engineer with expertise in Java backend development and automation. Enthusiastic about Data Structures, Algorithms, and writing technical content that helps developers understand concepts through real-world examples and hands-on implementation.

Java became the heartthrob of techies because of its powerful Object Oriented Programming (OOP) system that revolutionized software engineering by moving it from traditional function-oriented methodologies to efficient, object based methodology. In OOP, data and its associated code (methods) is bound together in units called objects; by binding together the data structures, complex applications can be developed which are easier to understand, manage and maintain.

The Foundation: Classes and Objects

The Classes and Objects will form the basic unit within Java. Class: Class is a logical template based on which, objects are created. An instance is the physical, real-world folder made from that blueprint taking up space in the system memory.

• Analogy: A class is to an object what a blueprint for a house is to the actual, built house on land.

• Key Action: The object is instantiated by a developer with the 'new' keyword, resulting in memory being allocated on the heap (Java Virtual Machine (JVM) creates an instance of the class).

Encapsulation: The Secret Compartment

Encapsulation is the concept of encapsulating data (variables) and the code used to operate on it into one package. It is used mainly as a mechanism of data hiding so that the state of the object remains secure and insulated from external influences.

• How to implement: Class variables are declared private and use accessor and/or mutator methods to control access.

• Real-world Example: A bank account is an object that encapsulates your balance. You cannot alter your balance directly but can only use approved functions like deposit() and withdraw() to perform transactions.

Abstraction: Hiding the Messy Details

Encapsulation might conceal information for security; on the other hand, abstraction conceals complexity for ease of interaction. Abstraction is focused on what the object does rather than how it does it.

• Tools: For the implementation of abstraction in Java, abstract classes (partial implementation) and interfaces (100% abstract) are used.

• Real-world Example: When using the steering wheel and pedals while driving a car, you are dealing with an abstract interface without knowing what goes on with the internal combustion, hydraulic pressure, etc.

Inheritance: Reusing Family Traits

Inheritance allows a new class to derive properties and behaviours from an existing parent class, establishing an "is-a" relationship. This promotes massive code reusability.

Types: Java supports single, multilevel, and hierarchical inheritance.

The "Diamond Problem": Java strictly prohibits multiple inheritance for classes (one child with two parents) to avoid ambiguity if two parents provide conflicting methods.

Example: A Dog is-a Mammal, which is-an Animal. The Dog inherits the breathe() method from the parent classes.

Polymorphism: The Shapeshifter

Polymorphism, meaning "many forms," allows a single method or interface to represent different underlying actions.

Static (Compile-time): Achieved through Method Overloading, where multiple methods have the same name but different parameters.

Dynamic (Runtime): Achieved through Method Overriding, where a subclass provides its own implementation of a method already defined in its parent class.

Example: A single draw() command can result in drawing a circle, a square, or a triangle depending on the specific object being called.

The 4 Pillars of OOPS in Java