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

Java Interfaces: Fundamentals & Usage

Module -4 (1)

Uploaded by

bkarri4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views13 pages

Java Interfaces: Fundamentals & Usage

Module -4 (1)

Uploaded by

bkarri4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Module 4

Interfaces: Fundamentals, Creating and Implementing an


Interface, Using Interface References,
Implementing Multiple Interfaces, Extending Interfaces,
Nested Interface.

1
Interfaces
• Abstraction in Java could be achieved with the following ways.
Interfaces
Abstract Methods and Class
• using interface, you can specify what a class must do, but not how it
does it.
• They are similar to classes, but they lack instance variables & their
methods are declared without any body. Interface variables are static
• Interface methods do not have a body - the body is provided by the
"implement" class
• Java does not permit multiple inheritance from classes, but permits
implementation of multiple interfaces.
Why use Java Interfaces
Why use Java Interfaces
Syntax:
interface <interface_name>{

methods // declare methods that abstract


//public
fields // declare constant fields
// by default. Public static final Interface fields are public,
static and final by default,
} and the methods are
public and abstract.
Java Interfaces

Prior to java 8, interface in java can only have abstract methods. All
the methods of interfaces are public & abstract by default.

Java 8 allows the interfaces to have default and static methods.

In Java 9, we can create private methods inside an interface. Interface


allows us to declare private methods that help to share common code
between non-abstract methods.
Why use Java Interfaces

Java Interface Example


Interfaces
Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces,


it is known as multiple inheritance.
Multiple inheritance in Java by interface
Multiple inheritance in Java by interface
Multiple inheritance is not supported through class in java, but it is possible by an
interface
Multiple inheritance in Java by interface

Java 8 Default Method in Interface

Output:
Nested Interface JAVA(inside interface)

 An interface can be declared a member of a class or another


interface. Such an interface is called a member interface or a
nested interface.
 A nested interface can be declared as public, private, or
protected.
 When a nested interface is used outside of its enclosing
scope, it must be qualified by the name of the class or
interface of which it is a member.
 Thus, outside of the class or interface in which a nested
interface is declared, its name must be fully qualified.
Nested Interface JAVA(inside interface)

You might also like