Java p10
Java p10
1. INTRODUCTION
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship. It cannot be instantiated just like the
abstract class.
One interface can inherits another interface by use of the keywords extends. When it’s
class implement an interface that inherit another Interface , it must provide
implementations for all methods in the both Interface.
Interface do not have constructors, main method, static keyword, final keyword or private
methods.
interface interface_name{
……….
//Methods or variables
……………
}
interface inter_face-1{
…………. // methods
}
void Display();
}
class TestClass implements ad1 { Class TestClass
public void Display() {
System.out.println("\nThis Method is inside Interface.");
}
}
public class Interface1 {
public static void main(String args[]) {
System.out.print("\nTHIS IS PROGRAM FOR INTERFACE.");
TestClass obj1 = new TestClass();
obj1.Display();
}
}
//Output
class TestClass
interface ad2 {
void ADD();
}
class TestClass implements ad1, ad2 {
public void Display() {
System.out.println("\nThis Method is inside Interface.");
}
public void ADD() {
int A = 5;
int B = 8;
System.out.println("\nSum of A and B= " + (A + B));
}
}