C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

What are the differences between abstract class and interface?

Learn: Here we will learn what is the difference between abstract class and interface? What are the similarity and dissimilarity of abstract class and interface? Compare abstract class and interface?
Submitted by Preeti Jain, on February 01, 2018

1) Abstract (abstract) class

1) abstract is a access modifiers which is applicable for class or methods.

2) abstract (keyword modifiers) is used for defining a abstract class.

Example:

abstract class AbstractClass{
	void printHello(){
		System.out.println("Say Hello to everyone");
	}
	void printHi();
}

3) When we are talking about implementation partially (Not Completely). It means we can implement few methods in a class (if you want).

4) Abstact class can contain abstract and concrete methods. (i.e. no restriction if you are defining concrete methods in abstract class that’s why we are calling as partially implementation).

5) Every method present in abstract class need not to be public and abstract.

6) If we are talking about which modifiers are applicable for abstract class methods (No restrictions of modifiers on abstract class methods because it is not needed that method should be public or abstract).

7) Variable declared inside abstract class need not to be public, static and final.

8) We can happily declare any modifiers on abstract class variable (i.e. No Restrictions).

9) It is not required to perform initialization at the time of declaration (i.e. abstract class variable may be non final) of abstract class variable.

10) We can declare constructor in abstract class and it will executes at the time of child class object creation.

2) Interface (interface) in java

1) interface is a keyword modifiers which is applicable only for interface.

2) interface (keyword modifiers) is used for defining a interface.

Example:

interface Interface{
	void printHello();
	void printHi();
}

3) When we are not talking about implementation .It means we cannot implement any of the methods in an interface (i.e. it is fully abstraction).

4) interface cannot contain concrete methods.(i.e. Restricted if you are defining concrete methods in an interface that’s why we are calling as fully abstraction).

5) Every method present in an interface need to be public and abstract.

6) If we are talking about which modifiers are applicable for interface methods (Restrictions of modifiers on interface methods because it is needed that method should be public and abstract).

7) Variable declared inside interface need to be public, static and final.

8) We cannot happily declare any modifiers on interface variable (i.e. Restricted like private, protected, transient and volatile).

9) It is required to perform initialization at the time of declaration (i.e. interface variable is final) of interface variable.

10) We cannot declare constructor in interface because creation of interface object is not possible.

Read more:



Liked this article? Do share with your friends :)


COMMENTS