2. • An interface in java is a blueprint of a class. It has
static constants and abstract methods.
• 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 inheritance in
Java.
• In other words, you can say that interfaces can
have methods and variables but the methods
declared in interface contain only method
signature, not body.
3. 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.
4. How to declare interface?
• Interface is declared by using interface keyword. It provides total
abstraction; means all the methods in interface are declared with
empty body and are public and all fields are public, static and final
by default. A class that implement interface must implement all the
methods declared in the interface.
• Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
5. abstract classes may contain non-final variables,
whereas variables in interface are final, public
and static.
// A simple interface
interface Player
{
final int id = 10;
int move();
}
6. // Java program to demonstrate working of
// interface.
import java.io.*;
// A simple interface
interface in1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements interface.
class testClass implements in1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Output:
Geek 10
7. • This is how a class implements an interface. It
has to provide the body of all the methods
that are declared in interface or in other
words you can say that class has to implement
all the methods of interface.
8. interface MyInterface
{
/* compiler will treat them as:
* public abstract void method1();
* public abstract void method2();
*/
public void method1();
public void method2();
}
class Demo implements MyInterface
{
/* This class must have to implement both the abstract methods * else you will get compilation error */
public void method1()
{
System.out.println("implementation of method1");
}
public void method2()
{
System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new Demo(); obj.method1();
}
}
OUTPUT
implementation of method1
9. interface Drawable
{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw()
{
System.out.println("drawing circle");
}
}
//Using interface: by third user
class TestInterface1
{
public static void main(String args[])
{
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}
}
10. Extending Interface
• One interface can inherit another by use of the
keyword extends. The syntax is the same as for
inheriting classes.
• When a class implements an interface that
inherits another interface,
• It must provide implementation of all methods
defined within the interface inheritance.
• Note : Any class that implements an interface
must implement all methods defined by that
interface, including any that inherited from other
interfaces.
11. interface if1
{
void dispi1();
}
interface if2 extends if1
{
void dispi2();
}
class cls1 implements if2
{
public void dispi1()
{
System.out.println("This is display of i1");
}
public void dispi2()
{
System.out.println("This is display of i2");
}
}
public class Ext_iface
{
public static void main(String args[])
{
cls1 c1obj = new cls1();
c1obj.dispi1();
c1obj.dispi2();
}
}
12. Output :
This is display of i1
This is display of i2
Note : We have to define disp1() and disp2() in
cls1.
13. interface Interface1
{
public void f1();
}
//Interface2 extending Interface1
interface Interface2 extends Interface1
{
public void f2();
}
class x implements Interface2
{
//definition of method declared in interfacel
public void f1()
{
System.out.println("Contents of Method f1() in Interface1");
}
public void f2()
{
System.out.println("Contents of Method f2() in Interface2");
}
public void f3()
{
System.out.println("Contents of Method f3() of Class X");
}
}
class ExtendingInterface
{
public static void main(String[] args)
{
Interface2 v2; //Reference variable of Interface2
v2 = new x(); //assign object of class x
v2.f1();
v2.f2();
x xl=new x();
xl.f3();
}
}
14. Multiple inheriance using interface..
class stu
{
int rollno;
String name = new String();
int marks;
stu(int r, String n, int m)
{
rollno = r;
name = n;
marks = m;
}
}
interface i
{
void display();
}
15. class studerived extends stu implements i
{
studerived(int r, String n, int m)
{
super(r,n,m);
}
public void display()
{
System.out.println("Displaying student details .. ");
System.out.println("Rollno = " + rollno);
System.out.println("Name = " + name);
System.out.println("Marks = " + marks);
}
}
public class Multi_inhe_demo
{
public static void main(String args[])
{
studerived obj = new studerived(1912, "Ram", 75);
obj.display();
}
}
16. OBJECT CLONNING
• The object cloning is a way to create exact copy
of an object. The clone() method of Object class
is used to clone an object.
• The java.lang.Cloneable interface must be
implemented by the class whose object clone we
want to create. If we don't implement Cloneable
interface, clone() method generates
CloneNotSupportedException.
• The clone() method is defined in the Object class.
Syntax of the clone() method is as follows:
• protected Object clone() throws CloneNotSuppor
tedException
17. Advantage of Object cloning
• You don't need to write lengthy and repetitive
codes.
• It is the easiest and most efficient way for
copying objects, especially if we are applying it
to an already developed or an old project. Just
define a parent class, implement Cloneable in
it, provide the definition of the clone()
method and the task will be done.
• Clone() is the fastest way to copy array.
18. Disadvantage of Object cloning
• To use the Object.clone() method, we have to
change a lot of syntaxes to our code, like
implementing a Cloneable interface
• We have to implement cloneable interface
while it doesnot have any methods in it.
19. Example of clone() method (Object
cloning)
class Student18 implements Cloneable{
int rollno;
String name;
Student18(int rollno,String name){
this.rollno=rollno;
this.name=name;
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
public static void main(String args[]){
try{
Student18 s1=new Student18(101,"amit");
Student18 s2=(Student18)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}catch(CloneNotSupportedException c){}
}
}