Forms of inheritance:
Interface,
Implementation
Week 3
Objectives:
• Explain the function of
interface class in java
• Understand the use of
interface class in java
• Demonstrate how interface
class works.
Interfaces in Java
• An Interface in Java programming
language is defined as an abstract
type used to specify the behavior
of a class.
• An interface in Java is a blueprint
of a class.
• A Java interface contains static
constants and abstract methods.
Interfaces in Java
• Like a class, an interface can have
methods and variables, but the methods
declared in an interface are by default
abstract (only method signature, no body).
• Interfaces specify what a class must do
and not how. It is the blueprint of the
class.
• An Interface is about capabilities like a
Player may be an interface and any class
implementing Player must be able to (or
must implement) move(). So it specifies a
set of methods that the class has to
implement.
• If a class implements an interface and
does not provide method bodies for all
functions specified in the interface, then
the class must be declared abstract.
How to Create a interface class…………
Why do we use an Interface?
• It is used to achieve total abstraction.
• Since java does not support multiple
inheritances in the case of class, by
using an interface it can achieve
multiple inheritances.
• It is also used to achieve loose
coupling.
• Interfaces are used to implement
abstraction. So the question arises why
use interfaces when we have abstract
classes?
The major differences between a class and an interface are:
Example of Interface
Demo Code………
Output
Interface Class
Main Class
Example of Interface
Demo Code………
Main Class Interfaces Class
Example of
Interface
Demo Code………
Demo Code………
References:
https://medium.com/@isaacjumba/overview-of-inheritance-interfaces-and-abstract-classes-in-
java-3fe22404baf8
https://www.geeksforgeeks.org/c-sharp-inheritance-in-interfaces/