0% found this document useful (0 votes)
5 views18 pages

Lecture III - Interfaces in java

The document provides an overview of interfaces in Java, explaining their purpose, similarities and differences with classes, and how to declare and implement them. It details the rules for implementing interfaces, including the ability to extend multiple interfaces, and provides examples of interface declarations and implementations. The document concludes with a prompt for questions.

Uploaded by

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

Lecture III - Interfaces in java

The document provides an overview of interfaces in Java, explaining their purpose, similarities and differences with classes, and how to declare and implement them. It details the rules for implementing interfaces, including the ability to extend multiple interfaces, and provides examples of interface declarations and implementations. The document concludes with a prompt for questions.

Uploaded by

davidalieze2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

INTERFACES

IN
JAVA

<p> By DR. AJK </p>


LIST OF OBJECTIVES.
01 02 03

Similarities between Differences between


What are interfaces.
interfaces and classes. interfaces and classes.

04 05 06

Fields in interfaces (final


Methods in interfaces. An Example.
or static).
WHAT ARE INTERFACES?
<p> Java interface is a collection of abstract methods. </p>
The interface is used to achieve abstraction in which you can define methods
without their implementations (without having the body of the methods).

<p> An interface is a reference type and is similar to the class. </p>

<p> Writing an interface is similar to writing a class.</p>

● However, a class describes the attributes and behaviors of an object. An


interface contains behaviors that a class implements. However, a class
describes the attributes and behaviors of an object. An interface contains
behaviors that a class implements.
CONTRASTING JAVA INTERFACES AND
CLASSES
<p> SIMILARITIES </p>
● An interface can contain any number of methods.

● An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.

● The byte code of an interface appears in a .class file.

● Interfaces appear in packages, and their corresponding bytecode file must be in


a directory structure that matches the package name.
CONTRASTING JAVA INTERFACES AND
CLASSES
<p> DIFFERENCES </p>
● You cannot instantiate an interface.

● An interface does not contain any constructors.

● All of the methods in an interface are abstract.

● An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.

● An interface is not extended by a class; it is implemented by a class.

● An interface can extend multiple interfaces.


DECLARING AN
INTERFACES?
<p> The interface keyword is used to declare an interface </p>

/* File name : NameOfInterface.java */

import java.lang.*;

// Any number of import statements

public interface NameOfInterface {

// Any number of final, static fields/attributes

// Any number of abstract method declarations\

}
EXAMPLE OF AN
INTERFACES
/* File name : Animal.java */

interface Animal {

public void eat();

public void travel();

}
IMPLEMENTING
INTERFACES
<p> A class uses the implements keyword to implement an interface.</p>
public class MammalInt implements Animal {

public void eat() {

System.out.println("Mammal eats");

public void travel() {

System.out.println("Mammal travels");

}
IMPLEMENTING
INTERFACES
public class MammalInt implements Animal {

public void eat() {

System.out.println("Mammal eats");

public void travel() {

System.out.println("Mammal travels");

}
IMPLEMENTING
INTERFACES
public int noOfLegs() {

return 0;

public static void main(String args[]) {

MammalInt m = new MammalInt();

m.eat();

m.travel();

}
RULES

<p> Rules for implementing JAVA interfaces</p>

● A class can implement more than one interface at a time.

● A class can extend only one class, but implement many interfaces.

● An interface can extend another interface, in a similar way as a class can


extend another class.
EXTENDING JAVA INTERFACES

<p> The extends keyword is used to extend an interface, and the child
interface inherits the methods of the parent interface. </p>

public interface Sports {

public void setHomeTeam(String name);

public void setVisitingTeam(String name);

}
EXTENDING JAVA INTERFACES

public interface Football extends Sports {

public void homeTeamScored(int points);

public void visitingTeamScored(int points);

public void endOfQuarter(int quarter);

}
EXTENDING JAVA INTERFACES

public interface Hockey extends Sports {

public void homeGoalScored();

public void visitingGoalScored();

public void endOfPeriod(int period);

public void overtimePeriod(int ot);

}
EXTENDING JAVA INTERFACES

public class HockeyDemo implements Hockey {

public void setHomeTeam(String name) {

System.out.println("Home team: " + name);

public void setVisitingTeam(String name) {}

public void homeGoalScored() {}


EXTENDING JAVA INTERFACES

public void visitingGoalScored() {}

public void endOfPeriod(int period) {}

public void overtimePeriod(int ot) {}

public static void main(String[] args) {

Hockey hockeyDemo = new HockeyDemo();

hockeyDemo.setHomeTeam("India");

}
EXTENDING MULTIPLE
INTERFACES?
<p> A Java class can only extend one parent class. Multiple inheritance is not
allowed. </p>
public interface Hockey extends Sports, Event

<p> Suppose we have another interface named event, then a class can implement
multiple interfaces e.g. </p>

interface Event {
public void organize();
}
public class HockeyDemo implements Hockey, Event {
THANK
YOU! …@mail.com
Phone number
website

Do you have any questions?

You might also like