In this post, we are going to learn about the Object Oriented Programming (OOP) Concepts in Java. When talking about OOP, there are four main concepts.
Let’s discuss each of these concepts.
In general, we say that a child inherits behavior, qualities and appearance from their parents. That means it goes from the parent to the child by inheritance. In the same way in programming also, a child class can inherit from its’ parent class. It can inherits behavior(methods) and attributes(variables) contained in its’ parent class. Let’s move to an example.
We call the two classes as parent class and child class or the super class and the sub class. When we call a function belonging to the parent class by one of its’ sub classes, we use the keyword ‘super’. In the same way when creating a sub class in java, we use the keyword ‘extends’ to inherit from the super class.
- Can make the code reusable
- Makes the code readable
- Makes code more reliable
- Reduces code redundancy
When all the attributes and methods are bound into a single unit, we called that data is encapsulated.
Let’s consider a java class. It is an example for encapsulation. Within a class there are variables and also the methods related with various functionalities. All of them are wrapped inside it and that class then acts as a single functional unit. Here, these variables must be kept private and the methods must be implemented as public so that they can be accessed from outside of the class. These public methods are generally known as ‘getters and setters’. If we only use getters, we can make our code read-only. And if we use setters, we can make our code write-only.
In daily life, we can see many practical examples for this. Most of the people consider a medicinal capsule as the common example. Apart from that we’ll consider about a car as our example. Here, a car is made up of different parts. It contains tires, a steering wheel, seats, engine, fuel tank and so on. These parts are the examples for attributes. On the other hand a car has some behaviors. It can be driven by somebody, and also it can warn you if you haven’t put on your seat belts, and you can switch on the air conditioners inside it. These are like the methods inside a class.
- Helps to protect objects from unwanted access by users.
- Makes the code reusable
- Makes code understandable
- Testing code becomes easy
If we can perform a certain task in different ways, it is known as polymorphism.
There are two types of polymorphism as runtime polymorphism and compile time polymorphism. Runtime polymorphism is also known as dynamic polymorphism while compile time polymorphism is also known as static polymorphism. To acquire runtime polymorphism we use method overriding and to acquire compile time polymorphism we use method overloading.
This allows us to create more than one method with the same name. But they must have different method signatures. That means the parameters must be different.
In method overriding, parameters must be the same in each overridden method. This can be used in two classes which are having an inheritance between the two classes. By this we can do a specific implementation for a method which is already in the super class.
Below is a real world example for polymorphism. When we say speak. A dog sounds ‘Woof’, a cat sounds ‘Meow’ and a cow sounds ‘Moo’. All these different sounds belong to speaking.
- Makes code reusable
- Helps to reduce coupling
- Makes code readable
Hiding the implementation details from user and showing only the functionality is known as abstraction. This is achieved by using abstract classes and interfaces.
We use ‘abstract’ keyword to define an abstract class. This contains abstract methods inside it. An abstract method contains only the method signature. That means no method body implemented there. If a class has an abstract method, it definitely must be an abstract class. A 100% abstract class is called as an interface.
A mobile phone is a real world example where we can see abstraction. It does not show its’ internal hardware and other implementation details to the user. But the user can get use of its’ functionalities.
- Makes code reusable
- Avoids duplication of code
- Can perform changes to internal implementation without affecting the user
- Reduces complexity of design and implementation