0% found this document useful (0 votes)
4 views15 pages

Java Interfaces

This document provides a beginner-friendly guide to Java interfaces, explaining their purpose as contracts that define what a class must do without detailing how to do it. It covers key concepts such as declaring, implementing, and extending interfaces, along with practical examples, including a payment system. The document emphasizes the benefits of using interfaces for flexible design and loose coupling in Java programming.

Uploaded by

knvpkphd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Java Interfaces

This document provides a beginner-friendly guide to Java interfaces, explaining their purpose as contracts that define what a class must do without detailing how to do it. It covers key concepts such as declaring, implementing, and extending interfaces, along with practical examples, including a payment system. The document emphasizes the benefits of using interfaces for flexible design and loose coupling in Java programming.

Uploaded by

knvpkphd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PROGRAMMING

Java Interfaces
Declaring • Implementing • Extending

A beginner-friendly guide to understanding one of Java's


most powerful design features

PPT 1 | Java Interfaces


Learning Objectives 2 / 15

By the end of this lesson, 🎯 What You'll Build


you will be able to:
A complete Payment System using Java Interfaces — from
1 Understand what an Interface is design to working code.

2 Learn how to declare an interface ✓ Interface declaration syntax

✓ Multiple interface implementation


3 Implement interfaces in classes

✓ Interface extension chain


4 Extend multiple interfaces
✓ Real-world design patterns
5 Apply interfaces in real-world design

No prior Java knowledge required!

Java Interfaces | Beginner-Friendly Learning Series


What is an Interface? 3 / 15

A contract that defines WHAT to do — not HOW to do it.

{} π ▶ ⚙
Abstract Methods Constants Default Methods Static Methods

Methods without a body — must Fixed values shared across all Ready-made methods with code Utility methods inside the interface
be implemented implementations (Java 8+) (Java 8+)

⚠ Cannot be instantiated (no 'new Interface()') ✓ Enables multiple inheritance of type

Java Interfaces | Beginner-Friendly Learning Series


Real-Life Analogy: Interface = Job Contract 4 / 15

Think of an Interface like a Job Contract between an employer and employee.

Real World Java World

Job Description Interface

Required Skills listed Abstract Methods

Employee Class

Signing the Contract implements keyword

One Employee, Multiple Jobs Multiple Interfaces

💡 One class can implement MULTIPLE interfaces — just like one person can have multiple job roles!
Java Interfaces | Beginner-Friendly Learning Series
Declaring an Interface – Basic Syntax 5 / 15

◉ ◉ ◉ Java Code
KEYWORD 'interface' declares the contract
public interface Shape {

// Constant (public static final by default)


int MAX_SIDES = 10; CONSTANT Fields are public static final by default

// Abstract method — no body!


void draw(); ABSTRACT No body {} — must be implemented elsewhere

// Default method — has a body (Java 8+)


default void info() {
[Link]("This is a shape");
} DEFAULT Has a body — optional to override

// Static method (Java 8+)


static void category() {
[Link]("Geometry");
} STATIC Called via Interface name, e.g. [Link]()
}

Java Interfaces | Beginner-Friendly Learning Series


Key Rules of Interfaces 6 / 15

Methods are public abstract by default


1
Even if you don't write 'public abstract', Java adds it automatically.

Fields are public static final


2
All variables in an interface are constants — they cannot be changed.

Default & Static methods added in Java 8


3
Before Java 8, only abstract methods were allowed.

Cannot create an object of an interface


4
You cannot write: Shape s = new Shape(); — it will throw an error.

Java Interfaces | Beginner-Friendly Learning Series


Implementing an Interface – Basic 7 / 15

A class uses the 'implements' keyword to sign the contract.

◉ ◉ ◉ Java Code What happens here?


class Circle implements Shape {
'Circle' agrees to the Shape contract
@Override
public void draw() {
[Link]("Drawing Circle"); @Override tells Java we're filling in the abstract
} method
}

draw() now has a real body with code

Output: Drawing Circle

⚠ Must implement ALL abstract methods or use 'abstract class' ✓ Default methods are optional — you don't have to override them

Java Interfaces | Beginner-Friendly Learning Series


