OOPJ Notes 2 2
OOPJ Notes 2 2
CHAPTER -2
INTERFACES
Contents
Introduction to interfaces
Defining an Interface
Implementing interfaces
Nested intefaces
Variables in interfaces
Extending interfaces
Default Interface Methods
Static Methods in an Interface
References
INTRODUCTION TO INTERFACES
Interface: In Java, an interface is a blueprint of a class. It has abstract methods and static final
variables.
Defining an interface
General form of an inheritance
Implementing interfaces
access_modifier class implements intefaceName{
interface_method1(parameter list){
}
Interface_method2(parameter list){
}
}
If a class implements more than one interface, the interfaces are separated by (,)
If a class implements two interfaces that declare the same method, then the same
method will be used by clients of either interface.
The methods that implement an interface must be declared public.
/* Program implementing interface*/
interface GeoShapes{
double PI=22/7;
double area();
}
class Rectangle3 implements GeoShapes{
double dim1,dim2;
Rectangle3(double x, double y){
dim1=x;
dim2=y;
}
public double area() {
return dim1*dim2;
}
}
class Circle1 implements GeoShapes{
double dim1;
Circle1(double x){
dim1=x;
}
public double area() {
return dim1*dim1*PI;
}
}
public class InterfaceDemo {
public static void main(String[] args) {
GeoShapes r=new Rectangle3(10,20);
System.out.println("Area of Rectangle: "+r.area());
GeoShapes c= new Circle1(10);
System.out.println("Area of Circle: "+c.area());
}
}
Accessing Implementations through interface references:
Variables can be declared for interfaces to refer the implementing class object
When a method is called through one of the references, the correct version will be
called based on the actual instance of the interface being referred to.
Nested interfaces
In Java, an interface can be defined within another class or interface.
The interface that defined inside another class or interface is known as nested interface.
The nested interface is also known as inner interface.
The nested interface declared within an interface is public by default.
The nested interface declared within a class can be with any access modifier.
Every nested interface is static by default.
The nested interface can be accessed by using outer interface or outer classname
followed by (.).
/* program implementing nested interface inside another interface */
interface OuterInterface{
void outerMethod();
interface InnerInterface{
void innerMethod();
}
}
class Child implements OuterInterface,OuterInterface.InnerInterface{
public void outerMethod() {
System.out.println("Outer method: ");
}
public void innerMethod() {
System.out.println("inner method:");
}
}
public class NestedInterface {
public static void main(String[] args) {
Child ob=new Child();
ob.outerMethod();
ob.innerMethod();
}
}
/* program implementing nested interface inside a class */
class OuterClass{
void outerMethod () {System.out.println("In OuterClass..");}
interface InnerInterface{
void innerMethod();
}
}
class Child extends OuterClass implements OuterClass.InnerInterface {
public void outerMethod() {
System.out.println("Outer method: ");
}
public void innerMethod() {
System.out.println("inner method:");
}
}
public class NestedInteface {
public static void main(String[] args) {
OuterClass ab = new Child();
ab. outerMethod ();
Child ob=new Child();
ob.outerMethod();
ob.innerMethod();
}
}
Variables in Interfaces
Interfaces can be used to share constants by importing into multiple classes.
Interface SharedConstants{
String ODD=”ODD NUMBER”;
String EVEN=”EVEN NUMBER”;
}
Class EVENODD implements SharedConstants{
public static void main(String[] args) {
String res;
int x=5;
if(x%2 == 0) { res = EVEN;}
else {res = ODD; }
System.out.println(res);
}
}
Extending Intefaces
One interface can inherit another interface by use of the keyword extends.
When a class implements an interface that inherits another interface, it must provide
implementations for all methods defined within the interface inheritance chain.
Interface ParentInterface{
void parentMethod();
}
Interface ChildInterface extends ParentInterface{
void childMethod();
}
class ImpClass implements ChildInterface{
Public void parentMethod(){
System.out.println(“In Parent Method”);
}
Public void childMethod(){
System.out.println(“In Child Method”);
}
}
class Demo{
public static void main(String args[]){
ImpClass ob = new ImpClass();
ob.parentMethod();
ob.childMethod();
}
}
Default Interface Methods
A default interface lets user to define default implementation for an interface method.
Purpose of Default Methods:
1. Interfaces could be expanded without breaking existing code
2. To specify methods in an interface that are essentially optional depending on how the
interface is used.
An interface default method is defined similar to a method defined in a class. The prmary
difference is that the declaration is preceded by the keyword default.
interface Stack{
void push(int item);
int pop();
// it need not be implemented by preexisting classes those implements Stack
default void clear(){
System.out.println(“clear method not implemented”);
}
}