Lesson 11 - OOPs Concepts in Java
Lesson 11 - OOPs Concepts in Java
and
Real-time Example
Encapsulation ealtime Example…
Tightly Encapsulated Class in Java
If each variable is declared as private in the class, it is called tightly
encapsulated class in Java.
No need to check whether the class contains getter and setter method
or not and whether these methods are declared as public or not.
Example1;
Encapsulation Realtime Example…
Example 2; Q. Which of the following classes are tightly encapsulated?
Ans: Class A and Class C are tightly encapsulated classes. Class B is not a tightly encapsulated
class because of non-private variable y. Anyone can access it directly from outside the class.
Encapsulation ealtime Example…
Example 3; Q. Which of the following classes are tightly encapsulated?
Ans: None of these is tightly encapsulated class because class Q is the child class of P.
Non-private data members of class P by default are available in subclass Q.
Similarly, class R is the subclass of Q. All the non-private data members of class Q by
Encapsulation ealtime Example…
Abstraction in Java is a technique by which we can hide the data that is not
required to a user.
It hides all unwanted data so that users can work only with the required data.
It removes all non-essential things and shows only important things to users.
2. A car owner knows how to drive it. He knows about various components of car and how to use them.
For example, a car owner knows that the accelerator pedal is used to increase the speed of car, and pressing
the brake pedal stops it.
To perform these simple actions, you only need to know how to use these components but not need to know
how they function.
3. When you need to send SMS from your mobile, you only type the text and send the message. But you don’t
know the internal processing of the message delivery.
Abstraction in Java | Abstract Class, Example
How to achieve Abstraction in Java?
There are two ways to achieve or implement abstraction in java program. They are as follows:
Abstract class (0 to 100%)
Interface (100%)
Abstract Class in Java
An abstract class in Java is a class, which is declared with an abstract keyword. It is just like a normal class but
has two differences.
1. We cannot create an object of this class. Only objects of its non-abstract (or concrete) sub-classes can be
created.
2. It can have zero or more abstract methods which are not allowed in a non-abstract class (concrete class).
Classloader class is a good example of an abstract class that does not have any abstract methods.
Abstraction in Java | Abstract Class, Example
Java Abstract class makes programming
More flexible by providing scopes to write abstract method in subclasses of the abstract
class.
Key points:
1. Abstract is a non-access modifier in java which is applicable for classes, interfaces,
methods, and inner classes. It represents an incomplete class that depends on subclasses
for its implementation. Creating subclass is compulsory for abstract class.
2. A non-abstract class is sometimes called a concrete class.
3. An abstract concept is not applicable to variables.
Abstraction in Java | Abstract Class, Example
Since the abstract method does not contain any body. Therefore, it is also known as
incomplete method in java.
Abstraction in Java | Abstract Class, Example
Abstract Method in Java…..
Key point:
A concrete method is a method which has always the body. It is also called a
complete method in java.
When to use Abstract method in Java?
1. An abstract method can be used when the same method has to perform
different tasks depending on the object calling it.
2. A method can be used as abstract when you need to be overridden in its non-
abstract subclasses.