Java Programming – Interface
Interface
Java Programming – Interface
Why Interface?
• It provides abstraction.
• It provides loose coupling.
Java Programming – Interface
How to declare interface?
public abstract interface MyInterface{
public void display();
// public abstract void display(); Using the
interface
}
class Impl implements MyInterface{
public void display(){
[Link]("My own print");
}
}
Java Programming – Interface
Example
interface Modem { public class HuaweiModem implements Modem {
public boolean open(); public boolean open() { // implementation }
public boolean close(); public boolean close() { // implementation }
public int read() { // implementation }
public int read ();
public int write(byte[] buffer) { // implementati
public int write(byte[] buffer); on }
} }
public class MindStickModem implements Modem {
public boolean open() { // implementation }
public boolean close() { // implementation }
public int read() { // implementation }
public int write(byte[] buffer) { // implementati
on }
}
Modem modem = new MindStickModem Modem modem = new HuaweiModem();
(); [Link]();
[Link](); [Link](buffer);
[Link](buffer); [Link]();
[Link](); [Link]();
Java Programming – Interface
Point to remember
• A traditional interface as a 100 percent abstract class
Java Programming – Interface
Rules to declare the interface
Java Programming – Interface
Interfaces have the following properties
Java Programming – Interface
Does that mean I can not extend a
class?
• No, you can
interface MyInterface{
public void display();
}
class Impl extends MyClass implements MyInterface{
public void display(){
[Link]("My own print");
}
}
Java Programming – Interface
Example of loose coupling
interface Animal { class Dog implements Animal {
void child(); public void child() {
} [Link]("puppy");
class Cat implements Animal { }
public void child() { }
[Link]("kitten") public class LooseCoupling{
;
public static void main(String args[]
} ) {
} Animal obj = new Cat();
[Link]();
}
}
Java Programming – Interface
When overriding methods defined in
interfaces, there are several rules to be
followed
Java Programming – Interface
While implementing and interfaces, there
are several rules
Java Programming – Interface
Extending the interface
public interface Sports {
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
public interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
public interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
Java Programming – Interface
Can class implement multiple interface?
public class IceHockey implements Hockey {
}
Java Programming – Interface
Tagging Interfaces
interface DIV1{ }
interface DIV2{ }
class Demo implements DIV1{ // you have implemented something }
public class Main{
public static void main(String argds[]){
Demo d = new Demo();
if (d instanceof DIV1){
[Link]("DIV1");
} else if (d instanceof DIV2){
[Link]("DIV2");
}
}
}
Java Programming – Interface
Can I have data in interface?
Constant
interface MyInterface{
int interfaceConst = 20;
// public static final int interfaceConst = 20 ;
}
Java Programming – Interface
Static and default method Example
interface MyInterface {
static void hello() {
[Link]("Hello, New Static Method Here"); }
default void newMethod() {
[Link]("Newly added default method"); } }
class Demo implements MyInterface {
public static void main(String argds[]) {
Demo d = new Demo();
[Link]();
[Link]();
}
}
Java Programming – Interface
Declaring default Interface
Methods
Java Programming – Interface
Problem with default method
interface InterfaceA {
default void defaultMethod(){
[Link]("Interface A default method"); } }
interface InterfaceB {
default void defaultMethod(){
[Link]("Interface B default method"); } }
public class Impl implements InterfaceA, InterfaceB {
public static void main(String argds[]){
new Impl().defaultMethod();
} }
Duplicate default methods named defaultMethod with the
parameters () and () are inherited from the types InterfaceB and
InterfaceA
Java Programming – Interface
How to solver the issue?
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
[Link]();
}
}
OR
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){ }
}
Java Programming – Interface
Declaring static Interface Methods
Java Programming – Interface
Declaring static Interface Methods
interface NewInterface {
static void hello()
{
[Link]("Hello, New Static Method Here");
} }
public class InterfaceDemo implements NewInterface {
public static void main(String[] args)
{
InterfaceDemo interfaceDemo = new InterfaceDemo();
[Link]();
} }
Java Programming – Interface
An interface is similar to a class in the
following ways
Java Programming – Interface
An interface is different from a class in
several ways, including
Java Programming – Interface
The interface can have only - Constants
interface MyInterface{
int myData = 40;
}
class Subclass implements MyInterface{
Subclass(){ int myData = 40
myData=30; The final field [Link] cannot be
assigned
}
}
Java Programming – Interface
How to declare Constant in java?
• Any data declared with public static final is called constant
• Example
• static final int DAYS_IN_WEEK = 7;
• static final int integerConstant = 20;
• static final String stringConstant = "hello";
• static final float floatConstant = 1654.22f;
• static final char characterConstant = 'C';
Java Programming – Interface
Constants in interface
• public int x = 1;
• int x = 1;
• static int x = 1;
• final int x = 1;
• public static int x = 1;
• public final int x = 1;
• public static final int x = 1;