top of page

OOPs in Java with “Toaster”

In our day-to-day activities, we all come around different objects and use them for various purposes. Similarly, in the computer programming world, there is a concept of objects. In this blog, OOPS concepts in Java have been described briefly. Before proceeding further, let me give a small description of Object-oriented Programming System (OOPs) considering Java as language.

OOPs: The object is a real-world entity such as a laptop, mouse, mobile, book, paper, toy, etc. OOPs is an acronym for Object-oriented Programming System. It is a methodology used to develop programs using objects. An object has its behavior and attributes.

Java is a general-purpose, platform-independent, object-oriented programing language. Java is used to develop applications in different fields like finance, big data, mobile, e-commerce, and so on. Java is an object-oriented language as it uses an object in every program. OOPs concepts has been used in Java to develop applications which helps to improve code readability and reusability by writing java program efficiently. Some of the OOPs concepts in Java are Abstraction, Encapsulation, Polymorphism and Inheritance.

Here I have considered one of the most widely used kitchen appliance “Toaster” to relate to OOPs concepts.

Object: As described above, Object is a real world entity. It has states and behaviors. Toaster is an object which has states like black color, brand name-Bella and reheating, toasting as behaviors.

Class: Class is a group of objects which contains methods, constructors, fields, blocks, nested class and interfaces. It is a blueprint for creating objects. In the example, there are four sides with heating element in the center, its specification like reheating and toasting, and options like Gluten free and toasting Bagel can be considered as class.

Abstraction: Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it will do. In this scenario, when we consider toasting the bread slices, the complex details as how the coil heats up when we select the start button after inserting the bread slices is hidden from the user. The implementation details as to how coil glows red and also the temperature of coil changes based on user settings will not be known to user.

Encapsulation: Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Encapsulation is a programming technique that binds the class members (variables and methods) together and prevents them from being accessed by other classes. Toaster encapsulates two options where we can reheat variety of bread. Reheating Bagel and Gluten free bread, changing the temperature control, on and off sliders are variables and methods then the toaster will act as a class and whole process can be described as encapsulation.

Key Differences between Abstraction vs Encapsulation

  • Abstraction shows only useful data by providing the most necessary details, whereas Encapsulation wraps code and data for necessary information.

  • Abstraction is focused mainly on what should be done, while Encapsulation is focused on how it should be done.

  • Abstraction hides complexity by giving you a more abstract picture, while Encapsulation hides internal work so that you can change it later.

  • Abstraction helps you to partition the program into many independent portions, whereas Encapsulation is easy to change with new requirements.

  • Comparing Encapsulation vs Abstraction, Abstraction solves problems at the design level while Encapsulation solves problems at the implementation level.

  • Abstraction hides the irrelevant details found in the code, whereas Encapsulation helps developers to organize the entire code easily.

Why you need Abstraction?

Here, are the main reasons why abstraction is needed for Object-Oriented Programming:

  • Helps you to simplify the representation of the domain models.

  • Abstraction hides the irrelevant details found in the code.

  • Abstraction helps you to partition the program into many independent concepts.

  • Offers the greatest flexibility when using ADT(Abstract Data Type) objects in different situations

Why you need Encapsulation?

The main advantages of encapsulation are

  • It improves the maintainability of an application.

  • Offers flexibility to the user to use the system very easily

  • Helps the developers to organize the code better

  • Makes the overall coding process easier, as you are only concerned with what another class does, not how it does it

Polymorphism: Polymorphism means “many forms”. That is one thing that can take many forms.

Polymorphism is a concept by which we can perform a single task in different ways. That is, when a single entity behaves differently in different cases, it is called polymorphism in Java. We can achieve flexibility in our code using polymorphism because we can perform various operations by using methods with the same names according to requirements. The main purpose of toaster is toast the bread, however in toaster we can toast Bagel, reheat pizza, re crisp fries, cook an egg, reheat garlic bread. Toaster does more than one operation. Toaster basically does toasting and reheating.

Inheritance: Inheritance is defined as a process where one class acquires the properties from other class. The class which inherits the properties is known as child /sub class and the class whose properties are inherited is known as class. This concept is similar to real time example where children inherit the properties of parents.

In this example, Toaster is mainly used to toast the bread. There are different brands of toaster which inherits the basic functionality of toasters and also add their own new UI and other functional features. There are 2 slice and 4 slice toasters as well. All toasters internal implementation remains same. Each brand has their own color, size, shape, buttons and other new features. Inheritance concept is implemented in this example.

As I said before, wherever there is an object, we can co-relate it with OOPs concept and these concepts are used in programming world as well as in our daily lives.

Some of the advantages of using OOPS concepts are

  • OOPs Concepts in Java offer easy to understand and a clear modular structure for programs.

  • Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development cost.

  • Large programs are difficult to write, but if the development and designing team follow OOPS concepts, then they can better design with minimum flaws.

  • It enhances program modularity because every object exists independently.

Hope you learnt the basic Java OOPs concepts with this simple example.

Thank you for reading.

191 views0 comments

Recent Posts

See All


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page