Before we dive into Java OOP concepts, let’s first see what OOP programming is.
What is Object-oriented programming?
Object-oriented programming is a methodology or paradigm for writing programs using classes and objects. Contains many concepts such as inheritance, data binding, polymorphism, etc.
The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.
The first object-oriented programming languages were Simula and Smalltalk.
Object-oriented programming system
It simplifies software development and maintenance by providing the following concepts:
Class in Java OOP
A class is a pattern or contract that defines the data fields and methods that all class objects will possess. It describes objects with similar characteristics and behaviour and represents a general description of an object.
Class does not really exist. Only objects exist in reality. If we think of a class as a template for creating its own objects, then entity objects are created based on that template.
Object in Java OOP
An object is a specific instance of a particular class. It has its unique identity and properties, such as condition and behaviour. The state of an object (properties, attributes) represents data fields with their current values. The behaviour of an object (actions, operations) is defined by methods.
The identity of an object refers to some of its properties by which it differs from any other object. Each object is unique because any two or more objects, regardless of their similarities, have an individual identity.
We can create as many objects (class instances) as we want. Creating an instance of a class is called instantiation.
Inheritance in Java OOP
When an object acquires all the characteristics and behaviours of a parent object, it is called inheritance. It provides code reusability and is used to achieve polymorphism at execution time.
Learn more about inheritance.
Polymorphism in Java OOP
This is known as polymorphism when one task can be performed differently. In Java, we use method overloading and method overriding to achieve polymorphism. It represents a concept when a particular method behaves differently in different objects.
Learn more about polymorphism.
Encapsulation in Java OOP
The process of combining and protecting attributes and methods is called Encapsulation. Fields are not available directly through objects, so we need to have getter and setter methods.
Class attributes are stored privately; we can retrieve them only through public getter methods.
Using public setter methods, we can change the state of a class.
Learn more about encapsulation.
Abstraction in Java OOP
Abstraction is hiding implementation details and showing the user only the functionality. In other words, only important things are shown to the user, and internal details remain hidden. Abstraction allows us to focus on what the object is doing rather than how it is doing.
Learn more about abstraction.
Advantages of OOP over procedural-oriented programming languages
- OOP makes development and maintenance easier, while in procedurally-oriented programming languages, this is not easy to achieve if the code grows as the project’s scope grows.
- OOP provides data hiding while global data can be accessed from anywhere in procedural-oriented programming languages.
- OOP provides much more efficient opportunities to simulate real-world events. As a result, we can solve some of the real problems if we use the OOP language.