Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class" that is used to group related methods with empty bodies:
// the interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
To access the interface methods, the interface must be "implemented" (kind of inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class:
Cat2.java
// Cat2 "implements" the Animal interface
class Cat2 implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The cat2 says: miaw miaw");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
TestCat.java
class TestCat {
public static void main(String[] args) {
Cat2 myCat = new Cat2(); // Create a Cat object
myCat.animalSound();
myCat.sleep();
}
}
The output will be:
The cat2 says: miaw miaw
Zzz
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass)
Interface methods do not have a body - the body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
1) To achieve security - hide certain details and only show the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
To implement multiple interfaces, separate them with a comma:
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}