0% found this document useful (0 votes)
3 views

Module 2_4

An interface in Java is a blueprint that contains abstract methods and static constants, used to achieve abstraction and multiple inheritance. It cannot be instantiated and has properties such as being implicitly abstract and containing only abstract methods. Java 8 introduced default and static methods in interfaces, and interfaces can also extend other interfaces, supporting multiple inheritance.

Uploaded by

hwassignment126
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)
3 views

Module 2_4

An interface in Java is a blueprint that contains abstract methods and static constants, used to achieve abstraction and multiple inheritance. It cannot be instantiated and has properties such as being implicitly abstract and containing only abstract methods. Java 8 introduced default and static methods in interfaces, and interfaces can also extend other interfaces, supporting multiple inheritance.

Uploaded by

hwassignment126
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/ 24

Interface

Interface in Java
▷ An interface in Java is a blueprint of ▷ Java Interface also represents
a class. It has static constants and the IS-A relationship.
abstract methods.
▷ The interface in Java is a mechanism
▷ It cannot be instantiated just
to achieve abstraction. There can be
only abstract methods in the Java like the abstract class.
interface, not method body.
▷ It is used to achieve abstraction and ▷ Writing an interface is similar
multiple inheritance in Java. to writing a class. But a class
▷ In other words, interfaces can have describes the attributes and
abstract methods and variables. It behaviors of an object. And
cannot have a method body. an interface contains
behaviors that a class
implements.

2
Why use Java interface?
There are mainly three
reasons to use interface.
They are given below.

▷ It is used to achieve
abstraction.
▷ By interface, we can
support the
functionality of multiple
inheritance.
▷ It can be used to
achieve loose coupling.
3
Interface and Class
SIMILARITY DIFFERENCE

▷ An interface can contain any number of


▷ You cannot instantiate an interface.
methods.
▷ An interface does not contain any
▷ An interface is written in a file with
constructors.
a .java extension, with the name of the
interface matching the name of the ▷ All of the methods in an interface are
file. abstract.
▷ The byte code of an interface appears ▷ An interface cannot contain instance
in a .class file. fields. The only fields that can appear in
an interface must be declared both static
▷ Interfaces appear in packages, and
and final.
their corresponding byte code file must
be in a directory structure that ▷ An interface is not extended by a class; it
matches the package name. is implemented by a class.
▷ An interface can extend multiple
interfaces.
4
Declaring Interfaces
Interfaces have the following
properties −
/* File name : NameOfInterface.java */
import java.lang.*;
▷ An interface is implicitly abstract.
// Any number of import statements
You do not need to use the abstract
keyword while declaring an
public interface NameOfInterface interface.
{
// Any number of final, static fields ▷ Each method in an interface is also
// Any number of abstract method implicitly abstract, so the abstract
declarations keyword is not needed.
}
▷ Methods in an interface are
implicitly public.

5
Java Interface Example
interface printable { ▷ When a class implements an
void print(); interface, you can think of the
class as signing a contract,
} agreeing to perform the
class A6 implements printable { specific behaviors of the
interface.
public void print()
▷ If a class does not perform all
{ System.out.println("Hello"); the behaviors of the interface,
} the class must declare itself as
abstract.
public static void main(String
args[]) {
▷ A class uses the implements
A6 obj = new A6(); Output: keyword to implement an
obj.print(); Hello interface.
}
} 6
Java Interface Example:
Bank
interface Bank{
float rateOfInterest();
▷ Output:
} ▷ ROI: 9.15
class SBI implements Bank{
public float rateOfInterest() { return
9.15f; }
}
class PNB implements Bank{
public float rateOfInterest() { return
9.7f; }
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI:
"+b.rateOfInterest());
}} 7
Multiple inheritance in Java by
interface
▷ If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as
multiple inheritance.

8
Example
interface Printable{ public static void
void print(); main(String args[])
} {
interface Showable{
void show(); A7 obj = new A7();
} obj.print();
class A7 implements
obj.show();
Printable,Showable
{ }
public void print() }
{
System.out.println("Hello"); ▷ Output:
}
public void show() { ▷ Hello Welcome
System.out.println("Welcome");
}

