githubEdit

Ch 08: Interfaces and Abstract Classes

Source: Head First Java, Second Edition | Pages: 231-268

🎯 Learning Objectives

Abstraction and multiple inheritance

📚 Key Concepts

  • Abstract classes

  • Abstract methods

  • Interfaces

  • Implementing interfaces

  • Multiple inheritance via interfaces

  • When to use abstract vs interface

  • Object class and polymorphism


📖 Detailed Notes

1. Abstract classes

Essential concept for mastering Java and OOP.

Example:

2. Abstract methods

Essential concept for mastering Java and OOP.

Example:

3. Interfaces

Essential concept for mastering Java and OOP.

Example:

4. Implementing interfaces

Essential concept for mastering Java and OOP.

Example:

5. Multiple inheritance via interfaces

Essential concept for mastering Java and OOP.

Example:

6. When to use abstract vs interface

Essential concept for mastering Java and OOP.

Example:

7. Object class and polymorphism

Essential concept for mastering Java and OOP.

Example:


💡 Important Points to Remember

  • methods in

  • stuff

  • than that...

  • is

  • that an abstract class can have both abstract and non-abstract


✅ Self-Check Questions

Test your understanding:

  1. Can you explain the main concepts covered in this chapter?

  2. Can you write code examples demonstrating these concepts?

  3. Do you understand when and why to use these features?

  4. Can you explain the benefits and tradeoffs?

🔄 Quick Revision Points


📝 Practice Exercises

  1. Write your own code examples for each key concept

  2. Modify existing examples to test edge cases

  3. Explain concepts to someone else

  4. Create a small project using these concepts

Review related concepts from other chapters to build comprehensive understanding.


For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 231-268.

Chapter 8: Serious Polymorphism — Study Notes

This chapter explores how to exploit polymorphism fully by using abstract classes and interfaces. It focuses on designing flexible architectures where you can write code that works on generic types (like Animal) but executes specific behaviors at runtime (like Dog or Cat).


1. Abstract Classes

Sometimes a class is too generic to be instantiated. For example, in an animal simulation, you need Dog and Cat objects, but a generic Animal object doesn't make sense.

  • Definition: An abstract class is a class that cannot be instantiated (you cannot say new Animal()). It exists solely to be extended.

  • Purpose: It serves as a template for subclasses, enforcing a common protocol (a set of methods) that all subclasses must have.

  • Syntax: Use the abstract keyword.

    Java


2. Abstract Methods

An abstract class can contain abstract methods. These are methods that must be overridden by the first concrete (non-abstract) subclass.

  • No Body: Abstract methods have no body (no curly braces), just a semicolon.

    Java

  • The Rule: If a class contains even one abstract method, the class must be marked abstract.

  • Enforcement: This forces all concrete subclasses to implement the method, ensuring they all have that specific behavior (e.g., every Animal must implement eat(), but a Dog eats differently than a Lion).


3. The Ultimate Superclass: Object

In Java, every class extends Object, either directly or indirectly.

  • Universal Methods: Since every class inherits from Object, every object has certain methods, such as:

    • equals(Object o): Decides if two objects are "equal".

    • getClass(): Returns the class type of the object.

    • hashCode(): Returns a unique ID for the object (used in collections).

    • toString(): Returns a string representation of the object (often overridden to provide meaningful output).

  • Polymorphism with Object: You can use Object as a "universal type" to pass any object around (e.g., ArrayList<Object>), but this comes with a price: you lose the specific type information.


4. Casting and Type Safety

When you store a Dog in an Object reference (or ArrayList<Object>), the compiler only sees it as an Object. You cannot call Dog-specific methods (like bark()) on it unless you convert it back.

  • Downcasting: Converting a general reference back to a specific type.

    Java

  • Risk: If you cast an object to the wrong type (e.g., casting a Cat to a Dog), Java will throw a ClassCastException at runtime.

  • instanceof Operator: Use this to check an object's type safely before casting.

    Java


5. Interfaces

Java does not support "multiple inheritance" (a class cannot extend two parents like Dog extends Animal, Pet). To solve this, Java uses interfaces.

  • Definition: An interface is a 100% abstract class. All methods in an interface are abstract by default.

  • Contract: An interface defines a "role" that a class can play (e.g., Pet, Serializable). It says, "If you implement this interface, you promise to define these methods."

  • Syntax:

    • Define: public interface Pet { ... }

    • Implement: public class Dog extends Animal implements Pet { ... }

  • Multiple Implementation: A class can extend only one parent class, but it can implement many interfaces.

    Java


6. Abstract Class vs. Interface

Feature

Abstract Class

Interface

Methods

Can have both abstract and concrete methods.

All methods are implicitly public and abstract.

Inheritance

A class can extend only one abstract class.

A class can implement multiple interfaces.

Usage

Use when classes share a common "core" identity (IS-A).

Use to define a "role" or capability (e.g., Playable).


7. Revision Checklist

  • abstract vs concrete: Can you explain why you can't say new Animal() if Animal is abstract?

  • Polymorphism: Do you understand that List<Animal> can hold Dog, Cat, and Lion objects?

  • Interface Implementation: Remember that when you implement an interface, you must write the code for every method defined in that interface.

  • Super: Use super.methodName() to call the superclass version of a method from within a subclass override.

Last updated