Lecture III - Interfaces in java
Lecture III - Interfaces in java
IN
JAVA
04 05 06
● An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.
● An interface cannot contain instance fields. The only fields that can appear in
an interface must be declared both static and final.
import java.lang.*;
}
EXAMPLE OF AN
INTERFACES
/* File name : Animal.java */
interface Animal {
}
IMPLEMENTING
INTERFACES
<p> A class uses the implements keyword to implement an interface.</p>
public class MammalInt implements Animal {
System.out.println("Mammal eats");
System.out.println("Mammal travels");
}
IMPLEMENTING
INTERFACES
public class MammalInt implements Animal {
System.out.println("Mammal eats");
System.out.println("Mammal travels");
}
IMPLEMENTING
INTERFACES
public int noOfLegs() {
return 0;
m.eat();
m.travel();
}
RULES
● A class can extend only one class, but implement many interfaces.
<p> The extends keyword is used to extend an interface, and the child
interface inherits the methods of the parent interface. </p>
}
EXTENDING JAVA INTERFACES
}
EXTENDING JAVA INTERFACES
}
EXTENDING JAVA INTERFACES
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