Monday, January 2, 2012

Interface


Interfaces:

In the Java programming language, an interface is a reference type, similar to a class that can contain only constants, method signatures, and nested types. 

Syntax for declaring interface

interface <interface_name> {

//final variables
//methods
}

Example:

public interface Game{
                        public void play();
                        public abstract void start();
                        void stop();
}

  1. An interface cannot be instantiated, but it allows to create reference variable
  2. The interface all methods are inherently public and abstract. 
  3. An interface reference can point to objects of its implementing classes
  4. An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces
  5. All the variables in an interface are implicitly public static final
  6. A class can extend  only one class at a time by using the keyword extends, but can inherit from one or more interfaces at a time by using the keyword implements.
  7. Because interface methods are inherently abstract, the method cannot be declared final, native, strictfp, or synchronized
  8. An interface cannot implement any interface or class.
  9. Interfaces do not have constructors. Interfaces are not part of an object's inheritance tree.


interface Game{
            public void play();
            public abstract void start();
            void stop();
}

class Car implements Game{

@Override
public void play() {
            System.out.println("You are playing car game");
}
@Override
public void start() {
            System.out.println("Car game is started");

           
}
@Override
public void stop() {
            System.out.println("Car game is stoped");
           
            }
}

The implemented class of the interface must implement all the methods, otherwise the calss must be declared as the abstract

interface Game{
            public void play();
}
class Car implements Game{

}

It leads to compilation error; class must be declared as abstract or must give the implementation for the play method.


An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces.

interface One{
            public void one();
}

interface Two{
            public abstract void two();
}
interface Three extends One,Two{
            public abstract void three();
}


All methods in an interface are implicitly public and abstract, if declare method without public abstract automatically compiler provides public abstract for those methods.

interface Game{
            public void start();
            void play();
            public abstract void stop();
           
}


The Game class for all the methods automatically compiler provides the public abstract.
  
All the variables in an interface are implicitly public static final

interface DemoInterface{
      int a=10;
      public static final int b=20;
      static int c=30;
     
}

Here the compiler provides for all the variables as the public static final.

Case 1:

If the class is implementing two or more  interfaces and all the interface contain same method with same signature  then it’s enough to give the implementation for one method.

interface One{
      public void show();
}
interface Two{
      public void show();
}
class Example implements One,Two{

      @Override
      public void show() {
            System.out.println("The show method");
           
      }
     
}

Case2:

If  interface method name same as object class method, then method arguments and return type should be same otherwise it leads to the compilation error.

   interface One{
      public void show();
      public int toString();
 }

It leads to the compilation error, the return type of the object class toString method is string, but in interface we declared as int.

Case3:

If two interfaces contains a method with the same name but different arguments. In the implementation class compulsory we should provide implementation for both methods and these methods act as overloads method.
                       
interface One{
      public void show(int a);
}
interface Two{
      public void show(String name);
}
class Example implements One,Two{
      @Override
      public void show(String name) {
            System.out.println("The show method with the string arguments");       
      }

      @Override
      public void show(int a) {
            System.out.println("The show method with the int argument");           
      }
}


Case4:

If two interfaces contains a method with same signature but different return type then we can’t implement those two interfaces simultaneously.

interface One{
      public void show(int a);
}
interface Two{
      public int show(int a);
}
class Example implements One,Two{

      @Override
      public void show(int a) {
            // TODO Auto-generated method stub
           
      }
}

It leads to compilation error.



Case 5:

If the class is implementing two or more interfaces, if those interfaces contain the variables with same name with same data type, then it leads to compilation error, when you try to access that variable in the implemented class.

interface One{
      int a=10;
}
interface Two{
      int a=20;
}
class Example implements One,Two{
      public void show() {
            System.out.println(a);
      }
}


Marker Interface

Marker interface which doesn’t contain any body, Marker interface is used as a tag to inform a message to the java compiler so that it can add special behavior to the class implementing it

The marker interfaces examples  are : Serializable, Clonable, EventListener


Abstract classes
Interface

Abstract class contains zero or more abstract methods


Interface contains only abstract method

Abstract class contains instance variables and constants


Interface contains only constants. i.e static final variables

Abstract class can implement any number of interfaces and can extend one class


Interface can’t implement another interface or class, but it can extend any number of interfaces.

Abstract class can have constructor


Interface doesn’t have a constructor, interface doesn’t participate in object creation hierarchy

If new method is added in abstract class with the default implementation. Then all existing code will execute without any modification

If new method is added in interface, all implemented class we must give the implementation for newly added method.

Abstract class are fast


Interfaces are slow as it requires extra indirection to find corresponding method in the actual class



No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...