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

16 Interfaces

Uploaded by

srishreyan10b
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)
19 views18 pages

16 Interfaces

Uploaded by

srishreyan10b
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

Recap
• Why abstraction
• What is abstract class
• What is abstract method?
• Rules for abstract methods
Introduction
• An interface in Java is a blueprint of a class.
– The interface in Java is a mechanism to achieve abstraction.
– There can be only abstract methods in the Java interface, not method body.
– It is used to achieve abstraction and multiple inheritances in Java.
– Interfaces can have abstract methods and variables.
– It cannot have a method body.
– Java Interface also represents the IS-A relationship.
– It cannot be instantiated just like the abstract class.
• Since Java 8, we can have default and static methods in an interface.
• Since Java 9, we can have private methods in an interface.
Introduction
How to declare an interface?
• An interface is declared by using the interface keyword.
• It provides total abstraction; means all the methods in an interface are
declared with the empty body, and all the fields are public, static and final by
default.
• A class that implements an interface must implement all the methods
declared in the interface.

Syntax
interface <interface_name>{
// declare constant fields
// declare methods that abstract by default.
}
Introduction
• The Java compiler adds public and abstract keywords before the
interface method.
• It adds public, static, and final keywords before data members.
Classes and Interfaces
• A class extends another class, an interface extends
another interface, but a class implements an interface.
Classes and Interfaces
interface printable{
void print();
}
class DemoA implements printable{
public void print()
{System.out.println("Hello");}

public static void main(String


args[]){
DemoA obj = new DemoA();
obj.print();
}
}

The Printable interface has only


one method, and its
implementation is provided in the
//Interface declaration: by first user Classes and
interface Drawable{
void draw();
}
Interfaces
//Implementation: by second user
 The Drawable interface has only one
class Rectangle implements Drawable{
method.
public void draw(){
 Its implementation is provided by
System.out.println("drawing rectangle");}
Rectangle and Circle classes.
}  In a real scenario, an interface is
class Circle implements Drawable{ defined by someone else, but its
public void draw(){ implementation is provided by different
System.out.println("drawing circle");} implementation providers.
}  It is used by someone else.
//Using interface: by third user  The implementation part is hidden by
class DemoInterface1{ the user who uses the interface..
public static void main(String
args[]){ Drawable d=new
Circle();
d.draw();
interface Bank{
float rateOfInterest(); Classes and
}
class SBI implements Bank{
Interfaces
public float rateOfInterest(){return
9.15f;}
}
class AXIS implements Bank{
public float rateOfInterest(){return
9.7f;}
}
class DemoInterface2{
public static void main(String[]
args){ Bank b=new SBI();
System.out.println("ROI:
"+b.rateOfInterest());
Multiple Interface
• If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as
multiple inheritance.
interface Printable{
void print();
Multiple Interface
}
interface Showable{
void show();
}
class DemoB implements Printable,Showable{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");}
public static void main(String args[]){
DemoB obj = new DemoB();
obj.print();
obj.show();
}
}
Multiple Interface
Multiple inheritance is not supported through class in
java, but it is possible by an interface, why?
interface Printable{  Multiple inheritance is not supported in the case of
void print(); class because of ambiguity.
}  However, it is supported in case of an interface
interface Showable{ because there is no ambiguity.
void print();  It is because its implementation is provided by the
} implementation class.
class DemoInterface3 implements Printable, Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[])
Printable and Showable interface
{ DemoInterface3 obj = new have same methods, but its
DemoInterface3(); obj.print(); implementation is provided by Class
} DemoInterface3,so there is no
ambiguity.
}
A class implements an interface, but one interface
extends another interface
interface Printable{
void print();
} Interface
Inheritance
interface Showable extends Printable{
void show();
}
class DemoInterface4 implements Showable{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");}
public static void main(String args[]){
DemoInterface4 obj = new
DemoInterface4();
obj.print();
obj.show();
}}
Since Java 8, we can have method body in interface.
But we need to make it default method.

interface Drawable{
void draw();
default void msg(){System.out.println("default method");} Default Method in
}
class Rectangle implements Drawable{
Interface
public void draw(){System.out.println("drawing rectangle");}
}
class DemoInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Static Method in Interface
Since Java 8, we can have static method in interface.
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class DemoInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle(); d.draw();
System.out.println(Drawable.cube(3));
}}
Abstract Class vs. Interface
Practice Program
Write a Java programming to create a banking system with three classes - Bank,
Account, SavingsAccount, and CurrentAccount. The bank should have a list of
accounts and methods for adding them. Accounts should be an interface with
methods to deposit, withdraw, calculate interest, and view balances.
SavingsAccount and CurrentAccount should implement the Account interface
and have their own unique methods.

Write a Java program to create an interface Searchable with a method


search(String keyword) that searches for a given keyword in a text document.
Create two classes Document and WebPage that implement the Searchable
interface and provide their own implementations of the search() method.
• Searchable is an interface with a method
search(String keyword).
• Document and WebPage are classes
implementing the Searchable interface.
• Document has a private attribute content
representing the text content of the
document.
• WebPage has private attributes url
representing the URL of the webpage and
htmlContent representing the HTML content
of the webpage.
• Both Document and WebPage provide
implementations for the search(String
keyword) method.
• The main method in the Main class
demonstrates the usage of Document and
WebPage classes.

You might also like