Interface Declaration and Implementation

Interface Declaration and Implementation

  • whenever we are implementing an interface for each and every method of that interface we have to provide implementation otherwise we have to declare the class as abstract then the next level child class is responsible for providing an implementation.
  • every interface method is always public and abstract whether we are declaring or not, hence whenever we are implementing an interface compulsory we should declare it as public otherwise we will get compile time error.

eg-:

First Way-:

import java.io.*;
import java.lang.*;
interface interf{
    void m();
    void m1();
}
class serviceProvider implements interf{
    public void m(){
        System.out.println("Hello");
    }
    public void m1(){
        System.out.println("Word");
    }
}
class Test {
public static void main (String[] args) {
    serviceProvider sp=new serviceProvider();
    sp.m();
    sp.m1();

}
}
//Output-: Hello
                  Word

Second way-:

import java.io.*;
import java.lang.*;

interface interf{
    void m();
    void m1();
}
abstract class serviceProvider implements interf{
    public void m(){
        System.out.println("Hello");
    }
}
class serviceProvider2 extends serviceProvider{
    public void m1(){
        System.out.println("Word");
    }
}
class Test {
public static void main (String[] args) {
    
    serviceProvider2 sp2=new serviceProvider2();
    sp2.m1();
    sp2.m();
}
}

----------------------------------------------Wrong Way-------------------------------------------------------

import java.io.*;
import java.lang.*;
interface interf{
    void m();
    void m1();
}
class serviceProvider implements interf{
    void m(){
        System.out.println("Hello");
    }
    
}
class Test {
public static void main (String[] args) {
    serviceProvider sp=new serviceProvider();
    sp.m();
}
}

compile Time error-:
./Test.java:7: error: serviceProvider is not abstract and does not override abstract method m1() in interf
class serviceProvider implements interf{
^
./Test.java:8: error: m() in serviceProvider cannot implement m() in interf
void m(){
       ^
 attempting to assign weaker access privileges; was public
2 errors

import java.io.*;
import java.lang.*;
interface interf{
    void m();
    void m1();
}
abstract class serviceProvider implements interf{
    void m(){
        System.out.println("Hello");
    }
    
}

compile Time error-:
./Test.java:8: error: m() in serviceProvider cannot implement m() in interf void m(){
 attempting to assign weaker access privileges; was public
1 errors


                                                    extends vs implements

  • A class can extend only one class at a time.
  • An interface can extend any number of interfaces simultaneously.
                eg-:
                    interface A{
                   }
                   interface B{
                  }
                 interface extends A,B{
                 }
  • A class can implement any number of interfaces simultaneously.
  • A class can extend another class and can implement any number of interfaces simultaneously.
           eg-:
            class A extends B implements C,D,E{
           }

which of the following is valid
A class can extend any number of class at a time                                                                        //invalid
A class can implement only one interface at a time                                                                     // invalid
An interface can extend only one interface at a time                                                                   //invalid
An interface can implement any no. of interface simultaneously                                                //invalid
A class can extend another class are can implement an interface but not both simultaneously  //invalid
non of the above                                                                                                                                //Valid

Ques-:consider the following expression  X extends Y. for which of the following possibility of X and Y the expression is valid ?
  1. Both X and Y should be classes
  2. Both X and Y should be interfaces
  3. Both X and Y should be either classes or interfaces
  4. no Restriction
      Ans-: Both X and Y should be either classes or interfaces.

2-: X extends Y,Z
      X ,Y,Z should be interfaces

3-: X implements Y,Z
      X should be class
      Y ,Z are interfaces

4-: X extends Y implements Z
     X,Y  are classes
     Z are interface

5-: implement X extends Y
    compile time error because  we have to take extends first followed by interface