Abstract class and Interface.. Whats the difference!!

Few things to keep in mind about abstract class and interface in Java.. Thought of list them down and share them.

1)Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

2)Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

3)Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

4)Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

5)An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend multiple interfaces and cannot extend an abstract or concrete class. Also, an interface cannot implement (but extend) other interfaces”.

6)A Java class can implement multiple interfaces but it can extend only one abstract class.

7)Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

8)In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Advertisements

2 thoughts on “Abstract class and Interface.. Whats the difference!!

  1. From a point of view of a software design, if you have a class which has to adhere to certain standards and certain functionalities are expected out of it, then we have to use interfaces. Interfaces are like contracts which help in defining a breed of classes having almost same method calls. If any call is not available then we can use Unsupported custom exception in the concrete class!

  2. I would have agreed with you Manjit, if you’d use word ‘Declaring’ in place of ‘Defining’. By definition, interfaces are only allowed to declare and not define anything. I just am not sure what you want to convey by ‘defining a breed of claases’. I think, ‘Declaring a breed’ will make more sense when talking about interfaces. What is your opinion?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s