Java OOP concepts with real world examples

lakshani dharmarathna
2 min readNov 22, 2023
4 main OOP concepts

OOP standard for Object Oriented Programming. There are 4 main OOP concepts available.

  1. Inheritance

inheritance mean inherit attributes and method from one class to another class or In other words, one object acquire all the properties and behaviours from their parent object.

example: In real world we can get ‘Animal’ as a parent (super)class and from this we can create ‘Cat’, ‘Dog’, ‘Bird’ child (sub)classes. Each subclass inherit the common methods like ‘eat’, ‘sleep’, ‘makeSound’ and override the methods. Each subclass then adds its own specific attributes and behaviour, demonstrating how inheritance allows for code reuse and specialization in an object-oriented design.

2. Polymorphism

polymorphism mean one task is performed in many ways. There are two types of polymorphism available. Those are static polymorphism and dynamic polymorphism. method overloading belong to a static polymorphism and method overridden belong to a dynamic polymorphism.

example: In the company there are different types of employees available. Each type of employee have a different method of calculating salary, bonus or benefits. Therefore, we can use polymorphism to implement common method for calculating salary across all the employee type even though the calculating logic varies based on the role of the employee.

3. Abstraction

abstraction mean process of hiding certain details (implementation details) and showing only the essential information (features) to the outside of the world.

example: In real world smart phone is the best example for abstraction. because users interact with the apps and features on their smart phone, without needing to understand the hardware and software complexity. smart phone abstract the complexity of operating system, hardware components, protocol and showing only the user friendly interface.

4. Encapsulation

encapsulation mean binding data and methods together into a single unit that operate within the class and making data inaccessible to the outside world directly.

example: In real world ‘Bank Account’ class encapsulate the data like ‘account number’ and ‘account balance’ along with methods like ‘deposit’ and ‘withdraw’. In that ‘Bank Account’, account balance is private and can only be access or modified through designated method.

so those are the 4 main OOP concepts with examples in brief. Hope you guys enjoy that article and get knowledge. Thank you for reading my article.

Follow to get notified when I post more articles. I’m excited to continue sharing valuable insights with amazing readers like you.

--

--