0% found this document useful (0 votes)
4 views30 pages

04 Java Abstraction

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)
4 views30 pages

04 Java Abstraction

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/ 30

JAVA ENTERPRISE EDITION

PROGRAMMING
SUBJECT DESCRIPTION: JAVA ENTERPRISE EDITION PROGRAMMING
SUBJECT CODE: CS220

ENGR. RUELLA YARES MAGTAAS, CPE


ABSTRACT CLASS IN JAVA
A class which is declared with the abstract keyword
is known as an abstract class in Java. It can have
abstract and non-abstract methods (method with
the body).
Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only


functionality to the user.
Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstraction

There are two ways to achieve abstraction in java


1.Abstract class (0 to 100%)
2.Interface (100%)
Abstract class in Java

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.
Points to Remember

•An abstract class must be declared with an


abstract keyword.
•It can have abstract and non-abstract methods.
•It cannot be instantiated.
•It can have constructors and static methods also.
•It can have final methods which will force the
subclass not to change the body of the method.

Example of abstract class


Abstract Method in Java

A method which is declared as abstract and does not have implementation is known
as an abstract method.

Example of abstract method


Example of Abstract class that has an abstract
method
In this example, Bike is an abstract class that contains only one abstract method
run. Its implementation is provided by the Honda class.
Understanding the real scenario of Abstract class

In this example, Shape is the abstract class, and its implementation is provided by
the Rectangle and Circle classes.
Mostly, we don't know about the implementation class (which is hidden to the end
user), and an object of the implementation class is provided by the factory
method.

A factory method is a method that returns the instance of the class.

In this example, if you create the instance of Rectangle class, draw() method of
Rectangle class will be invoked.
Another example of Abstract class in java
Abstract class having constructor, data member
and methods
An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.

If there is an abstract method in a class,


that class must be abstract.
Interface in Java
• An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
• The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritance in Java.
• In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
• Since Java 8, we can have default and static methods in an interface.
• Since Java 9, we can have private methods in an interface.
Why use Java interface?

There are mainly three reasons to use interface. They are given below.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
How to declare an interface?
An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the
fields are public, static and final by default. A class that implements an interface
must implement all the methods declared in the interface.
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.
Java Interface Example
In this example, the Printable interface has only one method, and its
implementation is provided in the A6 class.
Java Interface Example: Drawable
In this example, the Drawable interface has only one method. Its implementation is
provided by Rectangle and Circle classes. In a real scenario, an interface is defined
by someone else, but its implementation is provided by different implementation
providers. Moreover, it is used by someone else. The implementation part is hidden
by the user who uses the interface.
Java Interface Example: Bank
Let's see another example of java interface which provides the implementation of
Bank interface.
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Interface inheritance
A class implements an interface, but one interface extends another interface.
Java 8 Default Method in Interface

Since Java 8, we can have method body in interface. But we need to make it default
method. Let's see an example:
Java 8 Static Method in Interface

Since Java 8, we can have static method in interface. Let's see an example:
Nested Interface in Java
Note: An interface can have another interface which is known as a nested interface.
We will learn it in detail in the nested classes chapter. For example:
Difference between abstract class and interface
Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods. Abstract class and interface both can't be
instantiated.
But there are many differences between abstract class and interface that are given
below.
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).
Example of abstract class and interface in Java

Let's see a simple example where we are using interface and abstract class both.

You might also like