• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class and Interface  RSS feed

 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I know this question might sound trivial. I know the differences between Abstract classes and Interfaces. Could you please provide me with real world examples. Examples like cats and dogs makes sense in books but when I start to do a project how do I decide when I need an Interface and when I need an Abstract class? I would really be glad if someone who is in the Industry can give me examples of few real world Interfaces and Abstract classes
Many Thanks
Muni
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is just a contract that an object can fulfill. It includes no implementation.

An abstract class is best used when you can provide, in a base class, the vast majority of the implementation, but you need to defer to subclasses for implementation of a small proportion.

Because a class can implement any number of interfaces, but can inherit only one superclass, using interfaces makes it easier to extend and maintain your code later. So, choose an interface over an abstract class, if there isn't some other big reason for a different choice.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a very good and precise information Peter.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another use for the interface concept, called the "marker" interface.
You can write an interface which is absolutely empty!Yes. Really!
Since a java class can implement multiple interfaces, any class can implement such a "marker" interface.

Imagine some code which has a method call say allow(Object object). This method is going to allow only certain type of objects to be processed. So you can have something like



Since you know what kind of object are legal objects, you just declare these objects to be implementing the IMarker interface.

Such approach is typically used to keep the code loosely coupled. Since the arguments are of Object type, which as you know is the cosmic super class and IMarker is an interface, you dont need to change the allow method at all in case you change any of the objects which you are passing as arguments.

Does this make sense or would you like some code to make things clear?
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for taking time to explain mate!

I would love to make this concept clearer and some code would be gratefully appreciated!

Just wondering under what scenarios would you make a loosely coupled method you mentioned in the example.

Regards
Muni
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I am posting some sample code which should clear up things. And yes it is not a "cat and dog" example. I hate those kind of examples too!

This is the "marker interface.


This is another interface which supplies the method to compute the size.


Now we have a concrete implementation of the IMarkerSizeInfo for the FILE object.


Now we have your class which is extending the File but with some added information.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to post the reply like this but the editor window is too small for me and scrolling is hell.

We have another concrete implementation which accepts the Rectangle object instead of File




As you can see the first level of checking is always for the IMarker interface. Only then the computation takes place.

Of course in reality, one should check the IMarker type before invoking the compute size for method. In that case there will be a factory which will return the corresponding concrete implementation class depending on the type of object. However to make things easier to figure out, I have coded it this way.

Hope this helps.
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a brilliant example. Thanks a ton! If you live around Hyderabad let me know,I owe you a nice chilled beer
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beer! Now you are talking man!
Unfortunately I am from Pune. So I will have to hold you on to it for the future
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!