Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

Java Interfaces: Contracts and Multiple Inheritance

A Payment interface lists one method, pay. Card and Upi each write it, so one checkout method takes any Payment. Then Card implements Payment and Refundable at once: Java's way of giving a class more than one parent type.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

What you will learn

  • Declare an interface and implement it in a class
  • Write code that asks for the interface so any implementing class fits
  • Implement two interfaces in one class and use the object through either type
  • Know the rules: many interfaces, one parent class, no new on an interface

Before you start

  • Java abstract classes
  • Java polymorphism

An interface is a list of methods a class promises to write. It names them but doesn't write them. It's close to an abstract class, with one big difference you'll see below: a class can sign up to several interfaces at once.

A list of methods to write

package org.studyeasy.s03l15;

// An interface: the method every way of paying must have
public interface Payment {
    String pay(double amount);    // no body: each class writes it
}

It says interface, not class. pay has no braces and no logic. Every class that implements Payment must write pay, or it won't compile.

implements Payment

package org.studyeasy.s03l15;

public class Upi implements Payment {
    @Override
    public String pay(double amount) {
        return "UPI paid " + amount;
    }
}

implements Payment means Upi agrees to write every method in Payment, with the same name and parameters. (UPI is a payment system in India; here it's just one more way to pay.) Rename pay to payNow and javac says Upi is not abstract and does not override pay(double).

One checkout, any payment

package org.studyeasy.s03l15;

public class Ex01_Checkout {
    // Asks for a Payment, so any class that implements it fits
    static void checkout(Payment payment, double amount) {
        System.out.println(payment.pay(amount));
    }

    public static void main(String[] args) {
        checkout(new Card(), 250);
        checkout(new Upi(), 80);
    }
}
Card paid 250.0
UPI paid 80.0

checkout only knows Payment. Pass a Card and Card's pay runs; pass a Upi and Upi's runs. Same line, different object. Add a new way to pay tomorrow and checkout doesn't change. This is called programming to an interface.

You can't create the interface itself: new Payment() gives "Payment is abstract; cannot be instantiated". It has no code to run.

Two interfaces, Payment with pay and Refundable with refund. Card implements both, with dashed lines to each. Upi implements only Payment. checkout(Payment) accepts both Card and Upi. interface Paymentpay(amount) interface Refundablerefund(amount) Upiimplements Payment Cardimplements both implements (dashed)
One class can implement several interfaces; code that asks for Payment accepts both Card and Upi.

One class, two interfaces

package org.studyeasy.s03l15;

// A second interface: payments that can be given back
public interface Refundable {
    String refund(double amount);
}
package org.studyeasy.s03l15;

// Card implements two interfaces, so it writes both methods
public class Card implements Payment, Refundable {
    @Override
    public String pay(double amount) {
        return "Card paid " + amount;
    }

    @Override
    public String refund(double amount) {
        return "Card refunded " + amount;
    }
}

A comma separates the interfaces, and Card writes both methods. Delete refund and Card no longer compiles. This is Java's alternative to multiple inheritance: one parent class at most, but as many interfaces as you like.

package org.studyeasy.s03l15;

public class Ex02_TwoInterfaces {
    public static void main(String[] args) {
        Card card = new Card();
        Payment payment = card;         // seen as a Payment
        Refundable refundable = card;   // same object, as Refundable
        System.out.println(payment.pay(500));
        System.out.println(refundable.refund(200));
    }
}
Card paid 500.0
Card refunded 200.0

One object, two types. Through payment you see the Payment methods; through refundable you see the Refundable ones. Both calls reach the same Card.

The rules in one place

You writeAllowed?
class Card implements Paymentyes
class Card implements Payment, Refundableyes, as many as you like
class Card extends Ayes, one parent class
class Card extends A, Bno, only one parent class
new Payment()no, an interface has no code to run

Recap

  • An interface lists methods; a class implements it and writes the logic.
  • Code that asks for the interface works with every class that implements it.
  • A class can implement many interfaces but extend only one class.
  • You never create an interface with new.

Interfaces with a single method power Java's lambdas; see lambdas and functional interfaces.

Keep going in Java · 37 of 79

Previous
Java Abstract Classes: A Parent That Leaves One Blank
Next post
Java Nested, Inner and Anonymous Classes
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Java Classes and Objects: One Blueprint, Many Cars

Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.

Java Constructors and the this Keyword

A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.

Java Instance Methods, Getters, Setters and toString

Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.

Java static: Fields, Methods and Blocks Shared by the Whole Class

A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.

On this page

  • A list of methods to write
  • implements Payment
  • One checkout, any payment
  • One class, two interfaces
  • The rules in one place
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.