aspose file tools
The moose likes Beginning Java and the fly likes abstract class and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "abstract class and Interface" Watch "abstract class and Interface" New topic
Author

abstract class and Interface

Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

Thats a very good and precise information Peter.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8433

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?


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8433

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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8433

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

Joined: Aug 23, 2007
Posts: 74
Thats a brilliant example. Thanks a ton! If you live around Hyderabad let me know,I owe you a nice chilled beer
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8433

Beer! Now you are talking man!
Unfortunately I am from Pune. So I will have to hold you on to it for the future
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: abstract class and Interface
 
Similar Threads
Abstract class and interface
Interface and Composition realtion
Naming Interfaces
UML question regarding abstract and interface classes
Object "Savvy"