Interfaces and Packages in Java

Abstract methods when brought together form a package. A class actualizes an interface, consequently inheriting the interface’s abstract methods. An interface is not a class. Composing an interface is like composing a class. However, they are two separate ideas. A class portrays the properties and behaviours of an object. On the other hand, An interface contains behaviours that a class shall implement.

Unless the class that actualizes the interface is abstract, all the methods for the interface need to be implemented in the class. An interface is like a class in the accompanying ways:

  • An interface is composed in a file with a .java augmentation, with the name of the interface matching the name of the file.
  • An interface can contain any number of methods.
  • Interfaces show up in packages, and their relating bytecode file must be in a directory structure that matches the name of the package.
  • The bytecode of an interface shows up in a .class record.

On the other hand, an interface is unique and different from a class in a few ways. These are:

  • An interface does not contain any constructors.
  • Interface cannot be instantiated
  • Instance fields cannot be contained in an interface. It is a requirement of interfaces that the main fields in them must be final and static.
  • It is a requirement that all of the methods must be abstract methods.
  • An interface can extend different interfaces.
  • A class does not access an interface. Actually, a class implements an interface.

 

For any query or issue, feel free to discuss on http://discuss.eduguru.in