ZenLogic
0
Q:

what is interface

// Interface
interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

// Pig "implements" the Animal interface
class Pig implements Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    // The body of sleep() is provided here
    System.out.println("Zzz");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}
7
public interface Exampleinterface {
	
  public void menthod1();
  
  public int method2();

}

class ExampleInterfaceImpl implements ExampleInterface {


  	public void method1()
    {
        //code here
    }
  
    public int method2()
  	{
    	//code here
  	}

}
6
Interfaces specify what a class must do and not how. 
It is the blueprint of the class.It is used to achieve total abstraction. 
Interfaces are used to implement abstraction

Let’s consider the example of vehicles like bicycle, car, bike………, 
they have common functionalities. So we make an interface and put all these 
common functionalities. And lets Bicycle, Bike, car ….etc implement all these 
functionalities in their own class in their own way.
0

New to Communities?

Join the community