function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VKrishVKrish 

How to call parent class method from a web service class

I am new to apex programming.

I am writing a webservice class. Many of the functions I need is already available in one of the base class. But when I extend the base class in webservice class & call the functions, I get the error that method doesnot exist or incorrect signature. I googled and found that the static methods cannot override other method.

Is there a different way to implement the functions I already have in the webservice class?

Ex:

// base class

global virtual class Base{

  public boolean setMember(){

  // code

  }

  public string getMemberName(){

  //code

  }

}

// webservice class

global class Child extends Base{

  WebService static string getMemberName(){

  // repeate same code

  }

  WebService boolean string setMember(){

  // repeate same code

  }

}

 

I know I could call the base class method in java. But it is not allowed in apex.

Is there a way to avoid repeating same code in both the classes?

 

Any ideas is appriciated.

Thanks in advance

kiranmutturukiranmutturu

if you remove the virtual keyword then u can call the method in that webservice class..

VKrishVKrish

My webservice class has to be global. And global class can extend only virtual or abstract classes. So my base class has to be virtual to be extended in another global class. :(

subaasubaa

Krish,

 

Are you sure, you have used 'public' modifier for the method(s) in virtual class? Please correct me, if 'global' modifier is equalent to 'public'.

 

Regards,

SuBaa

VKrishVKrish

Well, I have used global in class because some of the variables are accessed through other application through webservice call. Although methods are not called by other applications, so I just use public there.

Any way that doesn't solve my problem. Static methods can override only abstract methods. :(

 

Here is what I have done to overcome the problem:

No inheritance, No extends, Just created global static instance of the base class from the webservice child class & access the methods of the base class. Since the instance is static, in every method of child class I call, I have to check if the instance is null or already has value.

Ex:

global class ChildClass{

    global static BaseClass b {get;set;}

    webserice static string getMemberName(string memberId){

       (if b == null) b = new BaseClass();

       return b.getMemberName(memberId);

   }

 }

 

I know this is not an ideal solution. But couldn't think of any other way to avoid code repetition in given short time. 

So better ideas are welcome.