• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Abstract classes instead of Interfaces

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 [email protected]



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
 
Ranch Hand
Posts: 36
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic