Java Interface
Interface Data Types in Java
• An interface is similar to a class however the
only difference is that its methods are
abstract by default i.e. they do not have body.
• It is also called a fully abstract class.
• It cannot be instantiated just like the abstract
class.
Why use Java interface?
• It is used to achieve abstraction.
• By interface, we can support the functionality
of multiple inheritance
How to declare an interface?
• An interface is
declared by using the
interface keyword.
• A class that
implements an
interface must
implement all the
methods declared in
the interface.
Static Keyword in Java
• Definition:
– Belongs to the class rather than an instance of the
class.
– Static variables are shared /common by different
objects.
• Access:
– Can be accessed without creating an object.
• Uses:
– Static variables → Shared across all objects.
– Static methods → Can be called using class name.
– Static blocks → Used to initialize static data.
Static Example
class Student {
static int id; // static variable (shared by public class TestStaticWrong {
all objects)
public static void main(String[] args) {
String name;
Student s1 = new Student(101,
"Aman");
Student(int i, String n) {
Student s2 = new Student(102,
id = i; // ❌ overwrites id for all
"Riya");
objects
name = n;
} [Link](); // ID: 102 Name:
Aman
void display() { [Link](); // ID: 102 Name: Riya
[Link]("ID: " + id + " // ❌ Both show same ID (wrong
Name: " + name); behavior)
} }
} }
Final Keyword in Java
• Definition:
– Used to declare constants, prevent method
overriding, or inheritance.
• Uses:
– Final variable → Value cannot be changed
(constant).
– Final method → Cannot be overridden by
subclasses.
– Final class → Cannot be inherited.
How to declare an interface?
• All the methods in an interface are declared
with the empty body, and all the fields are
public, static and final by default.
Relationship between classes and interfaces
Printable interface
has only one method,
and its
implementation is
provided in the A6
class.
Using Interface by
third user
Interface : BANK
RateOfIntrest()
Class : SBI Class : PNB
RateOfIntrest()= RateOfIntrest()=
9.15 9.7
Interface inheritance
Interface : Animal
AnimalSound()
Sleep()
Class : Pig
AnimalSound()
= The pig says:
wee wee
Sleep()= Zzz
Multiple inheritance in Java by interface
Interface : FirstInterface Interface : SecondInterface
myMethod() myOtherMethod()
Class : DemoClass
myMethod() = Some text..
myOtherMethod()= Some
other text.
Practice Problem
• Create an interface named area, with two
methods names dimensions which accepts 2
arguments length and breath from user and
area that calculates area of rectangle.
import [Link];
interface area
{
public void dimensions();
public void area();
}
public class Interface_Implementation implements area
{
int length,breadth,area;
public void dimensions()
{
Scanner s=new Scanner([Link]);
[Link]("Enter length:");
length=[Link]();
[Link]("Enter breadth:");
breadth=[Link]();
}
public void area()
{
area=length*breadth;
[Link]("Area:"+area);
}
public static void main(String[] args)
{
Interface_Implementation obj=new Interface_Implementation();
[Link]();
[Link]();
}
}