Methods of Java · Introduction to Java Programming

Interface in a Package

Watch lectures

Interface in a Package

1. What Is an Interface? (beginner)

Interface = pure promise list (methods WITHOUT bodies! public abstract by default). Class implements (fulfills ALL!). Like job contract: sign = do all duties! Enables multiple-inheritance!

2. Code — Drawable Contract (exam classic!)

package shapes; // interface lives in package too!
public interface Drawable {
    void draw(); // public abstract (auto!) — no body!
    default void info() { System.out.println("shape"); } // Java 8+ body allowed!
}
class Circle implements Drawable { // fulfills!
    public void draw() { System.out.println("O"); } // MUST public! (can't weaken!)
}
Drawable d = new Circle(); // interface ref (polymorphism!)
d.draw(); // O (object wins!)

3. Rules (exam traps!)

  • Methods: public abstract auto (Java 7). default/static bodies allowed (Java 8+).
  • Fields: public static final constants ONLY (int X = 10; = constant!).
  • Class implements ALL (else abstract!). implements A, B MANY (multiple ok!).
  • interface extends interface MANY (interface C extends A, B!).

4. Interface vs Abstract (exam table!)

Interface Abstract class
Bodies none (default ok) mix!
Fields constants only any!
Inherits many! one!
new? No No

5. Exam Points

  • Interface + implement program — 7 marks, very frequent.
  • Rules + vs abstract — 5 marks, frequent.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Introduction to Java Programming modules