4. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L6S2
Functional Interface is an interface that
has only pure one abstract method.
It can have any number of static and
default methods and also even public
methods of java.lang.Object classes
1.Functional Interface
5. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
Examples of Functional Interfaces:
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L6S3
When an interface contains only one abstract
method, then it is known as a Functional
Interface.
Examples of Functional Interfaces:
Runnable : It contains only run() method
ActionListener : It contains
only actionPerformed()
ItemListener : It contains
only itemStateChanged() method
6. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L6S4
2. Marker Interface:
An interface that does not contain any
methods, fields, Abstract Methods, and any
Constants is Called a Marker interface.
Also, if an interface is empty, then it is known
as Marker Interface.
The Serializable and the Cloneable interfaces
are examples of Marker interfaces.
7. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L5S5
1. Cloneable Interface
A cloneable interface in Java is also a Marker
interface that belongs to java.lang packages.
It generates a replica(copy) of an object with a
different name. Therefore we can implement
the interface in the class of which class object
is to be cloned.
It implements the clone() method of the
Object class to it.
import java.lang.Cloneable;
class abc implements Cloneable
{int x;
String y;
public abc(int x,String y)
{ this.x = x;
this.y = y; }
protected Object clone()
throws CloneNotSupportedException
{ return
super.clone();}
}
public class Test
{ public static void main(String[] args)
throws
CloneNotSupportedException
{
abc p = new abc(10, "We Are Reading GFG Now");
abc q = (abc)p.clone();
System.out.println(q.x);
System.out.println(q.y)
}}
8. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L5S6
2. Serializable Interface:
It is a marker interface in Java that is defined in the java.io package. If we want
to make the class serializable, we must implement the Serializable interface. If a
class implements the Serializable interface, we can serialize or deserialize the
state of an object of that class.
Serialization is a mechanism in which our object state is ready from memory and
written into a file or from the databases.
Deserialization- is the opposite of serialization means that object state reading
from a file or database and written back into memory is called deserialization of
an object.
9. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L5S7
import java.io.*;
class A implements Serializable {
int i;
String s;
public A(int i, String s)
{
this.i = i;
this.s = s;
}
}public class Test {
public static void main(String[] args)
throws IOException, ClassNotFoundException
{
A a = new A(20, "GeeksForGeeks");
FileOutputStream fos
= new FileOutputStream("xyz.txt");
ObjectOutputStream oos
= new ObjectOutputStream(fos);
oos.writeObject(a);
FileInputStream fis
= new FileInputStream("xyz.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
A b = (A)ois.readObject(); // down-casting object
System.out.println(b.i + " " + b.s);
oos.close();
ois.close();
}
}
Serializable Interface Example:
10. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L5S8
3. Remote Interface:
A remote interface is a marker interface that belongs
to java.rmi package. It marks an object as a remote that can be
accessed from the host of another machine.
We need to implement the Remote interface if we want to make an
object remote then. Therefore, It identifies the interface.
A remote interface serves to identify interfaces whose methods may
be invoked from a non-local virtual machine. Any object that is a
remote object must directly or indirectly implement this interface.
The remote interface is an interface that declares the set of methods
that will be invoked from a remote Java Virtual Machine, i.e.(JVM
11. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
LESSON 5:- INTERFACE BASICS
UNIT 3
ARRAY,STRINGS,INTERFACES ,
PACKAGES & EXCEPTION
HANDLING
P4UGCA1932U3L5S9
3. Remote
Interface Example:
1.Create an interface that extends the
predefined interface Remote which belongs to
the package or, implement the Remote
interface with the class, which you need to
make remote.
2.Declare all the business methods that can
be invoked by the client in this interface.
3.Since there is a chance of network issues
during remote calls, an exception named
RemoteException may occur; throw it.
12. Sector 54, Chandigarh, Phase-2,
Mohali - PB
ASPIRE. ACHIEVE.
ASCEND
Quiz on lesson
RECAPITULATION
Answers of quiz
A1)Functional,marker,cloneable,serializable
A2) Functional Interface is an interface that
has only pure one abstract method.
A3) An interface that does not contain any methods, fields,
Abstract Methods, and any Constants is Called a Marker
interface
A4) Serialization is a mechanism in which our object state is
ready from memory and written into a file or from the
databases.
1.What are different types of interfaces?
2.What is functional interface?
3.What is marker interface?
4..What is serializable interface?
LESSON 2 :-JAVA INPUT & OUTPUT
UNIT 1. JAVA FUNDAMENTALS &
ESSENTIALS
P4UGCA1932U3L1S8
Different types of Interfaces
Functional Interface
Examples of Functional Interface
Marker Interface
Cloneable Interface
Serializable Interface
Serializable interface Example
Remote Interface
Remote Interface example