Interface and Abstract Class
Interface and Abstract Class
Declaring an interface
An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the
fields are public, static and final by default. A class that implements an interface must
implement all the methods declared in the interface.
Syntax:
interface interface_name
{
// declare constant fields
// declare methods that abstract
// by default.
}
Example
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("Hello");
}
}
class MB
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
Output:
Hello
Interface Example:
In this example, the interface A has only one method. Its implementation is provided by
B and C classes. In a real scenario, an interface is defined by someone else, but its
implementation is provided by different implementation providers. Moreover, it is used
by someone else. The implementation part is hidden by the user who uses the
interface.
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("Display method in B class");
}
}
class C implements B
{
public void display()
{
System.out.println("display method in C class");
}
}
class MainClass
{
public static void main(String args[])
{
D obj=new D();
obj.draw();
}
}
interface A
{
void display();
}
interface B
{
void show();
}
class C implements A,B
{
public void display()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
}
class MainClass
{
public static void main(String args[])
{
C obj = new C();
obj.display();
obj.show();
}
}
Output:
Hello
Welcome
Interface inheritance
A class implements an interface, but one interface extends another interface.
interface A
{
void display();
}
interface B extends A
{
void show();
}
class C implements B
{
public void display()
{
System.out.println("Hello"); }
}
Class MainClass
{
public static void main(String args[]) {
Abstract Method
A method which is declared as abstract and does not have implementation is known as
an abstract method.
Example
abstract void display(); //no method body and abstract
1) Abstract class can have abstract and Interface can have only abstract methods. Since
non abstract methods. Java 8, it can have default and statc methods also.
3) Abstract class can have fnal, non-fnal, statc Interface has only statc and fnal variables.
and non-statc variables.
4) Abstract class can provide the Interface can't provide the implementaton
implementaton of interface. of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare interface.
abstract class.
6) An abstract class can extend another An interface can extend another Java interface only.
Java class and implement multple Java
interfaces.
8) A Java abstract class can have class Members of a Java interface are public by default.
members like private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }