Difference between Abstract Class and Interface in Java ?

The blog covers the detailing about abstract class and interfaces in java. 

buy modafinil bangkok What is an Abstract Class in java ?

  • An abstract class contains the abstract method ( without body /implementation) and concrete methods (methods with implementation)
  • An abstract class is defined using the keyword “abstract” 
  • A class can override the methods of abstract class by implementing them
  • An abstract class can not be instantiated i.e., cannot have an object creation
  • As an abstract class can have concrete methods, it supports partial abstraction

For instance, consider an abstract class example with methodOne() , methodTwo() and methodThree() where the methodThree() is a concrete method with the body .

package core.java.planforexams.oops;

abstract class AbstractExample {

abstract void MethodOne();

abstract void MethodOTwo();

//This is concrete method with body
void MethodThree(){
System.out.println (" This is a concrete method in anstract class.. ");
}
}

http://choicespregnancycentre.co.uk/post-abortion/ What is an Interface Class in java ?

An interface in java provides the abstract methods ( without implementation) and thus supports full abstraction.

For instance, consider the student marks interface example in java 

Create an interface StudentMarksInterface  with abstract method getMarks() 

Create the class StudentMarks that implements StudentMarksInterface and overrides the getMarks() 

package core.java.planforexams.oops;

interface StudentMarksInterface {

public int getMarks();
}
package core.java.planforexams.oops;

public class StudentMarks implements StudentMarksInterface {

public int getMarks()
{
System.out.println("Implemnts the getMarks() in classs StudentMarks... ");
return 0;
}

public static void main(String arg[])
{
StudentMarksInterface obj = new StudentMarks();
obj.getMarks();

}

}

The below given are the major differences between abstract class and an interface in java

Abstract Class in Java  Interface in Java 

The abstract keyword is to be declared with class name to make it as an abstract class

The interface keyword is to be declared with class name to make it as an interface class

An abstract class is extended using keyword “extends”

An interface class is implemented using keyword “implements”

An abstract class supports final, non-final, static and non-static variables

An interface supports static and final variables

Multiple Inheritance is supported by an abstract class

Multiple Inheritance is supported by an interface class

An Abstract class can have abstract and non-abstract methods

An interface class can have only abstract methods. 

From Java 8 onwards , an interface class can have default and static methods.

An Abstract class provides the implementation of interface

An interface does not support abstract class implementation

An abstract class can extend another Java class and implement multiple Java interfaces

An interface class can extend another interface class

An abstract class members could be private , protected and public

An interface class members are public by default

public abstract class Vehicles{

public abstract void getModel();

}

public interface VehicleModel{

void getModel();

Leave a Reply

Your email address will not be published. Required fields are marked *

*