Interfaces
An interface is a completely "abstract class" that is used to group related methods with empty bodies.
Characteristics:
Like abstract classes, interfaces cannot be used to create objects.
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.
When To Use Interfaces?
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.
Types of interfaces:
Single interface: single interface create.
Syntax:
interface interface_name
{
header of method1;
header of method2;
...
}
Example:
interface Animal {
public void animalSound();
public void sleep();
}
To access the interface methods, the interface must be "implemented" by another class with the implements keyword.
Example:
class Pig implements Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
public void sleep() {
System.out.println("The sleep says: Zzz Zzz");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
Multiple interface: more than one interface
Example:
interface FirstInterface {
public void myMethod();
}
interface SecondInterface {
public void myOtherMethod();
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("interface 1: method1");
}
public void myOtherMethod() {
System.out.println("interface 2: method2");
}
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
An interface is a completely "abstract class" that is used to group related methods with empty bodies.
Characteristics:
Like abstract classes, interfaces cannot be used to create objects.
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.
When To Use Interfaces?
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.
Types of interfaces:
Single interface: single interface create.
Syntax:
interface interface_name
{
header of method1;
header of method2;
...
}
Example:
interface Animal {
public void animalSound();
public void sleep();
}
To access the interface methods, the interface must be "implemented" by another class with the implements keyword.
Example:
class Pig implements Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
public void sleep() {
System.out.println("The sleep says: Zzz Zzz");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
Multiple interface: more than one interface
Example:
interface FirstInterface {
public void myMethod();
}
interface SecondInterface {
public void myOtherMethod();
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("interface 1: method1");
}
public void myOtherMethod() {
System.out.println("interface 2: method2");
}
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}