Java OOP Concepts with Real-World Examples

Nipuni Premadasa
Nerd For Tech
Published in
5 min readDec 30, 2022

--

Hello Readers!!!👋

Through this article, you can learn about OOP concepts in Java. As a programmer, you should know about these concepts to increase the readability, flexibility, and maintainability of your programs.

Also, If you are looking for a good Software Engineering internship opportunity, you should definitely have a good understanding of OOP concepts in java with real-world examples before going for an interview. During interviews, many interviewers are asking about OOP concepts and real-world examples of them. Therefore, I thought to write an article on this famous question to help all programmers and interviewees😉.

First, we need to know what OOP is…🤔

OOP stands for Object-Oriented Programming. The object is a real-world entity and the class is a collection of objects. So, OOP is a method to design a program using classes and objects.

There are 4 types of OOP concepts to simplify the software development process and maintenance. They are,

  1. Abstraction

2. Inheritance

3. Polymorphism

4. Encapsulation

Cool…😎 Let’s go through them one by one😃.

1. Abstraction

Abstraction is selecting data from a large number of data sets and showing only necessary data to the user. The non-essential details are not displayed to the user. The main purpose of abstraction is data hiding. It helps to reduce programming complexity.

There are two methods to achieve abstraction. They are,

1. Abstract class

i) An abstract class is a restricted class that cannot be used to create objects.

ii) It is used to provide common method implementation to all the subclasses.

iii) There may be one or more abstract methods inside the abstract class.

iv) Non-abstract methods also can be included in abstract classes.

2. Interface

i) The interface is a blueprint of a class.

ii) It can contain static, constant, and abstract methods.

iii) But it cannot contain non-abstract methods.

iv) The interface also cannot be used to create objects.

Difference between abstract class and interface

Difference between abstract class and interface

Real-world example: Withdrawing money from an ATM is a good example of abstraction. You need to know how to use an ATM to withdraw money. You need to enter your Visa card into the machine, provide a security code and answer some simple questions. But you don’t need to know how the ATM is working internally to withdraw money. You just interact with a simple interface that doesn’t require any knowledge about the initial implementation. All of that details are hidden from you.

2. Inheritance

Inheritance is a method that one object inherits common properties of another object. It provides code reusability. Inheritance is used to achieve runtime polymorphism.

There are three types of inheritance

i. Single Inheritance

ii. Multilevel Inheritance

iii. Hierarchical Inheritance

Types of Inheritance

Real-world example: Mobile phones are the best example of inheritance. Basic mobile functionalities are dialing and receiving a call and sending a message. So, smartphone brands use these core functionalities and add new features to their brands apart from them.

3. Polymorphism

Polymorphism is a process that performs a single action in different ways. In Java, we can use method overloading and method overriding to achieve polymorphism.

· Overloading — Create different methods with the same name and different parameter signatures (number of parameters, data types of parameters, order of parameters) within a class.

· Overriding — method in a subclass has the same name, the same parameter signature, and the same return type of its superclass, then the method in the superclass is overridden by the method in the subclass.

Difference between overloading and overriding

Difference between overloading and overriding

Real-world example: A best real-world example is a man. A man can be the husband of someone, the father of his children, and the manager of his company at the same time. So the same man performing different roles is called polymorphism.

4. Encapsulation

Encapsulation is the wrapping up data or code together into a single unit. It is like a capsule with various medicines wrapped inside. It controls the accessibility of the methods and variables inside the class. We can make a fully encapsulated class with all the members of the class private. Then using getters and setters we can get and set the data inside the class.

Real-world example: when considering your banking account, the balance variable should be private. Otherwise, anyone can access it and know your account balance. So they declare the balance variable as private for making the account safe. If you want to access your account balance you have access through the method defined in the class by providing a username, and password for authentication.

Conclusion

We have discussed all 4 OOP concepts in detail. Now I think you have a good understanding of OOP concepts with examples😎. Using these 4 concepts you can easily make your java code more secure, simple, and reusable.

You made it to the end of the article😍. Good luck with your interviews and programming journey!

Thank you…!❤️

--

--

Nipuni Premadasa
Nerd For Tech

Undergraduate at Faculty of Information Technology, University of Moratuwa | Former Trainee Software Engineer at Embla Software Innovation(PVT) Ltd., Sri Lanka