Java Interfaces

Home   »    Java   »    Java Interfaces


We cannot do multiple inheritance in Java because we cannot create a subclass by extending more than one superclass. This way of multiple inheritance adds to the complexity in the program. Hence, it is not permitted in Java.

To achieve multiple inheritance in Java, we can implement multiple interfaces to create subclasses. One of the main features of OOP is its ability to reuse the code by extending the classes and implementing interfaces. If we need to reuse the classes from other programs without copying them , we use packages.

Contents



Interfaces

We know that java doesn't support multiple inheritance But in java we can implement the concept of multiple inheritance by using interfaces :- Interfaces are just like a Class but the main Feature of Class is that it provides Reusability of Code means if a user wish to use only three methods from a class rather all Methods resides in a class. Interfaces are also same like a class means they also contains data members and member functions but Interfaces doesn't allow us to do there are some Restriction while using Interfaces.

  1. All the data members of Interfaces are by default final means you cant specify a value outside from interface you can just use the value of data member which is declared in interface but you cant specify a new value to data member Means The data members of interfaces are final by default.

  2. Member Functions of interfaces are abstract by default means they have no definition a Class which uses interface will Define all the Methods of interface Note that if an interface contains Five Methods then you have to use or define all Five Methods in a class Which uses that interface if you skip a Method or in a Simple Words if you Doesn't define the body of Member Function then Compiler will gives us an Error Message it is necessary to define all the methods those are declared in a interface because all the member functions of interfaces are abstract In interface they are just declared and defined in that class which uses interface.

  3. For using an interface implements Keyword is used and When you define a member function of interface then it must be defined with the help of public access modifier.

  4. An interface may use another interface by using extends Keyword But if a class wants to use an interface then you have to use implements.

  5. At a Time a Class can Extends only one class but he can use many number of interfaces The Concept of Multiple Inheritance is Full filled when a Class can Extends a Class and Implements an interface.





Defining Interface

An interface is like a fully abstract class, except that it cannot have any concreate method or instance variables. It is a collection of abstract method declarations and constancts, that is , static final variables. This means that interfaces do not specify any code to implement these methods. Any class that implements an interface must implement all of the methods specified in that interface. A class can implement many interfaces but can extend only one class. The general syntax for defining interface is:

interface InterfaceName
{
  variable declaration;
  method declaration;
}

Here interface is a keyword and interfaceName is any valid Java variable.

Example:

interface Item
{
  static final int code = 1001;
  static final String name = "fan";
  void display();
}


Variables are declared as constants using static final keywords. Note that the code for display() is not included in the interface. The class that implements this interface must define the code for the method.



Extending Interfaces

Like classes, interfaces can also be extended. The new subinterface will inherit all the members of the super interface in the manner similar to subclasses.

interface ItemConstants
{
  int code = 1000;
  String name = "Fan";
}
interface Item extends ItemConstants
{
  void display();
}

While interfaces are allowed to extend to other interfaces, sub interfaces cannot define the method declared in the super interface. Instead, it is the responsibility of any class that implements the derived interface to define all the methods.

Interfaces can have access modifiers of public or blank, just like classes, An interface can be defined as extending another interface, similar to class hierarchy, but there is no base interface analogous to the Object class.

Interface - Extending Interface:


Output of "ExtendingInterface.java"



Download Complete Program

  -  




Implementing Interfaces

Interfaces are used as "superclasses" whose properties are inherited by classes. it is, therefore, necessary to create that inherits the given interface.

class classname extends superclass implements interface1, interface2,.............
{
}

Class can extend another class while implementing interface.

Implementing Interface:


Output of "ImplementingInterface.java"







Download Complete Program

  -  






5 comments: