java2db

What is the difference between interface and abstract class in java



interface and abstract class are playing a major role , to ensure security and flexibility to the java projects.The main  difference between interface and abstract class are listed below.

Interface:
interface and abstract class in java Interface contains all unimplemented methods,having no body.
interface and abstract class in java The variables of an interface are  public and final (ie constants).
interface and abstract class in java Interface can extends another interface but not possible to implement.
interface and abstract class in java We cannot instantiate an interface.
interface and abstract class in java Interface reduced the multiple inheritance problem in java.
interface and abstract class in java All methods of an interface is by default public and abstract.
interface and abstract class in java Each and every method in an interface terminates with a semicolon (;).
interface and abstract class in java Methods of an interface should not allow static , final and synchronized access modifiers.
interface and abstract class in java Methods of an interface should not allow private and protected access specifiers.

Abstract class:
interface and abstract class in java An Abstract class may contain all abstract or all implemented methods or both.
interface and abstract class in java Variables can be declared as public private and also protected.
interface and abstract class in java It can extends an another abstract class but not possible to implements.
interface and abstract class in java Instantiation is not possible.
interface and abstract class in java It cannot support in multiple inheritance.
interface and abstract class in java Methods can be public,protected and abstract.
interface and abstract class in java Abstract  methods only ends with semicolon(;) .
interface and abstract class in java Methods in an abstract class should not allow static , final and synchronized access modifiers.
interface and abstract class in java Methods in an interface should not allow only private  access specifier.

 

Syntax for interface and abstract class:

Interface:
public interface interfaceExe extends interface1,interface2,..
{
//final fields
//public and abstract methods
}
Abstract class:
public abstract class abstClassExe_2 extends abstClassExe_1 implements interface1,interface2,…
{
//fields
//implemented methods
//public abstract methods
}

 

Interface example :

 

Abstract class example :

 

Interface implementation in a class: We can able to implement an interface in a class.See the example below.

output:

In the above example , we implemented the Comparator interface to sort (ascending order) the objects based on it’s names .

 

Implementation of  an  interface in a method:




Related Posts :
What is an interface and how to work with an interface in java
What is Map interface in java
What is List interface in java
What is Set interface in java
Difference between Set and Map
Difference between List and Set
   [will not be published]



^ <