System.out in each Base Class

V

vnssoftware

Hi,
I have a Interface with two methods.
Each BaseClass implements these.
I want to log when these methods get executed using System.out.
Do I have to add System.out in each of the BaseClass or there is a
better way?
Can anyone give a Suggestion such that I don't have to go and write
System.out in each of those methods.
Thankyou.
 
J

John C. Bollinger

vnssoftware said:
I have a Interface with two methods.
Each BaseClass implements these.
I want to log when these methods get executed using System.out.
Do I have to add System.out in each of the BaseClass or there is a
better way?
Can anyone give a Suggestion such that I don't have to go and write
System.out in each of those methods.

There is no way to specify implementation details with an interface.
The possibilities I see are these:

(1) Put the necessary code in an abstract base class from which all
implementations inherit
(2) Use helper classes (perhaps inner classes) to implement the
interface instead of having the main classes do it, and give the helper
classes a common ancestor that provides the necessary methods
(3) Invoke the methods in question via some sort of proxy object that
handles the logging
(4) Use AspectJ and define the behavior you want as an appropriate
aspect. (Unless you are already interested in or using AspectJ, this is
probably more work than you want to undertake.)


John Bollinger
(e-mail address removed)
 
M

Mr. X

You could use an abstract class and provide bodies for each of the methods,
only containg the System.out. Then just call via super.method() in the
extending classes methods. Other than that, the only way to do it is the way
you suggest: a System.out in each implemented method.
 
B

brougham5

Hi,
I have a Interface with two methods.
Each BaseClass implements these.
I want to log when these methods get executed using System.out.
Do I have to add System.out in each of the BaseClass or there is a
better way?

Use a real logging facility other than system.out. Make it a habit. If
you're using 1.4 or higher, look at the java.util.logging package.
Otherwise, google "log4j" and download it.

Look up "template pattern" in google or elsewhere. Basically...

abstract class Base
{
public void foo()
{
System.out.println( "entering foo" );
this.doFoo();
System.out.println( "exiting foo" );
}

abstract protected void doFoo();

}

class Derived
extends Base
{
protected void doFoo()
{
System.out.println( "doing derived work" );
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top