This is a common interview question, which seems to be simple and is also easy to be ignored. However without preparation, it can be difficult for interviewer to give a clear and impressive answer.
We try to obtain abstract view, model or structure of real life problem, and reduce its unnecessary details. With definition of properties of problems, including the data which are affected and the operations which are identified, the model abstracted from problems can be a standard solution to this type of problems. It is an efficient way since there are nebulous real-life problems that have similar properties.
Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible. (Data hiding: a language feature to restrict access to members of an object, reducing the negative effect due to dependencies. e.g. "protected", "private" feature in C++)
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class. And the purpose of inheritance is Code Reuse.
Polymorphism is the ability to present the same interface for differing underlying forms (data types). With polymorphism, each of these classes will have different underlying data. A point shape needs only two co-ordinates (assuming it's in a two-dimensional space of course). A circle needs a center and radius. A square or rectangle needs two co-ordinates for the top left and bottom right corners and (possibly) a rotation. An irregular polygon needs a series of lines.
I think most important feature of OOP is binding of data and its associated methods. This makes debugging somewhat easier since all the data modification is through methods. Data corruption are often most hard to debug problems. With OOP, especially languages which support data hiding or private methods, the methods that modify data are limited to a single class and so localization of errors/bugs makes debugging and maintenance somewhat easier.
Abstraction: To represent the essential feature without representing the background details is abstraction. It lets you focus on what the object does instead of how it does it.
Abstraction provides you a generalized view of your classes or objects by providing relevant information. Mostly we make abstract class to generalized a concept.
Ex: generalized file upload, where you can upload any kind of file like word pdf or any other.
Encapsulation: Prevents access to implementation details i.e. Wrapping up a data member and a method together into a single unit. example Mobile Phones.
Inheritance: Inheritance is a process of object reusability. When a class includes a property of another class it is Inheritance. Example parent-child relation.
Polymorphism: It mean one form, many uses. Using Many forms of a single object.
- Compile-time: compiler identifies which polymorphism form it has to take and execute at compile time. (Overloading).
- Run-time: compiler identifies which polymorphism form it has to take and execute at runtime but not at compile time. (Overriding).
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.