Interface in Java[1]
Interface in Java[1]
An interface in Java is syntactically similar to a class but can have only abstract methods declaration and constants as members.
In other words, an interface is a collection of abstract methods and constants (i.e. static and final fields). It is used to achieve
complete abstraction.
Every interface in java is abstract by default. So, it is not compulsory to write abstract keyword with an interface.
Once an interface is defined, we can create any number of separate
classes and can provide their own implementation for all the abstract
methods defined by an interface.
A class that implements an interface is called implementation class. A class can implement any number of interfaces in Java.
Every implementation class can have its own implementation for abstract methods specified in the interface
Since the implementation classes will have all the methods with a body, it is possible to create an instance of implementation classes
Why do we use Interface?
There are mainly five reasons or purposes of using an interface in Java. They are as follows:
1.In industry, architect-level people create interfaces, and then it is
given to developers for writing classes by implementing interfaces provided.
2. Using interfaces is the best way to expose our project’s API to some other projects. In other words, we can provide interface
methods to the third-party vendors for their implementation.
3. Programmers use interface to customize features of software differently for different objects.
4. It is used to achieve full abstraction in java.
5. By using interfaces, we can achieve the functionality of multiple inheritance.
interface A
{
int x = 10;
int y = 20;
}
interface B extends A
{
void show();
interface A
{
int x = 20;
int y = 30;
}
interface B extends A
{
void show();
}
interface C extends A, B
{
. . . . . . . .
}
Key points:
1. An interface cannot extend classes because it would violate rules that
an interface can have only abstract methods and constants.
2. An interface can extend Interface1, Interface2.
package interfacePrograms;
public interface ConstantValues
{
// Declaration of interface variables.
int x = 20;
int y = 30;
}
public class Add implements ConstantValues
{
int a = x;
int b = y;
void m1()
{
System.out.println("Value of a: " +a);
System.out.println("Value of b: " +b);
}
void sum()
{
int s = x + y;
System.out.println("Sum: " +s);
}
}
public class Sub implements ConstantValues
{
void sub()
{
int p = y - x;
System.out.println("Sub: " +p);
}
}
public class Main
{
public static void main(String[] args)
{
Add a = new Add();
a.m1();
a.sum();
Sub s = new Sub();
s.sub();
}
}
package interfacePrograms;
public interface A
{
void msg(); // No body.
}
public class B implements A
{
// Override method declared in interface.
public void msg()
{
System.out.println("Hello Java");
}
void show()
{
System.out.println("Welcome you");
}
public static void main(String[] args)
{
B b = new B();
b.msg();
b.show(); // A reference of interface is pointing to objects of class B.
A a = new B();
a.msg();
// a.show(); // Compile-time error because a reference of interface can only
//call methods declared in it and implemented by implementing class.
package interfacePrograms;
public interface Rectangle
{
void calc(int l, int b); // no body.
}
public class RecArea implements Rectangle {
public void calc(int l, int b)
{
int area = l * b; // Implementation.
System.out.println("Area of rectangle = "+area);
}
}
public class RecPer implements Rectangle {
public void calc(int l, int b)
{
int perimeter = 2 * (l + b); // Implementation.
System.out.println("Perimeter of rectangle = "+perimeter);
}
}
public class Execution {
public static void main(String[] args)
{
Rectangle rc; // Creating an interface reference.
rc = new RecArea(); // Creating object of RecArea.
rc.calc(20, 30); // calling method.
rc = new RecPer(); // Creating an object of RecPer.
rc.calc(20, 30);
}
}
Multilevel Inheritance by Interface
package interfacePrograms;
public interface Continent
{
void showContinent();
}
public interface Country extends Continent
{
void showCountry();
}
public interface State extends Country
{
void showState();
}
public class City implements State
{
public void showContinent()
{
System.out.println("Asia");
}
public void showCountry()
{
System.out.println("India");
}
public void showState()
{
System.out.println("Jharkhand");
}
void showCity()
{
System.out.println("Dhanbad");
}
public static void main(String[] args)
{
City c = new City();
c.showContinent();
c.showCountry();
c.showState();
c.showCity();
}
}
In Java, Multiple Inheritance is not supported through Class, but it is possible by Interface. Why?
If two superclasses have the same method name, then which method is inherited into subclass is the main confusion in multiple
inheritance.
That’s why Java does not support multiple inheritance in the case of class. But, it is supported through an interface because there is
no confusion. This is because its implementation is provided by the implementation class.
package multipleInheritancebyInterface;
public interface AA
{
void m1();
}
public interface BB
{
void m1();
}
public class Myclass implements AA, BB
{
public void m1()
{
System.out.println("Hello Java");
}
public static void main(String[] args)
{
Myclass mc = new Myclass();
mc.m1();
}
}
package interfaceProgram;
int x = 20; // Interface variable must be initialized at the time of declaration. By default, interfa
ce variable is public, static, and final.
public interface BB
{
int y = 20;
void m2();
}
Serialization In Java
Serialization can be defined as a process by which we convert the object state into its equivalent byte stream to store the object into
the memory in a file or persist the object.
When we want to retrieve the object from its saved state and access its contents, we will have to convert the byte stream back to the
actual Java object and this process is called deserialization.
import java.io.*;
import java.io.Serializable;
} catch (Exception e) {
System.out.println(e);
}
}
package interfaceArea;
import java.io.*;
import java.io.Serializable;
} catch (Exception e) {
System.out.println(e);
}
}
Java.io.NotSerializableException In Java
The exception of java.io.NotSerializableException is an exception that is thrown when the class is not eligible for serialization. The
class that does not implement the Serializable interface becomes
ineligible for serializatio
package interfaceArea;
import java.io.*;
public class Student{
transient int id;
String name;
} catch (Exception e) {
System.out.println(e);
}
}
package interfaceArea;
// class constructor
Student2(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
// clone method
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
} catch (CloneNotSupportedException c) {
System.out.println(c);
}