Java OOP: quick revision, 12 bites

All 12 revision bites from this kit, free and with no sign-up.

Abstraction: Hide Complexity, Show Essentials

What you see is what you need.

**Abstraction** is the process of hiding implementation details and exposing only essential features to the user. In Java, abstraction is achieved using **abstract classes** and **interfaces**.

Takeaway. Abstraction lets you drive a car without knowing how the engine works.

Abstract Class: Partial Abstraction

A class that can't be instantiated.

An **abstract class** is declared with the `abstract` keyword. It can have both **abstract methods** (without body) and **concrete methods** (with body). You cannot create objects of an abstract class.

Takeaway. Abstract classes provide a common base with some shared code.

Interface: Pure Abstraction Contract

A blueprint of what a class must do.

An **interface** is a reference type that contains only **abstract methods**, **default methods**, **static methods**, and **constants**. It specifies a contract that implementing classes must follow.

Takeaway. Interfaces define capabilities without dictating implementation.

Abstract Class vs Interface

When to use which?

Takeaway. Use abstract class for shared code; interface for capability contract.

Abstract Methods Have No Body

Declaration only, no implementation.

An **abstract method** is declared without an implementation (no curly braces). It must be overridden by the first concrete subclass. If a class contains any abstract method, the class itself must be declared `abstract`.

Takeaway. Abstract methods force subclasses to provide specific behavior.

How to Implement Abstraction

Steps to abstract in Java.

Takeaway. Abstraction is a design process, not just a keyword.

Real-World Example: Payment System

Different payments, same interface.

The `Payment` interface abstracts the payment process. Client code can call `pay()` without knowing the specific payment method.

Takeaway. Abstraction allows swapping implementations without changing client code.

Common Mistake: Forgetting to Override

Abstract methods must be implemented.

Takeaway. Always implement inherited abstract methods in concrete classes.

Exam Tip: Abstract vs Interface Keywords

Remember the syntax differences.

Takeaway. Use `extends` for abstract classes, `implements` for interfaces.

Memory Trick: IS-A vs CAN-DO

Abstract class = IS-A, Interface = CAN-DO.

Use an **abstract class** when the subclass **IS-A** more specific type (e.g., `Car` IS-A `Vehicle`). Use an **interface** when the subclass **CAN-DO** something (e.g., `Car` CAN-DO `Drivable`).

Takeaway. IS-A for identity, CAN-DO for capability.

Did You Know? Interfaces Can Have Default Methods

Java 8 added default methods.

Since Java 8, interfaces can have **default methods** (with implementation) and **static methods**. This allows adding new methods to interfaces without breaking existing implementations.

Takeaway. Default methods maintain backward compatibility.

Best Practice: Program to an Interface

Code to the contract, not the implementation.

Always declare variables using the **interface** or **abstract class** type, not the concrete class. This makes your code more flexible and easier to change.

Takeaway. Interface-based programming reduces coupling.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages