💻Step 18:Interface – 12th hour + code

Learn with youtube video-

Quick Overview between Interface, Abstract class and simple class-

FeatureInterfaceAbstract ClassClass
DefinitionA set of related methods with empty bodiesA class with one or more abstract methodsA concrete class with a complete implementation
Syntaxinterfaceabstract classclass
Extends another interfaceYesNoNo
Implements another interfaceYesYesYes
Extends another classNoYesYes
Can be instantiatedNoNoYes
Can have abstract methodsYesYesNo
Can have concrete methodsNoYesYes
Can have instance variablesYes (since Java 8)YesYes
All methods are automatically abstractYesNoNo

💡Definition-

Interface is a 100% abstract class.
– Till JAVA 7 – All methods in interface are public and abstract.
In JAVA 8 – public default and public static methods are allowed in java
In JAVA 9 – private methods and private static are allowed in java
-All data members in interface are public, static and final.

– multiple inheritance is allowed in interfaces.

Note 1- class ‘implements’ interface

Note 2- interface ‘extends’ interface

Code samples are here

💡1- One class can implements multiple interfaces

multpl

A-

package intrfc;

public interface X {
void show();
}

B-

package intrfc;

public interface Y {
void display();
}

C-

package intrfc;

public class Z implements X,Y {
@Override
public void display() {
System.out.println("bye");
}

@Override
public void show() {
System.out.println("hi");
}
public static void main(String[] args) {
Z z=new Z();
z.show();
z.display();
}
}

Run this Program ▶️

💡2- Interface can extend other interface

A-


package intrfc;

public interface X1 {
void show();
}

B-


package intrfc;

public interface Y1 extends X1 {
void display();
}

C-


package intrfc;

public class Z1 implements Y1 {
@Override
public void display() {
System.out.println("bye");
}
@Override
public void show() {
System.out.println("hi");
}
public static void main(String[] args) {
Z1 z=new Z1();
z.show();
z.display();
}
}

Run this Program ▶️

After Java 7 – 
💡1- Interface’s default methods – 

public interface Person{
default String getName(){
return “kuldeep kaushik”;
}
}

Rules of interface’s default methods-

  1. If the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect.
  2. If one interface inherits another interface and both of them implement a default method, the default method of the child interface would be used by an implementing class.
  3. An explicit call to an interface default method can be made from inside an implementing class using super. For example Interface.super.defaultMethod()

💡– Interface’s static methods – 

public interface Test{
static String getStaticNumber(){
return 1;
}
}

Overview comparison of interface methods in Java:

MethodDescription
Interface methodAn interface method is a method that is declared in an interface and does not have an implementation.
Interface static methodAn interface static method is a static method that is declared in an interface and does not have an implementation. It must be implemented by the implementing class.
Interface private methodAn interface private method is not allowed in Java. An interface can only contain public or default methods.
Interface default methodAn interface default method is a method that is declared in an interface and has a default implementation. It can be overridden by the implementing class if needed.

Interview Questions —

  • What is difference between abstract class and interface?
  • What is difference between default methods and static methods?

Leave a Reply

Your email address will not be published. Required fields are marked *