Interfaces In Java
Interface or Pure Abstract Class
An abstract class having all methods abstract called pure Abstract class or Interface. In java programing language a pure abstract class is represented by a separate keyword Interface.
An interface is a list of methods that is defined by any class.
Implementing an Interface:An abstract class having all methods abstract called pure Abstract class or Interface. In java programing language a pure abstract class is represented by a separate keyword Interface.
An interface is a list of methods that is defined by any class.
- Access level of interface can only be default or public
- Access level for methods in interface can only be public
- Even if you do not specify access modifiers for method in interface it can be public by default.
- All the methods in interface are abstract by default you don't need to specify explicitly.
if a class implements an interface it must define all methods of that interface. if a class don't implement all methods of an interface it must be abstract class. A class can implement more than one interface.
Example Code:
package
com.aurangzeb.utk;
interface abc{
public void myMethod();
void method();
int methodthree(String abc);
}
Difference Between an Interface and Abstract Class:
- The most obvious difference between the two mechanisms that abstract classes are permitted to contains implementation for some methods while interfaces are not .
-
A more important difference is that to implement the type defined by an abstractclass, a class must be a subclass of the abstract class. Any class that defines all ofthe required methods and obeys the general contract is permitted to implement aninterface, regardless of where the class resides in the class hierarchy. Because Javapermits only single inheritance, this restriction on abstract classes severely constrainstheir use as type definitions.
