Polymorphism and Encapsulation

Polymorphism and Encapsulation

In the last blog, we covered Inheritance and Abstraction so make sure you have read that before understanding the following content.

Polymorphism is a concept in OOP that refers to the ability of a function or method to operate on multiple data types. This means it allows for objects of different classes to be treated as an object of a common base class. In general, the word "polymorphism" means having many forms.

So in simple terms, polymorphism is a way to use the same function or method for different types of objects, which makes the code more flexible and reusable. An example of this would be a function that takes an object as an input and performs some operation on that object. If the function is written to operate on objects of a base class, it can be used to operate on objects of any class that inherits from that base class. This allows for code reuse and flexibility.

Types of Polymorphism

There are main two types of Polymorphism

  • Compile Time Polymorphism

    This is also known as overloading. Compile-time polymorphism is when we create multiple functions with the same name but with different parameter lists in the same class. Just like constructor overloading, which we understood in the last blog, the only difference is constructor is called automatically when the object is instantiated and the methods need to call.

  • Run Time Polymorship

    This is also known as overriding. Run-time polymorphism is when a subclass provides a specific implementation of a function or method that is already provided by its superclass.

So polymorphism is a powerful feature of OOPs that allows for code flexibility and reusability.

Encapsulation

Encapsulation in OOPs refers to the process of hiding the internal implementation of an object from the other class. By hiding the internal details of that object, the implementation of that object can be changed easily without affecting the other objects.

Encapsulation allows for the creation of more reliable and secure code. Hiding the implementation of an object from other classes makes it difficult for other classes to access and alter the state of that object, which is very helpful in preventing bugs and making code less vulnerable.

Encapsulation is achieved through the use of access modifiers. Access modifiers in Java are "public", "private", "protected" and "default".

  • private:

    This modifier can be used to control the accessibility of code saving it from being vulnerable to other parts of the code. For example, a variable with the "private" access modifier can only be accessed from within its class.

  • public:

    The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

  • protected:

    The protected access modifier can be accessed within the package and also from outside the package through the child class.

  • Default:

    The default access modifier can be used to only give access within the package. we cannot access it from outside the package. Note, If we do not specify any access modifier then it will be considered default access.

In summary, encapsulation is a concept in object-oriented programming that refers to the process of hiding the internal implementation of an object from the outside class. Encapsulation provides greater flexibility and code reuse and also helps to make the system more reliable and secure.

Thank you for reading. Hope you understood the concepts.

Did you find this article valuable?

Support Abhishek Madankar by becoming a sponsor. Any amount is appreciated!