Class 10 - Encapsulation And Inheritance
Class 10 - Encapsulation And Inheritance
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism,
and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data
(methods)together as a single unit. In encapsulation, the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their current class. Therefore, it is also known
as data hiding.
To achieve encapsulation in Java :
Declare the variables of a class as private.
Benefits of Encapsulation :
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
The users of a class do not know how the class stores its data. A class can change the data type of a
field and users of the class do not need to change any of their code.
Encapsulation is ineffective in absence of access specifiers.
Access Specifiers/modifiers:
Access specifiers allow to decide how parts of the classes(variables, methods, constructors etc) can be
accessed by other classes in other parts of the program. There are four types of access specifiers:
1. private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
2. default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
3. protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. 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.
The scope of a variable refers to that part of the program in which the variable is accessible.
The variables used in the class can be of any of the following type :
Type of variables
Global
Defined outside functions and blocks.
(Note:-However in Java Global variable term is not used)
Can be used in all parts of the class.
Variables use default value if not initialized.
Inheritance:
Inheritance can be defined as the process where one class acquires the properties of another class. It
implements the feature of reusability. Inheritance enables you to create new classes that reuse, extend and
modify the behavior that is defined in other classes.
Derived/child/sub class: The class which inherits the state and behavior from base class.
Base/parent/Super class: The class from which the state and behavior is inherited to child class.
Types of Inheritance:
1. Single Inheritance: When a class is derived from one base class.
2. Multiple Inheritance: When a sub class is derived from multiple base classes.
3. Hierarchical Inheritance: When multiple sub classes are inherited from one base class.
4. Multilevel Inheritance: When a subclass is inherited from a class that itself is being inherited from
another class.
5. Hybrid Inheritance: When more than one type of inheritance is used together.