Who should read it: It is for you if you are looking for an overview of this topic for a project, to conduct/appear in an interview, or in general. As we learn more, we will update this article.
In Java, below are OOP concepts:
1. Abstraction: means hiding the details from the implementation. For example, we do not know details behind the gears of the car.
2. Encapsulation: Encapsulation is the concept to wrap the data under a single unit.
3. Inheritance: Inheritance is the concept to allow an object to inherit properties or behaviors of the parent class into a child class. It creates a parent-child relationship between classes. This concept provides code re-usability and maintainability. Below are types of inheritances:
- Single Inheritance: A child class is derived from a single parent class.
- Multiple Inheritance: A child class is derived from multiple parent classes. Note: In Java, multiple inheritance was not possible till JDK 1.7. It\’s possible from JDK 1.8 onwards, via the default methods in interfaces. Multiple inheritance via use of interfaces is possible in Java.
- Hybrid Inheritance: It is a combination of multiple inheritances. For example, a class A extends another class B and it also implements an interface.
4. Polymorphism: Polymorphism simply means many forms. It is a concept to declare multiple functions with the same name. It is also referred as one name with many forms. For example, ‘+’ operator sums two numbers or concatenates two strings.
There are two types of polymorphism in Java:
- Overloading: Overriding is related to compile-time polymorphism. It is also referred as a static binding or early binding. For example, a Java class can have multiple sum() functions with varied number of parameters.
- Overriding: Overloading is related to run-time polymorphism. It is also referred as a dynamic binding or method overloading. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime. Overriding is the feature that allows a subclass to provide a specific implementation of a method which is already into the super class. It is the type of object being referred to, not the type of reference variable.
- Final methods can not be overridden.
- Status method can not be overridden.
- Provide methods can not be overridden.
- The overriding method must have same return type as in the base class’s method.
- Constructors are not members of the class. So, they are not inherited. Constructor of a class can be invoked from another sub-class’s constructor.