Mark As Completed Discussion

11. What is method or operator overloading?

The method overloading is a feature of object-oriented programming in which multiple methods can have the same method name with different arguments. The call to the method is resolved based on the arguments.

Operator overloading means that depending on the arguments passed, the operators’ behavior can be changed. However, it works only for user-defined types.

12. What do you know about encapsulation?

Encapsulation is an important feature of object-oriented programming which allows the binding of the data and the logic together in a single entity. It also allows the hiding of data, so it is also known as a combination of data-hiding and abstraction.

Overloading, Encapsulation and Abstraction
Encapsulation can be achieved by performing two steps:

  • Declaring the variables of a class as private
  • Providing public setter and getter methods to modify and view the variables' values

13. What is an abstraction?

Abstraction shows only the necessary details to the client of an object. This means that it shows only required details for an object, not the inner constructors of an object.

For example, if you turn on the TV, you do not need to know the inner mechanism needed to do that, so whatever is needed to turn it on will be shown using an abstract class.

You can achieve abstraction in two ways:

  1. Using Abstract Class Abstract Class is a class which is declared with an abstract keyword and cannot be instantiated. Few pointers to create an abstract class:
  • It can contain abstract and non-abstract methods
  • It can contain constructors and static methods as well
  • It can contain final methods which force the subclass not to change the body of the method

You cannot create an instance of an abstract class since it lacks implementation logic in its methods. You first need to create a subclass that implements all the methods before an object can be initialized.

1class MyAbstractClass {
2  abstractMethod() {
3    throw new Error("This method is abstract and must be overridden");
4  }
5  
6  display() {
7    console.log("method");
8  }
9}
  1. Using Interface An interface is a blueprint of a class that contains static constants and abstract methods. It represents the IS-A relation. You need to implement an interface to use its methods or constants.
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment