0% found this document useful (0 votes)
5 views5 pages

Java Interface Study Notes

The document provides an overview of Java interfaces, defining them as collections of abstract methods that standardize method names across implementing classes. It outlines key rules governing interfaces, including their role in promoting polymorphism, the inability to implement other interfaces, and the requirement for abstract classes when partially implementing an interface. Additionally, it compares interfaces with abstract classes, highlighting differences in method types, variable declarations, and inheritance capabilities.

Uploaded by

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

Java Interface Study Notes

The document provides an overview of Java interfaces, defining them as collections of abstract methods that standardize method names across implementing classes. It outlines key rules governing interfaces, including their role in promoting polymorphism, the inability to implement other interfaces, and the requirement for abstract classes when partially implementing an interface. Additionally, it compares interfaces with abstract classes, highlighting differences in method types, variable declarations, and inheritance capabilities.

Uploaded by

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

Java Interface — Study Notes

TAP Academy · Pages 153–163

1. What is an Interface?
An interface is a collection of pure abstract methods. Inside an interface you have only method
signatures — no method bodies.
Syntax: interface InterfaceName { void method1(); void method2(); }
In UML diagrams, an interface is represented by a rectangular box with angular brackets < >.

1.1 Why Interfaces? — The Standardization Problem


Without an interface, different developers may implement the same functionality with completely
different method names (e.g., add() vs addition() vs sum()). This makes it hard for users to
remember which method to call.
An interface solves this by acting as a contract — all implementing classes must use the same
method names, achieving standardization.

interface Calculator {
void add(); // public abstract by default
void sub();
}

class MyCalculator1 implements Calculator {


@Override
public void add() { int a=200, b=100; [Link](a+b); }
@Override
public void sub() { int a=200, b=100; [Link](a-b); }
}

class MyCalculator2 implements Calculator {


@Override
public void add() {
Scanner scan = new Scanner([Link]);
int a = [Link](); int b = [Link]();
[Link](a+b);
}
@Override
public void sub() { /* similar with scanner */ }
}
2. Rules of Interface (All 11 Rules)

# Rule
1 Acts like a contract to achieve standardization.
2 Promotes polymorphism — interface type reference can point to implementing class
objects (loose coupling).
3 All methods are automatically public and abstract.
4 Specialized (extra) methods of the implementing class cannot be accessed via
interface type reference.
5 If a class partially implements an interface, it must declare itself abstract.
6 A class can implement multiple interfaces (no diamond problem since interfaces have
no parents).
7 An interface CANNOT implement another interface (no method bodies allowed inside
interfaces).
8 An interface CAN extend another interface (even multiple — multiple inheritance via
interfaces).
9 A class can extend a class AND implement an interface. Order: extends first,
implements later.
10 Variables inside an interface are automatically public static final (constants).
11 An empty interface is called a Marker/Tagged interface (e.g., Serializable).

Rule 2 — Polymorphism with Interface Type Reference


An interface reference can hold any object of a class that implements it, just like a parent-class
reference holding a child object:
MyCalculator1 mc1 = new MyCalculator1();
Calculator c; // interface type reference
c = mc1; // loosely coupled
[Link](); // calls MyCalculator1's add()
[Link](); // calls MyCalculator1's sub()
📝 Note: This achieves loose coupling, code reduction, and code flexibility — same
advantages as polymorphism.
Rule 4 — Specialized Methods Not Accessible via Interface Reference
If the implementing class has extra (specialized) methods not declared in the interface, they
cannot be called via the interface reference:
class MyCalculator implements Calculator {
public void add() { ... }
public void sub() { ... }
public void mul() { ... } // specialized — NOT in interface
}

Calculator c = new MyCalculator();


[Link](); // OK
[Link](); // OK
[Link](); // ERROR: mul() not defined in Calculator interface

Rule 5 — Partial Implementation Requires abstract Class


If a class implements only some methods of an interface (not all), it must be declared abstract:
abstract class MyCalculator implements Calculator {
@Override
public void add() { int a=200, b=100; [Link](a+b); }
// sub() NOT implemented — class must be abstract
}
Rule 6 — Multiple Interface Implementation
A class can implement multiple interfaces. This avoids the diamond problem since interfaces
have no parent classes:
interface Calculator1 { void add(); void sub(); }
interface Calculator2 { void mul(); void div(); }

class MyCalculator implements Calculator1, Calculator2 {


public void add() { ... }
public void sub() { ... }
public void mul() { ... }
public void div() { ... }
}

Rule 7 — Interface Cannot Implement Another Interface


An interface only contains method signatures (no bodies), so it cannot use implements:
interface Calculator2 implements Calculator1 { // ERROR!
void mul();
}

Rule 8 — Interface CAN Extend Another Interface (Multiple


Inheritance)
An interface can extend one or more interfaces. This is how multiple inheritance is achieved in
Java:
interface Calculator1 { void add(); }
interface Calculator2 { void sub(); }

interface Calculator3 extends Calculator1, Calculator2 {


void mul(); // Calculator3 now has add(), sub(), mul()
}

class MyCalculator implements Calculator3 {


// Must implement add(), sub(), AND mul()
}

Rule 9 — Extend a Class AND Implement an Interface


A class can do both — but extends must come before implements:
interface Calculator1 { void add(); void sub(); }

class Calculator2 {
public void mul() { ... }
public void div() { ... }
}

// extends first, implements after


class MyCalculator extends Calculator2 implements Calculator1 {
public void add() { ... } // from interface
public void sub() { ... } // from interface
// mul() and div() inherited from Calculator2
}

Rule 10 — Variables Are Automatically public static final


Any variable declared inside an interface is implicitly public static final (a constant):
interface Calculator {
int count = 3; // same as: public static final int count = 3;
void fun(); // same as: public abstract void fun();
}
• public — accessible to all implementing classes.
• static — one shared copy across all implementing class objects.
• final — value cannot be modified.

Rule 11 — Marker / Tagged Interface


An empty interface (no methods, no variables) is called a Marker Interface or Tagged Interface.
It provides special properties to the objects of implementing classes:
interface Calculator { } // Marker Interface

// Real-world example from [Link]:


// interface Serializable { } // marks class as serializable

3. Interface vs Abstract Class — Quick Comparison

Property Interface Abstract Class


Keyword interface abstract class
Methods All abstract by default Can have both abstract &
concrete
Variables public static final only Any type of variables
Inheritance implements (multiple OK) extends (single only)
Constructor Not allowed Allowed
Multiple inheritance Yes (via implements) No
Extend interfaces Yes (using extends) Yes (using implements)

TAP Academy — Java Interface Notes (Pages 153–163)

You might also like