Abstract class vs Interface in Java

Abstract class vs Interface in Java

1. Abstract class: Can have public, protected.

Interface: Must have only public.[by default everything in interface will be public.]

Example:

[java]

// No exception [private is not allowed since we need to access and implement the methods]
abstract class JdAbstractClass{
protected abstract void display();
public abstract void display1();
}

//Exception [Illegal modifier for the interface method display; only public, abstract, default, static and strictfp are

permitted]
interface JdInterface{
// protected should not be used inside the interface
protected void display();
public abstract void display1();
}
[/java]

 
2. Abstract class: Methods inside the abstract class must be the abstract type methods.
Interface: Interface can have both abstract type methods and non-abstract methods [should not have any implementation inside the method, only declarations are allowed of type abstract and non-abstract methods].

Example:

[java]
abstract class JdAbstractClass{
protected abstract void display();
// Exception [This method requires a body instead of a semicolon]
// display1() should be a abstract method, since it is inside the abstract class.
public void display1();
}

//Can have both abstract(display1()) and non-abstract(display()) methods.
interface JdInterface{
public void display();
public abstract void display1();
}
[/java]

 
3. Abstract class: Methods definition/implementation can be coded in the non-abstract type methods inside the abstract class.
Interface: By default all the methods are of type abstract in the interface, so it should not have the method definitions.
Example:

[java]
abstract class JdAbstractClass{
public void display1(){
System.out.println("inside the abstract class");
}

// since display() is a abstract method it should not have body/method definition
public abstract void display(){
System.out.println("inside the abstract class");
}
}

interface JdInterface{
//by default all the methods inside the interfaces are abstract
// so both abstract and non-abstract type methods should not have the method definition/implementation inside the

interface.
// Exception [Abstract methods do not specify a body]
public void display(){

}

// Exception [Abstract methods do not specify a body]
public abstract void display1(){

}
}
[/java]

 

4.
Abstract class: Abstract type methods should be overridden in the extended class.
Example:

[java]
abstract class JdAbstractClass{
// this can be overridden/not
public void display1(){
System.out.println("inside the abstract class");
}

// but these 3 abstract methods must be overridden.
public abstract void display2();
public abstract void display3();
public abstract void display4();
}

public class JdImplementations extends JdAbstractClass{
public static void main(String[] args) {
System.out.println();
}

@Override
public void display2() {
// TODO Auto-generated method stub

}

@Override
public void display3() {
// TODO Auto-generated method stub

}

@Override
public void display4() {
// TODO Auto-generated method stub

}

}
[/java]

 

Interface: All the methods are abstract types (only method declarations inside the interface) by default. so all the methods should be overridden in all the implemented classes.

Example:

[java]
interface JdInterface{
public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}

public class JdImplementations implements JdInterface{

@Override
public void display() {
// TODO Auto-generated method stub

}

@Override
public void display1() {
// TODO Auto-generated method stub

}

@Override
public void display2() {
// TODO Auto-generated method stub

}

@Override
public void display3() {
// TODO Auto-generated method stub

}

}
[/java]

 
5.
Abstract class: It can have private, public, protected, final and static variables.
Interface: It can have only public, static and final. [private and protected is not allowed]
Example:

[java]

// abstract can have private, public, protected, final and static variables
abstract class JdAbstractClass{
int intVariable = 10;
private int intPrivVariable=10;
public int intPubVariable=10;
protected int intProtVariable=10;
static int intStatVariable = 10;
final int intFnalVariable = 10;

public void display1(){
System.out.println("int :::"+intVariable);
System.out.println("private int :::"+intPrivVariable);
System.out.println("public int :::"+intPubVariable);
System.out.println("protected int:::"+intProtVariable);
System.out.println("static int:::"+intStatVariable);
System.out.println("Final int:::"+intFnalVariable);
}

public abstract void display2();
public abstract void display3();
public abstract void display4();
}

// Interface can have publi,static and final variables. [private and protected is not allowed inside the interface]
interface JdInterface{
int intVariable = 10;
public int intPubVariable=10;
static int intStatVariable = 10;
final int intFnalVariable = 10;

// Exception [Illegal modifier for the interface field JdInterface.intPrivVariable; only public, static & final

are permitted]
private int intPrivVariable=10;
// Exception [Illegal modifier for the interface field JdInterface.intPrivVariable; only public, static & final

are permitted]
protected int intProtVariable=10;

public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}
[/java]

 

 

6.
Abstract class: one abstract class(JdAbstractClass_1) can extend another abstract class (JdAbstractClass).

When you extend the extended abstract class (JdAbstractClass_1) in JdImpl class then it will implement all the non-abstract type methods (methods which do not have the definitions). But you can also extend the already defined methods also by overriding it.

Example:

[java]
abstract class JdAbstractClass{
public void display1(){
System.out.println(" great ");
}

public abstract void display2();
public abstract void display3();
public abstract void display4();
}

abstract class JdAbstractClass_1 extends JdAbstractClass{
public void display8(){
System.out.println(" great ");
}

public abstract void display1();
public abstract void display5();
public abstract void display6();
}

public class JdImpl extends JdAbstractClass_1{

@Override
public void display1() {
// TODO Auto-generated method stub

}

@Override
public void display5() {
// TODO Auto-generated method stub

}

@Override
public void display6() {
// TODO Auto-generated method stub

}

@Override
public void display2() {
// TODO Auto-generated method stub

}

@Override
public void display3() {
// TODO Auto-generated method stub

}

@Override
public void display4() {
// TODO Auto-generated method stub

}

}

[/java]

Interface: one interface(JdInterface) can be extended in another interface(JdInterface_1). If you implement “JdInterface” then you can implement only the JdInterface methods in the implemented class.

But if you implement the JdInterface_1 interface then you have to implement JdInterface methods and JdInterface_1 methods in the implemented class. Because JdInterface_1 is also extend the JdInterface interface.
Example:

[java]

interface JdInterface{
public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}

interface JdInterface_1 extends JdInterface{
public void display();
public void display4();
public void display5();
public abstract void display6();
public abstract void display7();
}

public class JdImpl implements JdInterface_1{
public static void main(String[] args) {

}

@Override
public void display() {
// TODO Auto-generated method stub

}

@Override
public void display1() {
// TODO Auto-generated method stub

}

@Override
public void display2() {
// TODO Auto-generated method stub

}

@Override
public void display3() {
// TODO Auto-generated method stub

}

@Override
public void display4() {
// TODO Auto-generated method stub

}

@Override
public void display5() {
// TODO Auto-generated method stub

}

@Override
public void display6() {
// TODO Auto-generated method stub

}

@Override
public void display7() {
// TODO Auto-generated method stub

}
}

[/java]

 
7.
Abstract classes: We know in java we can not extend more than one class in another class. Same applicable for abstract classes also.

Example:

[java]
abstract class JdAbstractClass{
public void display1(){
System.out.println(" great ");
}

public abstract void display2();
public abstract void display3();
public abstract void display4();
}

abstract class JdAbstractClass_1{
public void display1(){
System.out.println(" great ");
}

public abstract void display2();
public abstract void display3();
public abstract void display4();
}

// Exception, since we are trying to extend two classes/abstract classes
abstract class JdAbstractClass_2 extends JdAbstractClass,JdAbstractClass_1{

}
[/java]

Interface: Interface can extend more than one interface.

Example:

[java]

interface JdInterface{
public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}

interface JdInterface_2{
public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}

interface JdInterface_3{
public void display();
public void display1();
public abstract void display2();
public abstract void display3();
}

// no exception, since interface can extend more than one interface.
interface JdInterface_1 extends JdInterface,JdInterface_2,JdInterface_3{
public void display();
public void display4();
public void display5();
public abstract void display6();
public abstract void display7();
}
[/java]

8.
Abstract Class: Abstract class can have constructor.
Interface: Interface will not have the constructor.

Example:

[java]
abstract class JdAbstractClass{
// constructor
JdAbstractClass(){

}
}

interface JdInterface{
//Exception [Interfaces cannot have constructors]
JdInterface(){

}
}
[/java]

 

9.
Abstract class & Interface: Both can have the main method.
Example:

[java]
public abstract class JdAbstractClass{
// constructor
JdAbstractClass(){

}

public static void main(String[] args) {
System.out.println("Hello world!");
}
}
// prints Hello world!

interface JdInterface{
public static void main(String[] args) {
System.out.println("Hello world by interface");
}
}

// prints Hello world by interface

[/java]

 
10.
Abstract class: abstract keyword used to create the abstract classes.
Interface: interface keyword used to create the interfaces.

 

Please add if you have any other difference between abstract classes and interfaces in java in the below comments section…….

 

Leave a Reply