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

Interfaces in Java

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

Interfaces in Java

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

Interfaces in Java:

An interface in Java is a blueprint of a class. It has static constants and abstract


methods.
Interface is a collection of Variables and abstract methods up to Java7 version. 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?


 It is used to achieve abstraction.
 By interface, we can support the functionality of multiple inheritance.
 It can be used to achieve loose coupling.

Coding rules of Interface:

Rule-1 : we use 'interface' keyword to declare interfaces


syntax:
interface Interface-name

{
//members
}

Rule-2 : The members which are declared within the interface are automatically 'public'

Note:
The members which are declared within the class without any access modifier
are automatically 'default'.

Rule-3 : The interfaces can be declared with both Primitive Data-Type variables and Non-
Primitive Data-Type Variables.
Rule-4 : The variables declared in interfaces are automatically static and final variables
Note:
static variables in interfaces will get the memory within the interface while
interface loading and can be accessed with Interface-name.
final variables must be initialized with values and once initialized cannot be
modified.(final variables are secured variables)

o How can we declare Non-Static variables in Interfaces?


There is no concept of declaring Non-Static variables in Interfaces, because the
variables are automatically static variables.

Rule-5 : The methods which are declared within the interface are automatically Non-Static
abstract methods.
Note:
There is no concept of static abstract methods in interfaces.

Rule-6 : we cannot instantiate interfaces, because the interfaces are abstract components in
Java.(abstract components means physical existence not available)

Rule-7 : These interfaces are implemented to classes through 'implements' keyword and the
classes are known as implementation classes.

Rule-8 : These implementation classes must construct the body for abstract methods of
Interfaces

Rule-9 : The interface can be declared with any number of abstract methods.

Rule-10 : The implementation class must construct body for all abstract methods of Interface.
Rule-11 : Implementation class can be declared with methods other than implemented
methods.

Rule-12 : The interface can be implemented to any number of implementation classes


without any restriction

Rule-13 : There is no concept of Blocks and Constructors in interfaces

Rule-14 : Interface can extract the features from another interface using 'extends' keyword

You might also like