0% found this document useful (0 votes)
2 views

Week3_Forms of inheritance (1)

The document explains the concept of interfaces in Java, defining them as abstract types that specify the behavior of classes through abstract methods and constants. It highlights the importance of interfaces for achieving total abstraction, multiple inheritance, and loose coupling in Java programming. Additionally, the document outlines the differences between classes and interfaces and provides examples and references for further understanding.

Uploaded by

michael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week3_Forms of inheritance (1)

The document explains the concept of interfaces in Java, defining them as abstract types that specify the behavior of classes through abstract methods and constants. It highlights the importance of interfaces for achieving total abstraction, multiple inheritance, and loose coupling in Java programming. Additionally, the document outlines the differences between classes and interfaces and provides examples and references for further understanding.

Uploaded by

michael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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/

You might also like