Interface in Java
"Define the contract, let the classes do the work!“
-Varshini Priya.G [271116]
CSE-B II YEAR
INTERFACE:
• An interface in Java is a blueprint for a class. It is a
collection of abstract methods (methods without a body)
that defines a set of rules or behavior that a class must
follow by implementing those methods.
• Defined using the interface keyword.
• A class implements an interface using the implements
keyword.
• A class can implement multiple interfaces, which allows
multiple inheritance of behavior.
IN SIMPLE:
Imagine an interface called RemoteControl
interface RemoteControl {
void turnOn(); // No body – just saying it must exist
void turnOff();
}
class TV implements RemoteControl {
public void turnOn() {
System.out.println("TV is ON");
}
public void turnOff() {
System.out.println("TV is OFF");
}
}
**Inheritence should not be confused with interface **
Inheritence is between two classes , the child class extract its
behaviour and state from the parent class . The parent can
also work alone without child class.
But in case of interface ,its not between two classes but
between interface and class. The interface cannot act without
the class .
Interface = A remote control with buttons, but no battery or
electronics inside.
Class = Puts the electronics inside and makes the buttons
work.
Interface can not work without class the same way the remote
A FUN EXAMPLE:
interface SubwaySurfers {
void run();
void collectCoins();
}
// Jake.java - A character class
class Jake implements SubwaySurfers {
public void run() {
System.out.println("Jake is running fast!");
}
public void collectCoins() {
System.out.println("Jake collects gold coins!");
}
}
// Tricky.java - Another character class
class Tricky implements SubwaySurfers {
public void run() {
System.out.println("Tricky is sprinting down the track!");
}
public void collectCoins() {
System.out.println("Tricky grabs the coins skillfully!");
} }
public class Main {
public static void main(String[] args) {
SubwaySurfers jake = new Jake();
SubwaySurfers tricky = new Tricky();
System.out.println("== Jake's Actions ==");
jake.run();
jake.collectCoins();
System.out.println("n== Tricky's Actions ==");
tricky.run();
tricky.collectCoins();
} }
another difference between inheritence and interface:
In inheritence the child class can have only one parent class but
in interface the class can have many interface .
EXAMPLE: (coding )
interface Runner {
void run();
}
interface Jumper {
void jump();
}
class Athlete implements Runner, Jumper {
public void run() {
System.out.println("Athlete is running");
}
public void jump() {
System.out.println("Athlete is jumping");
}
public static void main(String[] args) {
Athlete athlete = new Athlete();
athlete.run(); // OP: Athlete is running
athlete.jump();//OP:Athlete is jumping
}
}
Extending Java Interfaces:
interface GameCharacter {
void run();
}
interface AdvancedCharacter extends
GameCharacter {
void jump();
void collectCoins();
}
class Jake implements AdvancedCharacter {
public void run() {
System.out.println("Jake is running!");
}
public void jump() {
System.out.println("Jake jumps over a train!");
}
public void collectCoins() {
System.out.println("Jake collects coins!");
}
}
public class Main {
public static void main(String[] args) {
AdvancedCharacter jake = new Jake();
jake.run();
jake.jump();
jake.collectCoins();
}
}
Interfaces Vs Abstract classes:
Feature Interface Abstract Class
Purpose To define a contract (what should be done)
To provide a base class with common code (how
and what to do)
Keyword interface abstract class
Instantiation ❌ Cannot create objects ❌ Cannot create objects
Method types
Only abstract (until Java 8)Can have default,
static, private methods (Java 8+)
Can have abstract and concrete (normal)
methods
Field types Only constants (public static final) Can have variables, instance or static
Constructors ❌ No constructors ✅ Can have constructors
Multiple inheritance
✅ Yes — a class can implement multiple
interfaces
❌ No — a class can extend only one abstract
class
Access Modifiers All methods are public by default
Can use any access modifier (private, protected,
etc.)
Use case
When you want to enforce behavior across
unrelated classes
When you want to share code and behavior
across related classes
Quick Code Comparison
Interface Example:
interface Flyable {
void fly(); // abstract method
}
class Bird implements Flyable {
public void fly() {
System.out.println("Bird is flying.");
}
}
Abstract Class Example:
abstract class Animal {
abstract void sound(); // abstract method
void eat() {
System.out.println("Animal eats food.");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Dog barks.");
}
}

