Monday 26 June 2017

Abstract Class vs Interface

An interface is a "contract". If a class implements an interface it has 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 an interface but have the problem of unique extending so, for me, it have no interest.

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.



Interface
Abstract Class
Extend Class      
No
Yes
Extend Abstract Class
No
Yes
Implement Interface
Yes(Extend Interface)
Yes
Variables
public static final        
public/protected/private/static/final/transient/volatile
Contain Non-Public Method 
No
Yes
Contain Abstract Method                
Yes
No

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...