0% found this document useful (0 votes)
8 views11 pages

Java Interfaces Explained: Syntax & Usage

The document explains interfaces in Java, which are collections of abstract methods and constants that define a contract for classes. It outlines the syntax for defining interfaces, the differences between interfaces and abstract classes, and how to implement and extend interfaces. Additionally, it provides examples of creating and using interfaces in Java programs.

Uploaded by

Rainet Max
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)
8 views11 pages

Java Interfaces Explained: Syntax & Usage

The document explains interfaces in Java, which are collections of abstract methods and constants that define a contract for classes. It outlines the syntax for defining interfaces, the differences between interfaces and abstract classes, and how to implement and extend interfaces. Additionally, it provides examples of creating and using interfaces in Java programs.

Uploaded by

Rainet Max
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

Module 3

Interfaces

Athira S Nair, AP,CS


Interfaces in Java
• An interface in Java is a collection of abstract methods (methods
without body) and final fields (constants).
• It specifies a contract that classes must follow.
• Interfaces provide a way to achieve abstraction and multiple
inheritance in Java.

Athira S Nair, AP,CS


Syntax
interface InterfaceName {
//variables declaration

static final type VariableName = value;

// abstract methods (public and abstract by default)


void method1();
void method2();

// (Optional: default and static methods in Java 8+)


default void defaultMethod() {
[Link]("This is a default method");
}

static void staticMethod() {


[Link]("This is a static method"); Athira S Nair, AP,CS
Interfaces vs Abstract Classes
Feature Interface Abstract Class
Only abstract methods (till Java 7);
Can have abstract and non-
Methods Java 8+ allows default & static
abstract methods
methods
Variables Public, static, final (constants) only Instance & static variables allowed
Supports multiple inheritance
Inheritance (class can implement multiple Supports single inheritance
interfaces)
Methods can be public,
Methods are implicitly public
Access Modifiers protected, default, or
abstract
abstract
Interfaces cannot have Abstract classes can have
Constructor
constructors constructors
When you need to define a When classes share common
When to Use contract, and multiple inheritance behavior but also need some
is required abstract methods

Athira S Nair, AP,CS


Defining an Interface
• An interface is basically a kind of class.
• Like classes, interfaces contain methods and variables
but with a major difference.
• The difference is that interfaces define only abstract
methods and final fields.
• This means that interfaces do not specify any code to
implement these methods and data fields contain
only constants.
• Therefore, it is the responsibility of the class that
implements an interface to define the code for
implementation of these methods.
• The syntax for defining an interface is very similar to
that for defining a class

Athira S Nair, AP,CS


Implementing an Interface
class Circle implements Shape {
public void draw() { // must override
[Link]("Drawing Circle");
}
}

class Square implements Shape {


public void draw() {
[Link]("Drawing Square");
}
}

Athira S Nair, AP,CS


Accessing Implementations through Interface
References
• This allows polymorphism: we can call different implementations through a single
reference type (the interface).

public class InterfaceDemo {


public static void main(String[] args) {
Shape s1 = new Circle(); // interface reference
Shape s2 = new Square();

[Link](); // Output: Drawing Circle


[Link](); // Output: Drawing Square
}
}

Athira S Nair, AP,CS


Extending Interfaces
• One interface can
extend another
interface using
extends keyword.
• A class
implementing the
child interface
must implement
all methods from
both parent and
child interfaces.

Athira S Nair, AP,CS


Extending Multiple
Interfaces

• Java supports multiple


inheritance via
interfaces.

Athira S Nair, AP,CS


Create an interface BankAccount with methods deposit() and withdraw().
Implement it in a class SavingsAccount. Write a program to perform
deposit and withdrawal operations.

Athira S Nair, AP,CS


Example questions
• Write a program where an interface MusicPlayer has methods play(),
pause(), and stop(). Implement this in a class MP3Player.
• Define an interface Sports with a method play(). Create two classes
Football and Cricket that implement this interface. Demonstrate their
use in a main program.

Athira S Nair, AP,CS

You might also like