Interface in Java.pptxmb nmgvchtchhcjgchchmchdhydhhg

  • 1.
    Interface in Java "Definethe contract, let the classes do the work!“ -Varshini Priya.G [271116] CSE-B II YEAR
  • 2.
    INTERFACE: • An interfacein Java is a blueprint for a class. It is a collection of abstract methods (methods without a body) that defines a set of rules or behavior that a class must follow by implementing those methods. • Defined using the interface keyword. • A class implements an interface using the implements keyword. • A class can implement multiple interfaces, which allows multiple inheritance of behavior.
  • 3.
    IN SIMPLE: Imagine aninterface called RemoteControl interface RemoteControl { void turnOn(); // No body – just saying it must exist void turnOff(); } class TV implements RemoteControl { public void turnOn() { System.out.println("TV is ON"); } public void turnOff() { System.out.println("TV is OFF"); } }
  • 4.
    **Inheritence should notbe confused with interface ** Inheritence is between two classes , the child class extract its behaviour and state from the parent class . The parent can also work alone without child class. But in case of interface ,its not between two classes but between interface and class. The interface cannot act without the class . Interface = A remote control with buttons, but no battery or electronics inside. Class = Puts the electronics inside and makes the buttons work. Interface can not work without class the same way the remote
  • 5.
    A FUN EXAMPLE: interfaceSubwaySurfers { void run(); void collectCoins(); } // Jake.java - A character class class Jake implements SubwaySurfers { public void run() { System.out.println("Jake is running fast!"); } public void collectCoins() { System.out.println("Jake collects gold coins!"); } }
  • 6.
    // Tricky.java -Another character class class Tricky implements SubwaySurfers { public void run() { System.out.println("Tricky is sprinting down the track!"); } public void collectCoins() { System.out.println("Tricky grabs the coins skillfully!"); } }
  • 7.
    public class Main{ public static void main(String[] args) { SubwaySurfers jake = new Jake(); SubwaySurfers tricky = new Tricky(); System.out.println("== Jake's Actions =="); jake.run(); jake.collectCoins(); System.out.println("n== Tricky's Actions =="); tricky.run(); tricky.collectCoins(); } }
  • 8.
    another difference betweeninheritence and interface: In inheritence the child class can have only one parent class but in interface the class can have many interface . EXAMPLE: (coding ) interface Runner { void run(); } interface Jumper { void jump(); }
  • 9.
    class Athlete implementsRunner, Jumper { public void run() { System.out.println("Athlete is running"); } public void jump() { System.out.println("Athlete is jumping"); } public static void main(String[] args) { Athlete athlete = new Athlete(); athlete.run(); // OP: Athlete is running athlete.jump();//OP:Athlete is jumping } }
  • 10.
    Extending Java Interfaces: interfaceGameCharacter { void run(); } interface AdvancedCharacter extends GameCharacter { void jump(); void collectCoins(); }
  • 11.
    class Jake implementsAdvancedCharacter { public void run() { System.out.println("Jake is running!"); } public void jump() { System.out.println("Jake jumps over a train!"); } public void collectCoins() { System.out.println("Jake collects coins!"); } }
  • 12.
    public class Main{ public static void main(String[] args) { AdvancedCharacter jake = new Jake(); jake.run(); jake.jump(); jake.collectCoins(); } }
  • 13.
    Interfaces Vs Abstractclasses: Feature Interface Abstract Class Purpose To define a contract (what should be done) To provide a base class with common code (how and what to do) Keyword interface abstract class Instantiation ❌ Cannot create objects ❌ Cannot create objects Method types Only abstract (until Java 8)Can have default, static, private methods (Java 8+) Can have abstract and concrete (normal) methods Field types Only constants (public static final) Can have variables, instance or static Constructors ❌ No constructors ✅ Can have constructors Multiple inheritance ✅ Yes — a class can implement multiple interfaces ❌ No — a class can extend only one abstract class Access Modifiers All methods are public by default Can use any access modifier (private, protected, etc.) Use case When you want to enforce behavior across unrelated classes When you want to share code and behavior across related classes
  • 14.
    Quick Code Comparison InterfaceExample: interface Flyable { void fly(); // abstract method } class Bird implements Flyable { public void fly() { System.out.println("Bird is flying."); } }
  • 15.
    Abstract Class Example: abstractclass Animal { abstract void sound(); // abstract method void eat() { System.out.println("Animal eats food."); } } class Dog extends Animal { public void sound() { System.out.println("Dog barks."); } }