- All object-oriented programming languages provide the following mechanism that helps you implement the object-oriented model.
- Encapsulation:
- Encapsulation is the mechanism that binds code and data together and keeps both safe from outside interference and misuse.
- It works as a protective wrapper that keeps your code and data safe form being accessed by other code defined outside the wrapper.
- We can access the code and data inside the wrapper through a well-defined interface only (Class methods).
- Class is the fundamental unit that provides encapsulation of the code.
- A Data defined inside the class are referred to as member variables or instance variables.
- The code that operates on that data is referred to as member methods or just method.
- Each method or variable in a class may be marked private or public.
- The public interface of a class can be accessed from everywhere
- The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that is not a member of the class cannot access a private method or variable.
- Inheritance:
- Inheritance is a process of deriving new class based on the existing class.
- Through the Inheritance, object of one class acquires the properties of another class.
- It provides re usability of the code because we can use code from the existing classes by adding extra features without modifying it
- Common properties can be shared among multiple classes with the help of inheritance
- It supports the concept of hierarchical classification.
- The hierarchy moves from generalization to specific For example, the bird robin is a part of the class flying Bird, which is again a part of the class Bird.
- The new class have the combined feature of both the classes.
- Each subclass defines only those features that are unique to it.
- Derived class is known as ‘sub class’ or ‘child class’ and main class is known as ‘super class’ or ‘base class’.
- Polymorphism:
- Polymorphism means “One name, Multiple forms”
- It provides the ability to take more than one form.
- A single function name can be used to handle different number and different types of arguments.
- It allows different internal structures to share the same name.