⚡0 XP🔥0

Abstract Classes and Interfaces

Lesson 11 of 15 ⏱ 5 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Java

Abstraction with abstract classes and abstract methods, interfaces with default and static methods, implementing multiple interfaces, functional interfaces, and when to choose which.

Abstraction means showing what an object can do while hiding how. Java gives you two tools: abstract classes and interfaces.

Abstract classes#

An abstract class cannot be instantiated. It can contain abstract methods (no body) that every concrete subclass must implement, along with normal fields and methods:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Meera (Savings): 52000.00
Om (FixedDeposit): 214200.00
हिंदी में समझें

Abstract class एक अधूरा नक्शा है - इसमें कुछ methods का सिर्फ़ नाम होता है (abstract), body नहीं। इसका object नहीं बनता। Child class को वो methods पूरे करने ही पड़ते हैं। इससे सब child classes एक जैसा "contract" मानती हैं, पर हर एक अपने तरीके से काम करती है।

Interfaces#

An interface is a pure contract: a set of methods a class promises to provide. A class implements an interface and must define all its abstract methods. A class can implement many interfaces - this is how Java gets multiple inheritance of type safely.

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Printing document...
Inkjet printing in colour
Got scanned-page.pdf

Interface methods are implicitly public abstract, and interface fields are implicitly public static final (constants).

default and static methods in interfaces#

Since Java 8, interfaces can have default methods (with a body, inherited by implementers) and static helper methods:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Java Full Stack: 20000.0 + GST 3600.0

Comparable - an interface you will use constantly#

Implementing Comparable<T> gives a class a natural order, so Arrays.sort and Collections.sort can sort it:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
[Nisha(9.47), Kabir(9.16), Yash(7.78)]

Functional interfaces#

An interface with exactly one abstract method is a functional interface and can be implemented with a lambda (covered in the Lambdas lesson):

▶ Try it YourselfJava
Ctrl + Enter to run
Output
12
1024

Abstract class or interface?#

Abstract class Interface
Instance fields (state) yes no (only constants)
Constructors yes no
Method bodies yes default/static/private methods only
How many can a class use one (extends) many (implements)
Best for closely related classes sharing code and state capabilities shared by unrelated classes (Comparable, Runnable, Printable)
Tip

A common design: an interface for the contract (Payment), an optional abstract class for shared code (AbstractPayment), and concrete classes (UpiPayment, CardPayment). Program against the interface type.

Summary#

  • abstract classes can't be instantiated and may contain abstract (bodiless) methods that subclasses must implement.
  • Interfaces define contracts; a class can implement many; they can have default and static methods.
  • Comparable gives natural ordering; one-method interfaces can be implemented with lambdas.
  • Use an abstract class for shared state/code, an interface for shared capability.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Payable interface

Create an interface Payable with double amountDue() and a default method String summary() returning "<class name>: Rs <amount>" (use getClass().getSimpleName()). Implement it in Invoice(qty, unitPrice) and Salary(monthly). The given main prints:

Invoice: Rs 4500.0
Salary: Rs 52000.0
Total due: Rs 56500.0
Expected output:
Invoice: Rs 4500.0
Salary: Rs 52000.0
Total due: Rs 56500.0

Quick Quiz

Q1.

Can you create an object of an abstract class?

Q2.

How many interfaces can a class implement?

Q3.

Which keyword does a class use for an interface?

Q4.

What is a default method in an interface?

Q5.

When is an abstract class a better choice than an interface?