Interface In Java
What is an Interface?
- · An interface is just like Java Class, but it only has static constants and abstract method.
- · Java uses Interface to implement multiple inheritance.
- · A Java class can implement multiple Java Interfaces.
- · All methods in an interface are implicitly public and abstract.
To implement Interface:
1. create a package in eclipse > project > myPackage
2. in this myPackage > create an Interface > "MyInterface"
3. in this MyInterface .. : don't create any main method. We need to declare the method.
there will be NO body for these methods. the bodies will be defined in the class file, which implement this "MyInterface"
===================================
created an Interface:
===================================
package test;
public interface MyInterface {
public void testSum();
}
==================================
Create a class and implement this Interface
==================================
package test;
public class ClassA implements MyInterface{
public void testSum()
{
System.out.println("Hello");
}
public static void main(String[] args) {
MyInterface m = new ClassA();
m.testSum();
}
}
===============================
in Interface we only declare the method, the implementation and body definition will be done at Class, where this iterface is implemented.
إرسال تعليق