Overview of Classes, Abstract Classes & Interfaces
public static final constants and public abstract methods. JDK 8 introduced default and static interface methods; JDK 9 added private helper methods in interfaces.1. Topic Introduction
Module 1 concludes its Java foundations by introducing the three primary structural blueprints of Java programs: Concrete Classes, Abstract Classes, and Interfaces. While Modules 2 and 3 explore inheritance and interface hierarchies in full depth, understanding what separates these three constructs right now lets you read any Java program clearly.
class), Incomplete Frame (abstract), and Electrical Standard (interface)A normal **class** is a complete blueprint—you can immediately build real houses (new) from it. An **abstract class** is a partially completed blueprint: it includes shared walls and plumbing, but leaves the roof design blank (abstract method) so specific subclasses must finish it before building. An **interface** is a universal 3-pin electrical socket standard: it doesn't build the appliance at all—it just specifies WHAT contract any compatible plug must obey!
class = complete blueprint (can instantiate with new); abstract class = incomplete template (0–100% abstraction, CANNOT instantiate); interface = pure capability contract (implements, supports multiple inheritance).3. Essential Exam Definitions
abstract modifier that cannot be directly instantiated (new), designed to share common fields/methods while optionally leaving abstract methods (without body) for subclasses to implement.interface specifying what a class must do (contract of abstract methods + constants) rather than how it does it; a single class can implement multiple interfaces.4. Detailed Technical Explanation & Syntax
1. Concrete Classes
Declared with [access] class ClassName { ... }. A concrete class may contain fields, constructors, concrete methods, blocks, and nested types. It can be instantiated freely using new, and cannot contain any abstract methods.
2. Abstract Classes & Abstract Methods
Declared with abstract class Shape { ... }. Key rules from your notes:
- An abstract class CANNOT be instantiated (
new Shape()is a compile error). - It CAN contain both
abstractmethods (no body:abstract void draw();) and normal concrete methods with full bodies, plus constructors and instance variables. - An abstract class does NOT necessarily have to contain an abstract method—marking it
abstractalone prevents direct instantiation. - If ANY regular class
extendsan abstract class, it MUST override all inherited abstract methods—otherwise that subclass must also be declaredabstract. - An
abstractclass or method can NEVER be markedfinal(sinceabstractdemands subclassing/overriding, whilefinalforbids both!).
3. Interfaces (Contract Blueprint)
Declared with interface Printable { ... }. Key rules:
- Interface variables are implicitly
public static final(constants; must be initialized). - Methods without
default/static/privateare implicitlypublic abstract(method signature only, ending in;). - A class uses
implements Interface1, Interface2and MUST declare implemented methodspublic. - A class can
extendsonly ONE class, yet canimplementsANY NUMBER of interfaces, enabling multiple inheritance of type.
5. Working Principle: Choosing Between Class, Abstract Class, and Interface
- Use a normal
classwhen the entity is complete and objects should be constructed directly (new Student()). - Use an
abstract classwhen closely related classes share common state/code (IS-A, e.g.,Vehicle->Car,Bike), yet a generic object makes no sense. - Use an
interfacewhen unrelated classes need to share a common capability or contract (CAN-DO, e.g.,Printable,Comparable).
7. Complete Java Code Examples & Tracing
| 1 | interface Graded { |
| 2 | int PASS_PERCENT = 40; // implicitly public static final |
| 3 | boolean isPassed(); // implicitly public abstract |
| 4 | } |
| 5 | |
| 6 | abstract class Assessment { |
| 7 | String title; // instance variable allowed in abstract class |
| 8 | |
| 9 | Assessment(String title) { |
| 10 | this.title = title; |
| 11 | } |
| 12 | |
| 13 | void printTitle() { // concrete shared method |
| 14 | System.out.println("Assessment: " + title); |
| 15 | } |
| 16 | |
| 17 | abstract double percentage(); // subclass MUST implement |
| 18 | } |
| 19 | |
| 20 | public class BlueprintOverviewDemo extends Assessment implements Graded { |
| 21 | int scored; |
| 22 | int max; |
| 23 | |
| 24 | BlueprintOverviewDemo(String title, int scored, int max) { |
| 25 | super(title); |
| 26 | this.scored = scored; |
| 27 | this.max = max; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | double percentage() { |
| 32 | return (scored * 100.0) / max; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public boolean isPassed() { |
| 37 | return percentage() >= PASS_PERCENT; |
| 38 | } |
| 39 | |
| 40 | public static void main(String[] args) { |
| 41 | // Assessment a = new Assessment("Test"); // ILLEGAL: abstract class! |
| 42 | BlueprintOverviewDemo ese = new BlueprintOverviewDemo("KTU OOP ESE", 51, 60); |
| 43 | ese.printTitle(); |
| 44 | System.out.println("Percentage: " + ese.percentage() + "% | Passed: " + ese.isPassed()); |
| 45 | } |
| 46 | } |
Assessment: KTU OOP ESE Percentage: 85.0% | Passed: true
classabstractinterfaceextendsimplements@Override- Forgetting
publicwhen implementing an interface method (isPassed()withoutpublictriggers an access-weakening compile error!). - Trying to instantiate an
abstract classorinterfacedirectly usingnew. - Combining
abstractandfinalon the same class or method.
Comparison Tables
| Feature | Class | Abstract Class | Interface |
|---|---|---|---|
Instantiation (new) | Yes | No | No |
| Instance Variables | Yes | Yes | No (only public static final constants) |
| Constructors | Yes | Yes (called via super()) | No |
| Methods | Concrete only | Concrete + Abstract | Abstract (plus default/static JDK 8+) |
| Multiple Inheritance | Single (extends 1 class) | Single (extends 1 class) | Multiple (implements A, B, C) |
Real-World Applications
- Java Swing's
AbstractButtonabstracts shared button mechanics forJButton,JCheckBox, andJRadioButton. - Java's
ActionListenerandRunnableinterfaces standardize event and thread execution contracts.
Important KTU Exam Points
- State clearly: An abstract class can have 0 abstract methods, yet a class with even 1 abstract method MUST be declared abstract.
- Interface methods implemented in a class MUST always be marked
public.
Memory Techniques & Revision
- Abstract Class = 'Partial Blueprint + Constructors Allowed'; Interface = 'Pure Contract + Constants Only + Multiple Implements'.
Common Pitfalls to Avoid
- Thinking an abstract class cannot have a constructor—it CAN, and subclasses invoke it via
super().
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: Java uses classes, abstract classes, and interfaces to model concrete objects, partial abstractions, and pure behavioral contracts respectively.
- Concrete Class: Complete blueprint containing fields, constructors, and implemented methods; can be instantiated directly via
new. - Abstract Class: Declared with
abstract, cannot be instantiated, may combine concrete methods, constructors, andabstractmethods without bodies; supports single inheritance. - Interface: Declared with
interface, cannot be instantiated or hold instance fields/constructors; fields arepublic static finaland methods specify what to do rather than how; a class can implement multiple interfaces.
Quick Understanding Check
1. Can an abstract class in Java have a constructor and zero abstract methods?