9
Interface inheritance
interface Printable
{ public static void main(String
void print(); args[])
} {
interface Showable extends
TestInterface4 obj = new
Printable{
TestInterface4();
void show();
} obj.print();
obj.show();
class TestInterface4 implements
}
Showable
{ }
public void print()
{
System.out.println("Hello"); Output:
}
public void show() Hello
{
System.out.println("Welcome");
}
Welcome 10
Java 8 Default Method in Interface
▷ Since Java 8, we can have class TestInterfaceDefault
method body in interface. But
we need to make it default {
method. public static void main(String args[])
{
interface Drawable{
Drawable d=new Rectangle();
void draw();
default void msg() d.draw();
{ d.msg();
System.out.println("default
}
method");
} }
}
class Rectangle implements
Output:
Drawable{ drawing rectangle
public void draw()
{ default method
System.out.println("drawing
rectangle"); 11
}
Java 8 Static Method in
Interface
▷ Since Java 8, we can class TestInterfaceStatic
{
have static method in
public static void main(String args[])
interface
{
Drawable d=new Rectangle();
interface Drawable{
void draw(); d.draw();
static int cube(int x) System.out.println(Drawable.cube(3));
{
}
return x*x*x;
} }
}
class Rectangle implements
Output:
Drawable drawing rectangle
{
public void draw() 27
{
System.out.println("drawing
rectangle"); 12
Tagging Interfaces
▷ An interface with no methods in
it is referred to as a tagging
interface.
▷ Example:
package java.util;

public interface EventListener {}

public interface Serializable{ }

13
Nested Interface in Java
▷ An interface can have ▷ Nested interface must be public if it is
another interface which is declared inside the interface but it can
known as a nested interface. have any access modifier if declared
▷ The nested interfaces are within the class.
used to group related ▷ Nested interfaces are declared static
interfaces so that they can implicitly.
be easy to maintain.
▷ The nested interface must be
referred by the outer
interface or class.
▷ It can't be accessed directly.

14
SYNTAX
▷ Interface with in ▷ Interface with in class
interface
interface interfacename class classname
{ {
... ...
interface interface
nestedinterfacename nestedinterfacename
{ {
... ...
} }
} } 15
Example of nested interface which is
declared within the interface
interface Showable{
void show(); public static void main(String args[])
interface Message{ {
void msg();
Showable.Message message =new
}
TestNestedInterface1(); //upcasting here
}
class TestNestedInterface1 message.msg();
implements Showable.Message { }
public void msg()
}
{
System.out.println("Hello nested
interface");
} Output:
}
hello nested interface
16
Example of nested interface
which is declared within the
class
public static void main(String args[]){
class A{
interface Message{ A.Message message=new
void msg(); TestNestedInterface2();//upcasting here
} message.msg();
} }
class TestNestedInterface2 }
implements A.Message
{
public void msg() Output:
{
System.out.println("Hello nested hello nested interface
interface");
}

17
Example : Interface and
class

interface A class MainClass


{ {
void myMethod(); public static void
} main(String[] args)
{
class B A a = new C();
{
public void myMethod() a.myMethod();
{ }
System.out.println("My }
Method");
}
} ▷ OUTPUT
▷ My Method
class C extends B implements A
{
18
}
Example:
class Main implements A,B
interface A {
{ public static void Main(String args[])
int x=10; {
} /* reference to x is ambiguous both
variables are x
interface B
* so we are using interface name to
{
resolve the
int x=100;
* variable
}
*/
System.out.println(x); // Error
System.out.println(A.x);
System.out.println(B.x);
}
} 19
Difference between abstract
class and interface
▷ Abstract class and ▷ Simply, abstract class achieves
interface both are used partial abstraction (0 to 100%)
to achieve abstraction whereas interface achieves fully
where we can declare abstraction (100%).
the abstract methods.

▷ Abstract class and


interface both can't be
instantiated.

20
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since Java
abstract methods. 8, it can have default and static methods also.

2) Abstract class doesn't support multiple Interface supports multiple inheritance.


inheritance.
3) Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare interface.
class.
6) An abstract class can extend another Java class and An interface can extend another Java interface only.
implement multiple Java interfaces.

7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
21
Example of abstract class and interface in
Java
//Creating interface that has 4 //Creating subclass of abstract class, now
methods we need to provide the implementation of
interface A{ rest of the methods
void a(); //bydefault, public and class M extends B{
abstract public void a(){System.out.println("I am
void b(); a");}
void c(); public void b(){System.out.println("I am
void d(); b");}
} public void d(){System.out.println("I am
d");}
//Creating abstract class that }
provides the implementation of
one method of A interface //Creating a test class that calls the
abstract class B implements A{ methods of A interface
Output:
public void c() class Test5{
{ public static void main(String
I am a args[]){
System.out.println("I am C"); A a=new M(); I am b
} a.a(); I am c
} a.b(); I am d
a.c();
22
a.d();
Example
interface Arithmetic
public void comparison()
{
{
void operations();
if (n1>n2)
}
System.out.println("n1 is grerater than n2");
interface Relational
else if (n1<n2)
{
System.out.println("n1 is less than n2");
void comparison();
else
System.out.println("n1 is equal n2");
}
class sample implements Arithmetic,
}
Relational
}
{
int n1,n2;
class Main{
sample(int a,int b)
public static void main(String args[])
{
{
n1=a;
sample s=new sample( 34,56);
n2=b;
s.operations();
}
s.comparison(); Addition :90
public void operations()
} subtraction:-22
{
} multiplication :1904
System.out.println( "Addition :"+
(n1+n2)); Division :0.60714287
System.out.println( "subtraction :"+ n1 is less than n2
(n1-n2)); 23
System.out.println( "multiplication :"+
THANKS

24

You might also like