0% found this document useful (0 votes)
3 views

Class 10 - Encapsulation And Inheritance

The document discusses encapsulation and inheritance, two fundamental concepts of object-oriented programming (OOP) in Java. It explains encapsulation as a mechanism for data hiding through access specifiers, detailing their types and benefits, while also outlining the different types of variables and the distinction between static and final keywords. Additionally, it covers inheritance, its types, and the relationship between base and derived classes.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Class 10 - Encapsulation And Inheritance

The document discusses encapsulation and inheritance, two fundamental concepts of object-oriented programming (OOP) in Java. It explains encapsulation as a mechanism for data hiding through access specifiers, detailing their types and benefits, while also outlining the different types of variables and the distinction between static and final keywords. Additionally, it covers inheritance, its types, and the relationship between base and derived classes.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Ch-13 : 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

Local Argument / Parameter


Defined within functions and Defined in the function heading.
blocks. Can be used in that method only.
Can be used in that method or Variable must be initialized.
block only.
Variable must be initialized.

Instance or non-static Class or static


Defined outside functions and blocks. Defined outside functions and blocks.
Can be used in all parts of the class. Can be used in all parts of the class.
Variables use default value if not initialized. Variables use default value if not initialized.
Used without static keyword hence non-static. Used with static keyword hence called static also.
Values are unique to each instance of class. Values are same for all instances of class.
Need to create object to access non-static. Static can be accessed using class.

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.

Difference in static and final keyword.


The main difference between a static and final keyword is that static is keyword and is used to define the
class member that can be used independently of any object of that class.
Whereas Final keyword is used to declare, a constant variable, a method which can not be overridden and a
class that can not be inherited.

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.

You might also like