Four Main Object Oriented Programming Concepts of Java
Object-oriented programming generally referred to as OOPS is the backbone of java as java being a completely object-oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are listed below. These concepts aim to implement real-world entities in programs.
Abstraction is a process of hiding implementation details and exposes only the functionality to the user. In abstraction, we deal with ideas and not events. This means the user will only know “what it does” rather than “how it does”.
There are two ways to achieve abstraction in Java
- Abstract class (0 to 100%)
- Interface (100%)
Real-Life Example: A driver will focus on the car functionality (Start/Stop -> Accelerate/ Break), he/she does not bather about how the Accelerate/ brake mechanism works internally. And this is how the abstraction works.
Certain key points should be remembered regarding this pillar of OOPS as follows:
- The class should be abstract if a class has one or many abstract methods
- An abstract class can have constructors, concrete methods, static method, and final method
- Abstract class can’t be instantiated directly with the new operator. It can be possible as shown in pre tag below:
A b = new B();
- The child class should override all the abstract methods of parent else the child class should be declared with abstract keyword
Pillar 2: Encapsulation
Encapsulation is the process of wrapping code and data together into a single unit.
A capsule which is mixed of several medicines. The medicines are hidden data to the end user.
In order to achieve encapsulation in java follow certain steps as proposed below:
- Declare the variables as private
- Declare the setters and getters to set and get the variable values
Note: There are few disadvantages of encapsulation in java as follows:
- Control Over Data: We can write the logic in the setter method to not store the negative values for an Integer. So by this way we can control the data.
- Data Hiding: The data members are private so other class can’t access the data members.
- Easy to test: Unit testing is easy for encapsulated classes
Pillar 3: Inheritance
Inheritance is the process of one class inheriting properties and methods from another class in Java. Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends keyword.
The planet Earth and Mars inherits the super class Solar System and Solar system inherits the Milky Way Galaxy. So Milky Way Galaxy is the top super class for Class Solar System, Earth and Mars.
Let us do discuss the usage of inheritance in java applications with a generic example before proposing the code. So consider an example extending the Exception class to create an application-specific Exception class that contains more information like error codes. For example NullPointerException.
There are 5 different types of inheritance in java as follows:
- Single Inheritance: Class B inherits Class B using extends keyword
- Multilevel Inheritance: Class C inherits class B and B inherits class A using extends keyword
- Hierarchy Inheritance: Class B and C inherits class A in hierarchy order using extends keyword
- Multiple Inheritance: Class C inherits Class A and B. Here A and B both are superclass and C is only one child class. Java is not supporting Multiple Inheritance, but we can implement using Interfaces.
- Hybrid Inheritance: Class D inherits class B and class C. Class B and C inherits A. Here same again Class D inherits two superclass, so Java is not supporting Hybrid Inheritance as well.
Pillar 4: Polymorphism in java
Polymorphism is the ability to perform many things in many ways. The word Polymorphism is from two different Greek words- poly and morphs. “Poly” means many, and “Morphs” means forms. So polymorphism means many forms. The polymorphism can be present in the case of inheritance also. The functions behave differently based on the actual implementation.
A delivery person delivers items to the user. If it’s a postman he will deliver the letters. If it’s a food delivery boy he will deliver the foods to the user. Like this polymorphism implemented different ways for the delivery function.
There are two types of polymorphism as listed below:
- Static or Compile-time Polymorphism
- Dynamic or Run-time Polymorphism
Static or Compile-time Polymorphism when the compiler is able to determine the actual function, it’s called compile-time polymorphism. Compile-time polymorphism can be achieved by method overloading in java. When different functions in a class have the same name but different signatures, it’s called method overloading. A method signature contains the name and method arguments. So, overloaded methods have different arguments. The arguments might differ in the numbers or the type of arguments.
Example 1: Static Polymorphism
Dynamic or Run-time Polymorphism occurs when the compiler is not able to determine whether it’s superclass method or sub-class method it’s called run-time polymorphism. The run-time polymorphism is achieved by method overriding. When the superclass method is overridden in the subclass, it’s called method overriding.
Example 2: Dynamic Polymorphism
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.