Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between abstract classes and interfaces

 
Kalyani Athalye
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
I am with a very basic or may be stupid question, but plz answer my query.
I just want to know what are the differences between absract classes and interfaces and why there is need of these two separate entities when they function in similar ways..
Thanks & Regards,
Kalyani
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check out the following discussion on the same topic:
difference between interface and abstract classes
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalyani,
Well the abstract cla and the interface , these are 2 diff. entities. and not the same.
1. u can implement some methods in abstract class and u can keep some of them as abstract so that the derived class implements it. In the interface all methods are by default abstarct. u can not provide implementation to any generalised method also. the implementing class HAS TO implement those methods.
2. u can have private, protected instance member variables in abstract class. In the interface all variables are by default public static and final. u can't have private or protected in interface.
3. The interface is java's way to provide multiple inheritance functionality in OOPs. u can not extend 2 abstract classes at a time in java, however, u can very well implement 2 interfaces.
Hope this helps. :-)
Rashmi
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. u can have private, protected instance member variables in abstract class. In the interface all variables are by default public static and final.
However, it is a good practice not to see public static final fields as variables, but as constants.
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,
Abstact class can have constructors. Interfaces can't.
For no reason you can specify a fully developed class with abstract.
You can't instanciate an abstract class but you can instantiate a non abstract class derived from abstract class.
You can't instantiate an interface except the non abstract class that implements the interface.
What are we telling you? Difference between an abstract class and interface. What did you ask? Why these two?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For no reason you can specify a fully developed class with abstract.
For no reason? Are you sure? What about declaring a fully-developed class abstract in order to prevent people from directly instantiating it?
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For no reason? Are you sure? What about declaring a fully-developed class abstract in order to prevent people from directly instantiating it?

That is exactly my point. When the class is fully developed why are we using abstract. Any class that extends this fully developed class with {} and without adding a single line of code can be instantiated.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One example of the usage of abstract classes and interfaces can be seen in the collections framework in java.util package. Everything starts with an interface, Collection, then, many abstract classes are declared, these classes do some work that but let part of the work undone (that's why they are declared abstract). Then, many classes are created, they just extend an abstract class. Since part of the work is already done you have less work!!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly my point.
My point was about the "for no reason" part. If you declare a fully-developed class abstract, there is a reason, and the reason is that you don't want people to instantiate it directly.
So your statement would be best rephrased as
"you can intentionally declare a fully developed class abstract if you don't want people to instantiate it directly."
Pickyness is (one of) my vice
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One example of fully developed abstract classes are the adapter classes in AWT event handling.
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin,
I got the point of declaring the fully developed class as abstract, so that no one can instantiate it directly.
But you can have another approach for this too.
We can declare the constructor of the class as Private, which will not allow anyone to instantiate that class.
So, any other reason for declaring the fully developed class as abstract ?
Warm Regards
Mandar
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar,
If you declare the constructor private, you will have no way of extending the class, since the subclass constructor will not be able to call the supeclass constructor.
 
Kalyani Athalye
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
Thanks a lot
Regards,
Kalyani
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic