As the name suggests, Object-Oriented refers to the concept of organizing the software as a combination of different types of objects. Here, I have used a word “object”, so before going to OOPs in deep, let us explain what is this object.
Object can defined as any real-world entity that has state and behaviour. Lets take an example, A dog is an object because it has states like color, name, etc and behaviors like barking, eating, etc. Similarly, we can describe all many objects with their state and properties such as car, pen, bike, table etc.
Why OOPs …?
>> Object-Oriented programming (OOPs) is a programming paradigm where objects are used to interact with one another to design applications and computer programs.
>> OOPs makes easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
Lets learn about different properties of OOPs:
In our real life, have you even notice how we are receiving calls or sms on our smartphones? Most of the people don’t bother about these functionalities, they just need product should work perfectly. Thus, the smartphone shows only the essential things to the user and hides the internal functionalities.
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Lets take another example of a calculator. A simple calculator requires minimum two numeric inputs and one operator to perform any operation. When you press “=” button, it shows the result. Here, the functionality part (i.e., how the addition or subtraction results) is hidden from the user. The sample view to the end user is shown in the figure.
In our everyday life, I hope everyone have seen medicine capsule once, but do you know what’s there inside the capsule? There you will find drug or some smaller particles of medicine, which is safe inside the capsule. In the same way, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.
Encapsulation refers to the wrapping of code and data together in a single unit.
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
Suppose you have to create a class A that contains 10 methods. Now imagine if you have to create another class B that will contain all the properties of class A and some other properties. It will be hectic right to write all the methods again. Surely, you will try to find easiest approach to create class B. Here, we can use inheritance to reuse the code of class A.
Here, we inherit the properties of class A into another class B using “extends” keyword. Thus, class A is called as parent class or super class, and class B is called as child class or subclass.
Thus, we can define Inheritance in Java as a mechanism in which one object acquires all the properties and behaviors of a parent object.
Different types of inheritance in java:
In java programming, multiple and hybrid inheritance is supported through interface only (will discuss this later).
Polymorphism is derived from two words, Poly means many and morph means form. We can say, polymorphism is a process where one task is performed in different ways.
Lets take an example to explore this. Hope everyone has a memory of their KG class in schools. Suppose your teacher tells you to draw some geometric shapes. It can be possible that all have not drawn same shapes. Some shapes may be of triangle, rectangle, circle, square etc. Here, everyone has drawn shape but all shapes are not identical.