Interface and Package
Interface and Package
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.
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.
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.
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
Example
interface Developer
{
void disp();
}
interface Manager
{
void 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));
}
}
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
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);
}
}