• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

force function call

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to ensure all child classes will always call a particular implemented function of base class? i don't want to put it in finalize method of base class because i don't want to rely on garbage collector.
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) Are you trying to ensure that every call of a particular method will be handled by the base class?
2.) Are you trying to force every derived class to call some base class method during its (the derived class) member method execution?
3.) Are you trying to make a call before/after every call to a child class method?

A few possibilities (from obvious to obtuse, and mapped by index to the above questions):

1.) don't implement the method in the child classes
2.) call super.method() in the child class methods
3.) use AOP methods to inject the calls where needed

Let us know what you are trying to accomplish.
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be possible to perform the method call in the parents constructor?

/Svend Rost
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Subhajit Mitra:
how to ensure all child classes will always call a particular implemented function of base class? i don't want to put it in finalize method of base class because i don't want to rely on garbage collector.



Your question is not clear.Could you please explain this in detail.

Framework class implement the same using the following pattern:

abstract class BaseApollo {

public final String doMethod(){
String a = myImplementation();
return a;
}

protected abstract String myImplementation(){}
}

// From the framework class
...
...
BaseApollo bApollo = new BaseApolloImpl();//This is a class extended from BaseApollo and myImplementation is concrete.

String returnString = bApollo.doMethod();

This would ensure that myImplementation is called.

You cannot rely on GC as this is not guaranteed , so finalize might never be called.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP reference to finalize() makes me wonder if he wants to run some kind of cleanup or teardown after some other stuff. We can add to Rahul's example to make that kind of thing happen:

prelude and postlude can be concrete private methods in the base class so that the derived classes never even need to know they exist.
 
Subhajit Mitra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:


Your question is not clear.Could you please explain this in detail.



Let me explain with codes


If I forgot to endService() in LoginService class database connection will not close and wait till garbage collection. This will cause dirty connection leak. So I am finding a way where some kind of errors will be shown if someone neglect to call endService();

is AOP is the solution? if yes how to implement it.
[ November 20, 2006: Message edited by: Subhajit Mitra ]
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If I forgot to endService() in LoginService class database connection will not close and wait till garbage collection.



According to your example it won't, even though subclass(LoginService) doesn't implement endService() super class version(endService()) will run and close the connection.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want users to call a method defined on the base class, never directly call the method on the derived class. The base class method calls startService, the derived method, and endService.


The client only knows the interface. It never knows the doAuthorization method exists so it cannot call it out of turn. The isAuthorized method is final so no concrete class can override it to skip start & stop service.

That was kind of fun. Did it make sense?
 
Subhajit Mitra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Stan. it was really nice. it has solved my problem.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Subhajit Mitra:
[QB]




I feel its ok and endService would always be called.However you can modify the program in the following way.

* If startService(); throws exception then connection has not been created , so thats OK.

* If startService(); doesnot throw any exception the flow reached the method
Authintication code.In that you can do the following thing to handle the exception so that the flow reaches the third method endService() to tear down the connection.

authintication(){
try{
//your code
}catch(Exception e){
//LOG exception.
}
}

One more thing that is not advisible in this senario is creating and tearing connections for every authintication.You should be using pool for this.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic