Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pure abstract class and interface [duplicate]

Tags:

java

Can anyone tell me what exactly the difference between an completely abstract class and an interface?

An Abstract class can also have all its methods as abstract. An interface has all its methods as abstract. What is the main difference between the two in this scenario?

If there is difference between a pure abstract Class and interface? What is the use of interface? Where interface is being used we can make use of pure abstract class?

like image 268
user241924 Avatar asked Jan 19 '10 07:01

user241924


2 Answers

To complete the former answers :

An interface is a "contract". If a class implements an interface it have to propose all the services listed in the interface.

An abstract class is a skeleton. It defines a certain way its extended classes will work while letting them some free space (the abstract methods) to be unique.

A pure abstract class doing the same thing as a interface but have the problem of unique extending so, for me, it have no interest

like image 66
Damien Avatar answered Sep 20 '22 18:09

Damien


Every interface is implicitly abstract: Every method declaration in the body of interface is implicitly abstract and public.

An abstract class has methods that can contain implementation. Abstract methods can be either public, protected or default access (package visible). Unlike interfaces abstract classes can contain fields that are not static and final.

Also see:
Interfaces vs Abstract classes and the Java tutorial

like image 20
sateesh Avatar answered Sep 24 '22 18:09

sateesh