JAVA - INTERFACES
Elizabeth Alexander
Hindustan University
INTERFACES
● It is a collection of abstract methods.
● A class implements an interface, thereby inheriting the abstract methods of the
interface.
● Along with abstract methods, an interface may also contain constants, default
methods, static methods, and nested types. Method bodies exist only for default
methods and static methods.
● Writing an interface is similar to writing a class. But a class describes the
attributes and behaviors of an object. And an interface contains behaviors that a
class implements.
● Unless the class that implements the interface is abstract, all the methods of the
interface need to be defined in the class.
DIFFERENCE BETWEEN CLASS AND INTERFACE
● You cannot instantiate an interface.
● An interface does not contain any constructors.
● All of the methods in an interface are abstract.
● An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
● An interface is not extended by a class; it is implemented by a class.
● An interface can extend multiple interfaces.
Internal addition by compiler
•The java compiler adds public and abstract keywords before the interface method.
More, it adds public, static and final keywords before data members.
Declaring Interfaces
● The interface keyword is used to declare an interface. Here is a simple example
to declare an interface
■ public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
}
Properties of Interfaces
● An interface is implicitly abstract. You do not need to use the abstract keyword
while declaring an interface.
● Each method in an interface is also implicitly abstract, so the abstract keyword is
not needed.
● Methods in an interface are implicitly public.
Example
interface Animal {
public void eat();
public void travel();
}
Implementing Interfaces
● A class uses the implements keyword to implement an interface. The implements
keyword appears in the class declaration following the extends portion of the
declaration.
DIFFERENCE BETWEEN ABSTRACT AND FINAL
● Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).

Java interfaces

  • 1.
    JAVA - INTERFACES ElizabethAlexander Hindustan University
  • 2.
    INTERFACES ● It isa collection of abstract methods. ● A class implements an interface, thereby inheriting the abstract methods of the interface. ● Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. ● Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements. ● Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
  • 3.
    DIFFERENCE BETWEEN CLASSAND INTERFACE ● You cannot instantiate an interface. ● An interface does not contain any constructors. ● All of the methods in an interface are abstract. ● An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. ● An interface is not extended by a class; it is implemented by a class. ● An interface can extend multiple interfaces.
  • 4.
    Internal addition bycompiler •The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members.
  • 5.
    Declaring Interfaces ● Theinterface keyword is used to declare an interface. Here is a simple example to declare an interface ■ public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations } Properties of Interfaces ● An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. ● Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. ● Methods in an interface are implicitly public.
  • 6.
    Example interface Animal { publicvoid eat(); public void travel(); } Implementing Interfaces ● A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
  • 9.
    DIFFERENCE BETWEEN ABSTRACTAND FINAL ● Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).