0% found this document useful (0 votes)
15 views13 pages

Lesson 11 - OOPs Concepts in Java

Java

Uploaded by

egankian797
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
15 views13 pages

Lesson 11 - OOPs Concepts in Java

Java

Uploaded by

egankian797
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

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…

Key points about tightly encapsulated class:


If the parent class is not tightly encapsulated, no child class is tightly
encapsulated because the parent class’s non-private data members by default
are available to every child class.
Thus, we can say that data hiding, encapsulation, and tightly encapsulated
class concepts are used for security purposes.
Encapsulation ealtime Example…

Check through the materials for more examples……


Key Points to Remember:
1. Encapsulation is one of the four principles of OOPs concepts and the other
three are Abstraction, Inheritance, and Polymorphism.
2. OOPs concepts in Java program are implemented through four principles.
They are:
Encapsulation (protecting data of a class from being accessed by members of
another class).
Abstraction in Java | Abstract Class, Example
A process of hiding complex internal implementation details from the user

and providing only necessary functionality to the users.

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.

Let’s take some realtime examples to understand the concept of java


abstraction.
Abstraction in Java | Abstract Class, Example

One only need to call the specific classes or methods to implement


specific program logic, but you don’t know how these classes or
methods function. This concept is known as abstraction in java.

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

When to use Abstract class in Java?


 when we need to share the same method to all non-abstract subclasses with their own
specific implementations.
 the common member of the abstract class can also be shared by the subclasses.
 Thus, abstract class is useful to make the program more flexible and understandable.
Abstraction in Java | Abstract Class, Example
Abstract Method in Java
 A method that is declared with abstract modifier in an abstract class and has no
implementation (means no body) is called abstract method in java. It does not contain
any body.
 Abstract method has simply a signature declaration followed by a semicolon. It has the
following general form as given below:

 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.

You might also like