Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-08-2010
Member
 
Join Date: Mar 2010
Posts: 383
Static method in Abstract class

Hello,
Firstly, I understand the reasons why an interface or abstract class (in the NET / C # / java terminology) can not have abstract static methods. My question is more focused on the best design solution. What I want is a set of helper classes that all have their own methods such as static, if I get the objects A, B and C from a third party vendor, I can have classes assistance with methods such as
Code:
  AHelper.RetrieveByID (string id); 
  AHelper.RetrieveByName (string name); 
  AHelper.DumpToDatabase ();
Since my AHelper, BHelper, and classes will CHelper all basically the same methods, it seems logical to move these methods to an interface that these classes can be derived. However, wishing that these methods are static does not allow me to have a generic interface or an abstract class for all of them to draw conclusions. I could always do with these non-static methods, and then instantiate objects such as the first
Code:
  AHelper AHelper a = new (); 
  a.DumpToDatabase ();
However, this code does not seem as intuitive for me. What are your suggestions? Should I give up using an interface or abstract class altogether (the situation I'm in the business) or could it possibly be redesigned to accomplish the purpose that I seek?
Reply With Quote
  #2  
Old 02-08-2010
Member
 
Join Date: Dec 2009
Posts: 178
Re: Static method in Abstract class

If I were you I would try to avoid static. IMHO I always ended up a kind of synchronization problems on the road with the static. Having said that you have a classic example of generic programming using jigs. I will adopt the solution based on models presented in one of the java modules.
Reply With Quote
  #3  
Old 02-08-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Static method in Abstract class

In you case the solution will be this code

Code:
public static T RetrieveByID <T> (string iden) 
 (Var flnm = getFieldNamesBasedOnType (typeof (T)); 
   qyres webservice.query qy = ("SELECT" + flnm + "FROM" 
                                      Tyepof + (T). Name 
                                     + "WHERE iden = '" + iden + "'"); 
   return (T) qy.records [0]; 
 )
Reply With Quote
  #4  
Old 02-08-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: Static method in Abstract class

Given your response I think as follows:

- You could just have a static method that takes a type parameter and performs the usual logic based on gender.

- You can create a virtual method in your abstract base, where you specify the SQL in the concrete class. While containing all the common code that is required by both (eg the execution of the command and return the property) while encapsulating the specialist bits (eg SQL) in subclasses.

I prefer the second option, although its course down to you. If you need me to go into details, please let me know and I'll be happy to edit / update.
Reply With Quote
  #5  
Old 02-08-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: Static method in Abstract class

You can not overload methods by varying only the type of return. You can use different names:
Code:
static Aobj GetAObject (string iden); 
static Bobj GetBObject (string iden);
Or you can create a class with casting operators:
Code:
AorBobj class 
 (String iden; 
   AorBobj (string iden) (this.iden = iden;) 
   static public AorBobj RetrieveByID (string iden) 
    (Return new AorBobj (iden); 
    ) 
   public static explicit operator Aobj (AorBobj ab)  
    AObjectQuery (return (ab.iden); 
    ) 
   public static explicit operator Bobj (AorBobj ab) 
    BObjectQuery (return (ab.iden); 
    )  
 )
Then you can call it like this:
Code:
 var a = (Aobj) AorBobj.RetrieveByID (8); 
 var b = (Bobj) AorBobj.RetrieveByID (8);
Reply With Quote
  #6  
Old 02-08-2010
Member
 
Join Date: Aug 2009
Posts: 124
Re: Static method in Abstract class

How are Objects and AHelper related? Is AHelper.RetrieveByID (), the same logic as BHelper.RetrieveByID (). If yes, why not approach a utility class (class with public static methods only and no state) static [return type] Helper.RetrieveByID (ObjectX x).
Reply With Quote
  #7  
Old 12-08-2010
Member
 
Join Date: Mar 2010
Posts: 330
Re: Static method in Abstract class

Personally, I might be wondering why each type of need a static method before even thinking again
Why not create a utlity class with static methods they need to share? (Eg ClassHelper.RetrieveByID (string id) or ClassHelper <ClassA>. RetrieveByID (string id)
In my experience with these kinds of "roadblocks" the problem is not the limits of language, but the limits of my design.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Static method in Abstract class"
Thread Thread Starter Forum Replies Last Post
Problem with abstract class and interface Vodka Software Development 5 08-03-2010 12:20 PM
Abstract class method overloading problem ISAIAH Software Development 5 16-01-2010 10:18 AM
Difference among concrete class and abstract class Roxy_jacob Software Development 4 07-12-2009 01:22 PM
Abstract class in Java SoftWore Software Development 3 07-11-2009 01:58 PM
Abstract class and Interface in .net RogerFielden Software Development 3 04-05-2009 06:07 PM


All times are GMT +5.5. The time now is 06:22 PM.