aspose file tools*
The moose likes Beginning Java and the fly likes Interface Vs Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface Vs Abstract class" Watch "Interface Vs Abstract class" New topic
Author

Interface Vs Abstract class

Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
Hi All,

I am confusing with Interface and Abstract class I know the concept of these two, and I want to know which one is better (as of I know it depends on the situation rite).

For Ex: in my application I have 200 classes and I have one interface, I am using this interface in 50 classes, In future I will add a new method in interface in that situation I need to override this method in my all classes, it is difficult right.

like this situations which one is better(As of I know abstract class is better is ther any better solution)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

We have a FAQ about Interface vs Abstact classes : here.


[My Blog]
All roads lead to JavaRanch
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Reshma reddy wrote:
For Ex: in my application I have 200 classes and I have one interface, I am using this interface in 50 classes, In future I will add a new method in interface in that situation I need to override this method in my all classes, it is difficult right.


"Open to extension close for modification." - Specially if you are coding a program with about 200 classes

Yes but adding a new method to an interface will break all implementation that extend it (It will not break any implementation that use the interface only classes that implement it). Same thing may happen if you add a abstract method to an abstract class.

Also remember in Java we can only extend one class at a time so having a lot of abstract classes create their own problems.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

And you might consider a combination of the two using Adapter patterns:


Uh-oh, 200 classes down the road I realize I need to add a 'dropWaste(int size)' method


Now for those implementations deriving from AnimalAdapter that don't need a special version of 'dropWaste' I don't need to modify them. If some animals needs a special implementation of dropWaste I still have to edit those special animals, though. Callers don't care. And, if I come into a multiple - inheritance situation I can implement the Animal interface directly, rather than extend the AnimalAdapter.

A down side is that if all/most of my Animals need a special implementation of dropWaste then I don't get a fancy compile-time warning saying the method wasn't added to the class. The compiler error can save a lot of bugs in the run-time by reminding you of things like this.


Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface Vs Abstract class