KTU OOP Java2024 Scheme • 60M ESE
MODULE 1/Section 1: Introduction to Java

Overview of Classes, Abstract Classes & Interfaces

MEDIUM PRIORITY (Prep Guide)Official Syllabus~13 min study
Official KTU 2024 Syllabus Topics Covered Here
ClassesAbstract classesInterfaces
Java Version Note: Prior to JDK 8, interfaces could only hold 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.

2. Start From Zero: Ready House Plan (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!

Key Takeaway: 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

Class
A user-defined blueprint or template that groups state (fields/variables) and behavior (methods/constructors) common to a set of objects.
Abstract Class
A class declared with the 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
A reference type declared with 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 abstract methods (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 abstract alone prevents direct instantiation.
  • If ANY regular class extends an abstract class, it MUST override all inherited abstract methods—otherwise that subclass must also be declared abstract.
  • An abstract class or method can NEVER be marked final (since abstract demands subclassing/overriding, while final forbids 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/private are implicitly public abstract (method signature only, ending in ;).
  • A class uses implements Interface1, Interface2 and MUST declare implemented methods public.
  • A class can extends only ONE class, yet can implements ANY NUMBER of interfaces, enabling multiple inheritance of type.

5. Working Principle: Choosing Between Class, Abstract Class, and Interface

  1. Use a normal class when the entity is complete and objects should be constructed directly (new Student()).
  2. Use an abstract class when closely related classes share common state/code (IS-A, e.g., Vehicle -> Car, Bike), yet a generic object makes no sense.
  3. Use an interface when unrelated classes need to share a common capability or contract (CAN-DO, e.g., Printable, Comparable).

7. Complete Java Code Examples & Tracing

Side-by-Side Demo: Concrete Class, Abstract Class & InterfaceBlueprintOverviewDemo.java
Shows an interface contract, an abstract base class with shared code + abstract method, and a concrete subclass implementing both.
1interface Graded {
2 int PASS_PERCENT = 40; // implicitly public static final
3 boolean isPassed(); // implicitly public abstract
4}
5
6abstract 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
20public 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}
Expected Output
Assessment: KTU OOP ESE
Percentage: 85.0% | Passed: true
Keywords:classabstractinterfaceextendsimplements@Override
Common Exam & Coding Mistakes
  • Forgetting public when implementing an interface method (isPassed() without public triggers an access-weakening compile error!).
  • Trying to instantiate an abstract class or interface directly using new.
  • Combining abstract and final on the same class or method.

Comparison Tables

Concrete Class vs Abstract Class vs Interface
FeatureClassAbstract ClassInterface
Instantiation (new)YesNoNo
Instance VariablesYesYesNo (only public static final constants)
ConstructorsYesYes (called via super())No
MethodsConcrete onlyConcrete + AbstractAbstract (plus default/static JDK 8+)
Multiple InheritanceSingle (extends 1 class)Single (extends 1 class)Multiple (implements A, B, C)

Real-World Applications

  • Java Swing's AbstractButton abstracts shared button mechanics for JButton, JCheckBox, and JRadioButton.
  • Java's ActionListener and Runnable interfaces 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, and abstract methods without bodies; supports single inheritance.
  • Interface: Declared with interface, cannot be instantiated or hold instance fields/constructors; fields are public static final and 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?