Infolinks In Text Ads

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.
  • 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.
Implementing an Interface:
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 abstract
    class, a class must be a subclass of the abstract class. Any class that defines all of
    the required methods and obeys the general contract is permitted to implement an
    interface, regardless of where the class resides in the class hierarchy. Because Java
    permits only single inheritance, this restriction on abstract classes severely constrains
    their use as type definitions.
     


 

Newer Post Older Post

Leave a Reply

Powered by Blogger.