Implementing Multiple Interfaces 8 / 15

One class can fulfill more than one contract at the same time.
◉ ◉ ◉ Java Code

interface Printable { «interface» «interface»


void print(); Shape Printable
}

class Report implements Shape, Printable {

public void draw() {


[Link]("Drawing Report");
} class Report
implements Shape, Printable
public void print() {
[Link]("Printing Report");
}
} Both methods must
be implemented ✓

Use comma (,) to separate


multiple interfaces

Java Interfaces | Beginner-Friendly Learning Series


Extending Interfaces 9 / 15

Interfaces can also inherit from other interfaces using 'extends'.


◉ ◉ ◉ Java Code

interface A { Interface A Interface B


void methodA(); methodA() methodB()
}

interface B {
void methodB();
}

// C inherits from BOTH A and B


Interface C (extends A, B)
interface C extends A, B { methodA() methodB() methodC()
void methodC();
}

Any class implementing C


must implement ALL 3 methods

⚠ Key Difference: Classes use 'implements' | Interfaces use 'extends'


Java Interfaces | Beginner-Friendly Learning Series
Real-World Use of Interfaces 10 / 15

Java's entire ecosystem is built on interfaces. Here are 4 you'll use daily:

Java Collections JDBC – Database

List, Set, Map Connection, Statement

Sorting Multithreading

Comparable & Comparator Runnable

Why use Interfaces?


› Loose coupling — classes don't depend on each other
› Flexible design — swap implementations easily
› Easy to replace — change code without breaking other parts

Java Interfaces | Beginner-Friendly Learning Series


Interface Hierarchy – Visual Diagram 11 / 15

Interfaces stack like layers. Each level adds more requirements.

Interface A
methodA()

Interface B extends A
methodA() + methodB()

Class C implements B
implements methodA() + methodB()

💡 "Contracts build layers" — each interface adds more responsibilities to fulfill.


Java Interfaces | Beginner-Friendly Learning Series
Practice Problem – Payment System 12 / 15

Problem Statement
Design a payment system using interfaces.
› Payment interface → pay(double amount)
› Refundable interface → refund(double amount)
› Class CreditCardPayment implements both

📥 Input 📤 Expected Output


Pay: 500 Payment of 500.0 successful
Refund: 200 Refund of 200.0 successful

💡 Think before coding:


What interfaces do we need? What methods must each class implement? How do we call them?

Java Interfaces | Beginner-Friendly Learning Series


Dry Run – Step-by-Step Execution 13 / 15

Let's trace exactly what happens when the code runs.

Object Created
01 Both interfaces are now 'signed'
CreditCardPayment obj = new CreditCardPayment()

pay() Called
02 Output: Payment of 500.0 successful
[Link](500);

refund() Called
03 Output: Refund of 200.0 successful
[Link](200);

Contracts Fulfilled
04 ✓ Program complete — no errors
Both Payment and Refundable satisfied

Java Interfaces | Beginner-Friendly Learning Series


Solution Code – Payment System 14 / 15

◉ ◉ ◉ Java Code Line by Line


interface Payment {
void pay(double amount); Lines 1–3 Payment interface — defines pay()
}

interface Refundable { Refundable interface — defines


Lines 5–7
void refund(double amount); refund()
}

class CreditCardPayment implements Payment, Refundable { Line 9 Class signs BOTH contracts

public void pay(double amount) {


[Link]("Payment of " + amount + "
successful"); Lines 11–13 pay() — prints payment message
}

public void refund(double amount) {


Lines 15–17 refund() — prints refund message
[Link]("Refund of " + amount + "
successful");
}
} Output Payment of 500.0 successful
Refund of 200.0 successful

Java Interfaces | Beginner-Friendly Learning Series


Summary
Java Interfaces — Key Takeaways

Interface = Contract

Defines WHAT a class must do, not HOW

implements keyword

Used when a class follows an interface

extends keyword

Used when one interface inherits from another

Multiple Inheritance Clean Architecture


④ ⑤
One class can implement many interfaces Promotes loose coupling and flexible design

Java Interfaces | PPT 1 | Beginner-Friendly Learning Series

You might also like