0% found this document useful (0 votes)
2 views

13 Java Interface PDF

An interface in Java is a blueprint that contains only abstract methods, allowing for 100% abstraction. It enables a class to implement multiple interfaces, facilitating multiple inheritance, and since Java 8, interfaces can also include default and static methods. The document provides examples of creating and using interfaces, including abstract methods, default methods, and static methods, along with the use of final variables in interfaces.

Uploaded by

Vignesh gs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

13 Java Interface PDF

An interface in Java is a blueprint that contains only abstract methods, allowing for 100% abstraction. It enables a class to implement multiple interfaces, facilitating multiple inheritance, and since Java 8, interfaces can also include default and static methods. The document provides examples of creating and using interfaces, including abstract methods, default methods, and static methods, along with the use of final variables in interfaces.

Uploaded by

Vignesh gs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

INTERFACE

 Interface is similar to a class but only contains abstract


methods (methods without a body)
 Interface is also called as blueprint of class
 Interface is used to achieve 100% abstraction because
in abstraction using a non-abstract methods its not
consider as 100% abstraction so using a interface we
can only use abstract method in interface
 In interface we can't create object for interface same
as abstraction
 To access abstract method in interface need to
override to interface child class and creating an
runtime object (runtime polymorphism) we can access
abstract method
 We can't use non-abstract method in interface
(methods with body)
 Java doesn't support multiple inheritance with java
classes, but a class can implement multiple interfaces.
 Advantage of using interface a class can be implements
multiple interfaces
 Disadvantage of using interface before java 8,
interfaces couldn't have non-abstract(method with
body) after java 8 we can use static and default
methods(method with body) in interface
 In interface after java 8, only we can have public, static
and final constants (variables and methods)
Sample program for an interface using a shapes:
package javaprogram;
// for create java interface we need to use
interface keyword
interface shapes
{
// we can create n of number abstract methods
in interface
void square(); // abstract method square
void rectangle(); // abstract method rectangle
}

//child class of interface shapes


// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class shapesDefineSize implements shapes
{
public void square()
{
// Area of square
int width=5; int height=5;
int squareArea=width*height;
System.out.println("Area of square
"+squareArea);

// using a width or height multiple to


side value get perimeter
// Perimeter of square
int squarePerimeter=width*4;
System.out.println("Perimeter of square
"+squarePerimeter);
}
public void rectangle()
{
// Area of rectangle
int length=4; int width=6;
int rectangleArea=length*width;
System.out.println("Area of rectangle
"+rectangleArea);

// Perimeter of square
int size=2;
int
rectanglePerimeter=size*(length+width);
System.out.println("Perimeter of rectangle
"+rectanglePerimeter);
}
}
public class TopicInterface{
public static void main(String[] args) {

// create object of runtime(runtime


polymorphism)
shapes shapessize=new shapesDefineSize();
shapessize.square();
shapessize.rectangle();
}
}

(After java 8)Using a non-abstract methods (method with


body default and static) and variable as final in interface
package javaprogram;
// for create java interface we need to use
interface keyword
interface shapes
{
void circle(); // abstract method
// in interface for default method we need to
use default keyword
default void defaultmethod() // default method
{
System.out.println("interface shape default
method");
}
static void staticmethod() // static method
{
System.out.println("interface shape static
method");
}
}

//child class of interface shapes


// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class shapesDefineSize implements shapes
{
public void circle()
{
// Area of circle
int radius=3; int mathOfCircle=3;
int
circleArea=radius*radius*mathOfCircle;
System.out.println("Area of circle
"+circleArea);

// Perimeter of circle
int size=2;
int
PerimeterCircle=size*radius*mathOfCircle;
System.out.println("Area of circle
"+PerimeterCircle);

}
}
public class TopicInterface{
public static void main(String[] args) {

// create object of runtime(runtime


polymorphism)
shapes shapessize=new shapesDefineSize();
shapessize.circle(); // calling abstract
method of shapes interface
shapessize.defaultmethod();// calling
default method of shapes interface
shapes.staticmethod();// calling static
method of shapes interface
}
}

We can use final variable in java interface


package javaprogram;
// for create java interface we need to use
interface keyword
interface Numbers
{
int a=100; // the variable as automatically
define as public, static and final
void add(); // abstract method
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
class adds implements Numbers
{
public void add()
{
int b=200;
// We didn't use any object or static or
final but its access direct a variable
int addition=a+b;
System.out.println(addition);
}
}
public class TopicInterface{
public static void main(String[] args) {

// create object of runtime(runtime


polymorphism)
Numbers addValue=new adds();
addValue.add(); // calling abstract method

}
}

Using a multiple inheritance in java interface


package javaprogram;
// for create java interface we need to use
interface keyword
interface GotWhatsappMessage
{
void message();
}
interface ViewWhatsappMessage
{
void view();
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
// using multiple inheritance in java
class notification implements
GotWhatsappMessage,ViewWhatsappMessage
{
public void message()
{
System.out.println("got message in
whatsapp");
}
public void view()
{
System.out.println("view message in
whatsapp");
}
}
public class TopicInterface{
public static void main(String[] args) {

// create object of runtime(runtime


polymorphism)
GotWhatsappMessage gotmessage=new
notification();
gotmessage.message(); // calling
GotWhatsappMessage abstract method
ViewWhatsappMessage viewmessage=new
notification();
viewmessage.view();// calling
ViewWhatsappMessage abstract method

}
}

Creating an multiple interface in java we can connect


interfaces using extends keyword for create single object
package javaprogram;
// for create java interface we need to use
interface keyword
interface GotWhatsappMessage
{
void message();
}
// connect one interface to another interface using
a extends keyword
interface ViewWhatsappMessage extends
GotWhatsappMessage
{
void view();
}
//child class of interface shapes
// the both abstract method need to override to
child class shapesDefineSize
// without override error throws as change
shapesDefineSize to abstract class
// using multiple inheritance in java
class notification implements
GotWhatsappMessage,ViewWhatsappMessage
{
public void message()
{
System.out.println("got message in
whatsapp");
}
public void view()
{
System.out.println("view message in
whatsapp");
}
}
public class TopicInterface{
public static void main(String[] args) {

// create object of runtime(runtime


polymorphism)
ViewWhatsappMessage viewmessage=new
notification();
viewmessage.message();// calling
GotWhatsappMessage abstract method
viewmessage.view();// calling
ViewWhatsappMessage abstract method

}
}

You might also like