08 Interface

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

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(){
System.out.println("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();
(); modem.open();
modem.open(); modem.write(buffer);
modem.write(buffer); modem.read();
modem.read(); modem.close();
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(){
System.out.println("My own print");
}
}
Java Programming – Interface

Example of loose coupling


interface Animal { class Dog implements Animal {
void child(); public void child() {
} System.out.println("puppy");
class Cat implements Animal { }
public void child() { }
System.out.println("kitten") public class LooseCoupling{
;
public static void main(String args[]
} ) {
} Animal obj = new Cat();
obj.child();
}
}
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){
System.out.println("DIV1");
} else if (d instanceof DIV2){
System.out.println("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() {
System.out.println("Hello, New Static Method Here"); }

default void newMethod() {


System.out.println("Newly added default method"); } }

class Demo implements MyInterface {


public static void main(String argds[]) {
Demo d = new Demo();
MyInterface.hello();
d.newMethod();
}
}
Java Programming – Interface

Declaring default Interface


Methods
Java Programming – Interface

Problem with default method


interface InterfaceA {
default void defaultMethod(){
System.out.println("Interface A default method"); } }
interface InterfaceB {
default void defaultMethod(){
System.out.println("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(){
InterfaceA.super.defaultMethod();
}
}
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()
{
System.out.println("Hello, New Static Method Here");
} }

public class InterfaceDemo implements NewInterface {


public static void main(String[] args)
{
InterfaceDemo interfaceDemo = new InterfaceDemo();
NewInterface.hello();
} }
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 MyInterface.myData 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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy