Interface in Java
Interface is similar to class which is collection of public static final variables (constants) and
abstract methods.
The interface is a mechanism to achieve fully abstraction in java. There can be only abstract
methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java.
Why we use Interface?
It is used to achieve fully abstraction.
By using Interface, you can achieve multiple inheritances in java.
Properties of Interface
It is implicitly abstract. So we no need to use the abstract keyword when declaring an
interface.
Each method in an interface is also implicitly abstract, so the abstract keyword is not
needed.
Methods in an interface are implicitly public.
All the data members of interface are implicitly public static final.
Differences between a Class and an Interface:
Class Interface
The keyword used to create a class is The keyword used to create an interface is “in-
“class” terface”
A class can be instantiated i.e, objects of a An Inteface cannot be instantiated i.e, objects
class can be created. cannot be created.
Classes does not support multiple inherit-
ance. Inteface supports multiple inheritance.
It can be inherit another class. It cannot inherit a class.
It can be inherited by a class by using the
It can be inherited by another class using keyword ‘implements’ and it can be inherited
the keyword ‘extends’. by an interface using the keyword ‘extends’.
It can contain constructors. It cannot contain constructors.
It cannot contain abstract methods. It contains abstract methods only.
Class Interface
Variables and methods in a class can be
declared using any access specifier(public, All variables and methods in a interface are de-
private, default, protected) clared as public.
Variables in a class can be static, final or
neither. All variables are static and final.
Behavior of compiler with Interface program
5
In the above image when we compile any interface program, by default compiler added public static
final before any variable and public abstract before any method. Because Interface is design for
fulfill universal requirements and to achieve fully abstraction.
Declaring Interfaces:
The interface keyword is used to declare an interface.
Example
interface Person
{
datatype variablename=value;
//Any number of final, static fields
returntype methodname(list of parameters or no parameters)
//Any number of abstract method declarations
}
Explanations
In the above syntax Interface is a keyword interface name can be user defined name the default
signature of variable is public static final and for method is public abstract. JVM will be added
implicitly public static final before data members and public abstract before method.
Example
public static final datatype variable name=value; ----> for data member
public abstract returntype methodname(parameters)---> for method
Implementing Interfaces:
A class uses the implements keyword to implement an interface. The implements keyword appears
in the class declaration following the extends portion of the declaration.
Example
interface Person
{
void run();
}
class Employee implements Person
{
public void run()
{
System.out.println("Run fast");
}
}
Example of Interface
interface Person
{
void run(); // abstract method
}
class A implements Person
{
public void run()
{
System.out.println("Run fast");
}
public static void main(String args[])
{
A obj = new A();
obj.run();
}
}
Output
Run fast
Multiple Inheritance using interface
Example
interface Developer
{
void disp();
}
interface Manager
{
void show();
}
class Employee implements Developer, Manager
{
public void disp()
{
System.out.println("Hello Good Morning");
}
public void show()
{
System.out.println("How are you ?");
}
public static void main(String args[])
{
Employee obj=new Employee();
obj.disp();
obj.show();
}
}
Output
Hello Good Morning
how are you ?
Program 1:
class super1
{
int l,b;
void setvalue()
{
l=10;
b=5;
}
}
interface Item
{
int h=20;
void display();
}
class sub extends super1 implements Item
{
public void display()
{
System.out.println(" Volume of rect : " + l*b*h);
}
}
class Idemo1
{
public static void main(String args[])
{
sub s = new sub();
s.setvalue();
s.display();
}
}
program 2:
class A
{
void showa()
{
System.out.println(" Class A method");
}
}
interface B
{
void interB();
}
class C extends A implements B
{
public void interB()
{
System.out.println(" interface B method");
}
}
class interdemo
{
public static void main(String args[])
{
C c1=new C();
c1.showa();
c1.interB();
}
program 3:
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
System.out.println("Area Of Rectangle = "+ rect.compute(1,2));
System.out.println("Area Of Triangle = "+ tri.compute(10,2));
}
}
Access Modifier in Java
Java provides a number of access modifiers to set access levels for classes, variables, methods and
constructors. The four access levels are:
Visible to the package. the default. No modifiers are needed.
Visible to the class only (private).
Visible to the world (public).
Visible to the package and all subclasses (protected).
Default Access Modifier - No keyword:
Default access modifier means we do not explicitly declare an access modifier for a class, field,
method, etc.
A variable or method declared without any access control modifier is available to any other class in
the same package. The fields in an interface are implicitly public static final and the methods in an
interface are by default public.
Private Access Modifier - private:
Methods, Variables and Constructors that are declared private can only be accessed within the
declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
Public Access Modifier - public:
A class, method, constructor, interface etc declared public can be accessed from any other class.
Therefore fields, methods, blocks declared inside a public class can be accessed from any class
Protected Access Modifier - protected:
Variables, methods and constructors which are declared protected in a superclass can be accessed
only by the subclasses in other package or any class within the package
Console Input from User using DataInputStream Class
import java.io.DataInputStream;
class ReadDemo
{
public static void main(String args[])
{
DataInputStream di=new DataInputStream(System.in);
int i=0;
String s="Sample";
try
{
System.out.println("Enter any Number :");
i=Integer.parseInt(di.readLine());
System.out.println("Enter any String :");
s=di.readLine();
}
catch(Exception e)
{
}
System.out.println(" Number :" +i);
System.out.println("String :"+s);
}
}