Skip to main content

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

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...