Nearby lessons
43 of 125Java - Interfaces
📌 What You Will Learn
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
Interfaces is a core concept of the Java language. This lesson explains Interface — The Full Rules and Class vs Abstract Class vs Interface — Master Table with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Interface — The Full Rules
In the classic Java (before Java 8), an interface had these defaults:
- Every variable in an interface is public static final automatically — you do not write it.
- Every method in an interface is public abstract automatically.
- Interfaces cannot have constructors.
- You cannot create an object of an interface — only a reference variable.
Example01
Class vs Abstract Class vs Interface — Master Table
| Point | Class | Abstract class | Interface |
|---|---|---|---|
| Concrete methods | Allowed | Allowed | Not (classically) |
| Abstract methods | Not allowed | Allowed | Allowed |
| Keyword | class | abstract class | interface |
| Objects | Yes | No (only references) | No (only references) |
| Variables default | No default | No default | public static final |
| Methods default | No default | No default | public abstract |
| Constructors | Allowed | Allowed | Not allowed |
| Sharability | Less | Medium | Most |
📝 Key Takeaways
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1