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

abstract class

Njk Naresh
Greenhorn

Joined: Nov 16, 2006
Posts: 11
Hi, all this is a sample code of abstract class and it will get compiled , execute.

But i couldn't understand where we use this scenario. An abstract class without any abstract method.


public abstract class DemoAbstract
{
public static void main(String args[])
{
System.out.println("inside main");
}
}
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Consider the following situation.

I have to write two services for my application according to the transport layer protocol used.For TCP one service and for UDP another one.

But I know that there are some common functionalities (some helper functions for which would be required by both the services) for both the services.In that case I can write a abstract class (Service)having those common functionalities and later I can extend that class for making the main service classes (like TCPService and UDPService).The reason I made the base class abstract as the base class is not enough concrete to work but the sub classes are .As the sub classes would be complete in all senses to server the request according to the protpcol type.

hope this helps~


Rahul Bhattacharjee
LinkedIn - Blog
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
But in your code you would need to use references to the Concrete Service (thus your code is bound to TCPService or UDPService and you can not switch) UNLESS there is
either a common Service interface or the implementations override methods from the abstract superclass or you pass configuration trough the constructor.
If there are no abstract methods (and no overridden methods), then the superclass has no way of invoking implementation specific behaviour. Like this you are forced to invoke methods which are in the concrete class only.


So you wont see an abstract class without abstract methods that often.


Do i make sense here ? :-)

Hey it's friday ....

Pascal
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Hi Pascal,

Yes .you are right that at runtime I cannot switch between the two services.
This example is not from any real time application.I thought of this only for siting an example.

For example I have a application that runs on tomcat and can be configured for any one of the two adapters/services and we can pass the service class name using a property file (either TCPService or UDPService )to the application and cannot be changes in runtime.In this case specifying only serice would not help as this is not protocol specific and makes no sense.(in other words not concrete enough to serve a request.)

So if anyone wants to write another adapter (for any other transport layer protocol like XYZ to write a class XYZService extends Service{}), he can simple extend the service class (which has all the common functionalities required for a service and abstract as alone it cannot suffice any problem)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Well if you just think about code reuse, then an abstract class without abstract methods might make sense. But if you want to make use of polymorphism, choose an implementation at runtime, ... then this does not make much sense.

Rahul:
You can not even switch at config time (which is actualy runtime) because the abstract class has no way of invoking methods of the concrete classes and therefore you will need to call methods of the concrete classes in your code.



if in your code you reference AbstractService, then you can not invoke the doIt() method. So you would need to add the doIt() to AbstractService and overriding it in the TCP/UDP Service. Or make it abstract and implement it.

but if you define AbstractService like this:


then you can reference AbstractService in your code and choose the implementation trough config, lookup, injection, ...

pascal
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Yes Pascal , I agree that some contract must be there between the service class and the caller class.Before specifying this example I mentioned that its for only siting an example.



I am making specificCode as a empty implementation for siting in this example.but ideally it shoud be a abstract method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class