OOP Concepts

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:

  1. Single Inheritance: A child class is derived from a single parent class.
  2. 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.
  3. 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:

  1. 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.
  2. 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.
Here are some rules for polymorphism in Java:
  1. Final methods can not be overridden.
  2. Status method can not be overridden.
  3. Provide methods can not be overridden.
  4. The overriding method must have same return type as in the base class’s method.
Some key points:
  • 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.
References:
Book: Java, The Complete Reference
Other sources on the web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s