2. WHAT IS ENCAPSULATION???
Encapsulation is one of the four fundamental OOP concepts. Encapsulation in
Java is a process of wrapping variables and 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.
3. WORKING PROCESS
To achieve encapsulation in Java−
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the
variables values.
BENEFITS
Two major 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..
4. The public set() and get() methods are the
access points of the instance variables of the
EncapTest class. These methods are referred
as getters and setters. Therefore, any class
that wants to access the variables of the
EncapTest class have to access them
through these getters and setters.
Here we can see that , we used to setter
method to get the name and age. As a result
the name and age has been set to the
private variables of EncapTest.
We used getter method to get the name and
age that we had set and print it as we want.
6. WHAT IS ABSTRACT CLASS???
A class which is declared as abstract is known as an Abstract
Class. It can have abstract and non-abstract methods. It needs
to be extended and its method implemented. It cannot be
instantiated.
7. BENEFITS
• It helps in generalization of its methods when a class extends an Abstract class.
• Helps in code reusability.
• Abstract class also helps in defining the subclass features efficiently.
HOW TO WRITE ABSTRACT CLASS AND ITS
METHODS-
abstract class A{}
abstract void printMethod()
8. In this example, Shape is the abstract class,
and its implementation is provided by the
Rectangle and Circle classes.
But the implementation class and method
is hidden to the end user.
If we create the Rectangle class and use
the draw() method then Rectangle class
will be invoked.
Same will happen to Circle class.
So when we create an object of rectangle
or circle in the main class and call the
draw() method the desired output will
show.
Output:
drawing circle
10. WHAT IS INTERFACE???
An Interface in Java programming is defined as an abstract type used to
specify the behavior of a class.
A Java interface contains static constants and abstract methods.
A class can implement multiple interfaces.
In Java, interfaces are declared using the interface keyword.
All methods in the interface are implicitly public and abstract.
To use an interface in your class, append the keyword "implements"
after your class name followed by the interface name.
11. BENEFITS
• Through interfaces we can implement multiple inheritance in java.
• Interfaces function to break up the complex designs and clear the dependencies
between objects.
• Interfaces makes your application loosely coupled.
• Without bothering about the implementation part, we can achieve the security of
implementation.
• It is used to achieve full abstraction.
12. In this multiple inheritance example,
there are two interfaces Printable and
Showable both having a single method of
print().
Multiple inheritance is not supported in
the case of class because of ambiguity.
However, it is supported in case of an
interface because there is no ambiguity.
It is because its implementation is
provided by the implementation class.
So if we create another class and
implement the two interface classes and
create an object we will be able to done
the implementation.
Output:
Hello
13. THANK YOU
M D. A N I S U R R A H M A N
1 9 2 - 1 5 - 2 8 2 5
P C - B