Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Abstract classes instead of Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract classes instead of Interfaces" Watch "Abstract classes instead of Interfaces" New topic
Author

Abstract classes instead of Interfaces

surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
Hi Friends,





My question is In preceding example both abstract class and interface contains abstract methods,so why can't we use abstract class as a interface(or abstract classes instead of interfaces)?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You can extend from only one class- So for Abstract class- You would have to extend it. But in case of interfaces- You can implement multiple interfaces. Abstract classes can also have concrete methods where as interfaces do not.

Also- Interfaces don't form a part of the Inheritance tree- They can be used across different inheritance trees.


Mohamed Sanaulla | My Blog
Amit Kumar Ghosh
Greenhorn

Joined: Dec 14, 2010
Posts: 3
The Biggest difference is versioning
Let us Think of a scenario where We have a project having multiple classes.
Now we want to add a method so that all classes in the project can access this method, we can either add this new method to abstract class or interface.

In case of interface
if we add this new method to interface, all classes implementing this interface must override this new method
and you will always find it hard to get and edit all classes implementing this interface, even if new method is not relevant to be overriden

[ UseTheForumNotEmail ]
Amit Kumar Ghosh
Greenhorn

Joined: Dec 14, 2010
Posts: 3
just add the method as normal method in abstract class, so you do not need to find and edit all classes to override this new method where this abstract is being extended.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

er.Amit Kumar wrote:The Biggest difference is versioning
Let us Think of a scenario where We have a project having multiple classes.
Now we want to add a method so that all classes in the project can access this method, we can either add this new method to abstract class or interface.

In case of interface
if we add this new method to interface, all classes implementing this interface must override this new method
and you will always find it hard to get and edit all classes implementing this interface, even if new method is not relevant to be overriden

Any problem: mail me amitsbr2004@gmail.com


Usually the design of classes is in such a way that we abstract(does not mean the java keyword) out common features in one class. There are at times that the classes share common features/elements/properties/actions. In that case we move the common features into a Parent class and let the other classes extend this to add Child class specific features. So it doesn't mean that if all the classes require some method we move them to a class and then let the other classes extend this. Abstract classes are used when you want certain method/s to be compulsorily overridden by the child classes along with the Abstract class having some concrete methods as well. In that case you declare that method/s as Abstract in the Abstract class. Also note that an Abstract class can also exist without any abstract methods.

Also in case of Classes extending an Abstract class- They have to override the method (unless the extending class is also declared as Abstract). Even when you have an class implementing an Interface, the class can be declared as Abstract to avoid overriding any of the methods of the Interface.

And there is a clear disadvantage of using- Interfaces- We have to override all the methods part of the Interface, but that's a trade off for the capabilities Interfaces are providing as an alternate to multiple inheritance. So the newer languages are coming up with concepts like- mixins.

This article might be of some help- http://mindprod.com/jgloss/interfacevsabstract.html
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
The example I like is one for which a company has written a program. They have now hired you to add to features that they either do not have time to write themselves or the know how. They do not want to give you their code, so they give you an interface. Now you can use their product to add your features and you can't see their code, nor how it works. You just know that when you send 'x' information to their classes through the interface, you get 'y' information back for your part of the product.

This is also helpful in the scenario where a company provides you with basic programming, but to keep your own proprietary operations a secret, you use their interface, implement your own code, and no one knows anything about the others source...just the outcome.

An abstract class, though, you can see the concrete methods that are inherited, because you would then have access to the code that makes it work.

If my scenario is incorrect, please someone help me out. I am still fairly new, but that is the basic understanding I have between the two and it is how I approach tasks when I need to worry about either of those subjects.


Jason K. (AC)
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
when to use abstract class or interface is the interesting questions long time back I was worried about. I think it's depend on your requirement and but also there is a trade-off.

most people tend to design their application with use of interfaces.yes it's true it gives more flexibility to the system but you might be experiencing consequences without selecting a right guy for right job.

Technically, you are free to use whether abstract class or interface.I would approach this question form the scratch.By definition abstract class is a class which can't be instantiated.Indeed it does contains abstract ,non-abstract methods,instance variables.when It comes to interface, it does only have method declarations nothing concrete with in it.in general terms it specifies the contract to the outside world what you can do if you implement the interface.

According to my knowledge, these are the situations where you worry when you use interface or abstract class.

1.) Let say for a example, you come across a situation where you have to share common behavior among class.if so, abstract class is the right guy for the job.Interfaces does't cater for this need.(e.g. Think about the Fish abstract class and breathe method,most of the fish breathe in a same way so can share common behavior among others).I mean in a nutshell, abstract class helps you to structure the classes and helps define common behavior among others.

2.)Interface cater for multiple interface inheritance,more over you are not restricted with class hierarchies, you can just plug the interface when you need it.For a instance, consider the bounceable interface which has the bounce() method.There might be classes like Ball,Ballon which has bouncing capability but they are in distinct class hierarchies. Who is the guy comes to help you out.It is a interface.Finally use interface when you need to give responsibilities to distinct classes which doesn't share common structure.

This is the good reference if you need to distinguish difference between Abstract class & Interface.Interfaces vs Abstract class


Regards,
Nuwan Arambage



Thinker
Nuwan Arambage
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract classes instead of Interfaces