7/22/25 reviewJava Classes, Interfaces, Abstract Classes & Constructors

Regular (Concrete) Class

  • Complete blueprint for creating objects.

    • Can contain:

    • Fields (instance variables)

    • Methods (functions)

    • Constructors

    • Static blocks

  • Instantiable directly using the newnew keyword.

  • Real-world analogy: Car class

    • Writing newCar()new\,Car() produces a physical car based on the blueprint.

  • Example discussed

    • 4 fully-defined methods: color(), door(), wheel(), engine()

    • Each method initializes a corresponding variable (no abstraction).

  • Characteristics

    • No restrictions on method bodies or variables.

    • Represents a concrete implementation (0 % abstraction).

Interface

  • Acts like a contract / pure blueprint.

    • Declares only abstract methods (implicitly publicabstractpublic\,abstract).

    • No instance variables; only constants allowed (publicstaticfinalpublic\,static\,final).

    • Cannot be instantiated.

    • Supports multiple inheritance (a class can implement many interfaces).

  • Analogy: Animal vs. Dog/Cat/Bird

    • You never meet a generic “Animal” on the street; you meet concrete species.

  • Code snapshot

    • public interface CarSpec {
      void iColor();
      void iDoor();
      void iWheel();
      void iEngine();
      }

    • No method bodies ⇒ only declares what must be done, not how.

  • Usage rules

    • A class must use the implements keyword and override all methods.

    • Ensures every implementing class fulfils the same behaviour contract.

Interface Implementation (Concrete Class that Implements)

  • Syntax: class MyCar implements CarSpec { … }

  • Each interface method is implemented and annotated with @Override.

  • Provides the actual logic (method bodies), thereby enforcing the contract.

Abstract Class

  • Cannot be instantiated.

  • May mix

    • Abstract methods (no body)

    • Non-abstract methods (concrete, with body)

    • Fields, constructors, any access modifiers

  • Enables partial abstraction (some behaviour defined, some deferred).

  • Example pattern

    • Abstract class declares abstract void calculateMileage();

    • Also defines a concrete helper like void printBrand() { … }

  • To use:

    • Create a child class with the extends keyword.

    • Child must provide bodies for all inherited abstract methods.

    • Inherited non-abstract methods are available directly.

Side-by-Side Comparison

  • Method bodies

    • Interface: not allowed (except optional default / static)

    • Abstract class: allowed

    • Regular class: allowed

  • Abstract methods

    • Interface: all methods abstract

    • Abstract class: some may be abstract

    • Regular class: none

  • Variables

    • Interface: publicstaticfinalpublic\,static\,final only (constants)

    • Abstract class & Regular class: any type

  • Multiple inheritance

    • Interface: Yes (a class can implement many)

    • Abstract & Regular: No (single class hierarchy)

  • Instantiation

    • Interface: No

    • Abstract class: No

    • Regular class: Yes

  • Typical use case

    • Interface ⇒ full abstraction, unrelated classes, multiple inheritance.

    • Abstract class ⇒ partial abstraction, shared code + deferred parts.

    • Regular class ⇒ complete implementation, ready to use objects.

Constructors in Java

  • Special method invoked automatically when an object is created.

    • Same name as the class.

    • No return type (not even voidvoid).

    • Cannot be static or overridden but can be overloaded.

  • Purpose: initialise object state.

Default Constructor

  • No parameters.

  • Provided implicitly by Java if none is written.

  • Transcript example prints “Default Constructor”.

  • Analogy: ordering a standard cheese pizza without customisation.

Parameterised Constructor

  • Accepts arguments to set up object with specific data.

  • Example output: brand = “Yamaha”.

  • Analogy: customised pizza ("extra cheese & mushrooms").

Constructor Overloading

  • Multiple constructors in same class, each with different parameter lists.

  • Java differentiates by number & types of parameters.

  • Analogy: coffee shop menu

    • “Coffee” ⇒ default

    • “Coffee with milk” ⇒ one-argument version, etc.

Quick Table
  • Default ⇒ basic, no parameters, not customisable.

  • Parameterised ⇒ accepts data, customisable.

  • Overloaded set ⇒ flexible: some versions with, some without parameters.

Relationships & Inheritance Rules

  • Class  A  extends  Class  B\text{Class}\;A \; extends \; Class\;B ⇒ single inheritance.

  • AbstractClass  X  extends  AbstractClass  Y\text{AbstractClass}\;X \; extends \; AbstractClass\;Y ⇒ allowed.

  • Class  C  implements  Interface  I<em>1,I</em>2,\text{Class}\;C \; implements \; Interface\;I<em>1, I</em>2,\dots ⇒ multiple interfaces.

  • Interface  J  extends  Interface  K\text{Interface}\;J \; extends \; Interface\;K ⇒ interface-to-interface.

  • AbstractClass  Z\text{AbstractClass}\;Z may implement interfaces and still stay abstract.

When to Choose What?

  • Full abstraction & need for multiple inheritance ⇒ Interface.

  • Partial abstraction, shared code + must force subclasses to finish work ⇒ Abstract Class.

  • Ready-to-use, reusable objects with state & behaviour ⇒ Regular Class.

  • Need to initialise or overload creation logic ⇒ Constructor(s).

Grand Analogies Summary

  • Class = Blueprint of a car.

  • Object = Actual car built from the blueprint.

  • Abstract Class = General blueprint like Shape (circle, rectangle, etc.).

  • Interface = Remote-control contract / driver’s licence – defines allowed actions.

  • Constructor = Filling in a form to register / create the object identity.

Key Takeaways

  • Regular class: 0 % abstraction, instantiable.

  • Interface: 100 % abstraction, no state, multiple inheritance.

  • Abstract class: 0-99 % abstraction, can store state.

  • Constructors: match class name, no return, auto-invoked, can be overloaded.

Regular (Concrete) Class

  • Complete blueprint for creating objects, serving as a template for instances with specific states and behaviors.

  • Can contain:

    • Fields (instance variables): To hold data unique to each object.

    • Methods (functions): To define actions an object can perform.

    • Constructors: Special methods for initializing a new object.

    • Static blocks: For static initialization logic.

  • Instantiable directly using the newnew keyword, meaning you can create actual objects from this blueprint.

  • Real-world analogy: Car class- Writing newCar()new\,Car() produces a physical car based on the blueprint, with its own color, number of doors, wheels, and engine.

  • Example discussed- 4 fully-defined methods: color(), door(), wheel(), engine()

    • Each method initializes a corresponding variable (no abstraction or deferred implementation).

  • Characteristics- No restrictions on method bodies or variables; all members have complete implementations.

    • Represents a concrete implementation (0 % abstraction). This is the most common type of class in everyday programming.

Interface

  • Acts like a contract / pure blueprint for behavior, defining a set of methods that implementing classes must provide.

  • Declares only abstract methods (implicitly publicabstractpublic\,abstract by default in older Java versions). These methods have no bodies, only signatures, indicating what functionality must be provided.

    • Java 8 introduced default methods: These have an implementation in the interface itself and can be optionally overridden by implementing classes. They allow adding new methods to interfaces without breaking existing implementations.

    • Java 9 introduced private methods: Can be used within default or static methods inside the interface.

    • static methods: Can also be defined with bodies in interfaces, providing utility methods related to the interface itself.

  • No instance variables allowed; only constants permissible (implicitly publicstaticfinalpublic\,static\,final by default).

  • Cannot be instantiated directly. You cannot create an object of an interface.

  • Supports multiple inheritance in terms of type and behavior (a class can implement many interfaces), overcoming Java's single inheritance limitation for classes.

  • Analogy: Animal vs. Dog/Cat/Bird- You never meet a generic “Animal” on the street; you meet concrete species (like Dog or Cat), which implement the Animal