Java Question Implementation code of abstract method

moster67

Expert
Licensed User
Longtime User
I have not seen much about abstract classes here in the Java library developer help forum.

I am wrapping a SDK which includes an abstract class with abstract methods. The abstract class is to be used if one wants to support an external audio device, for instance a Watch.

OK, I know the basics i.e. I will need to extend this abstract class and then write the implementation block for the abstract methods.

This abstract class has for example an abstract method with the following signature:
B4X:
public abstract int configure(int var1, int var2, int var3);
It is supposed to return 0 if configuration was successful otherwise another number to indicate an error-code.

At this phase, there is no need to support an external device and thus there is no code to write for the implementation code/block.

However, if the B4A-developer who is using my wrapper of this SDK wants to support an external audio device, I guess the B4A-developer will probably need the implementation code to be written. So my question is :
can this be done only in the wrapper or can I somehow write my wrapper so the implementation code can be written directly in B4A?

PS: Also among Erel's many projects I don't think I have never seen any example where we write the implementation code of the abstract method in B4A

Thanks.
 

moster67

Expert
Licensed User
Longtime User
Let us say I have this sample code in my wrapper (not related to above):
B4X:
public abstract class Maths {

    public abstract int addition(int x, int y);
    public abstract int subtraction(int x, int y);
    public abstract int multiplication(int x, int y);
    public abstract void saySomething();

}

wrapper:
B4X:
@BA.Events(values = {"OnAddition(x as int, y as int) as int", "OnSaySomething()",
        "OnSubtraction(x as int, y as int) as int", "OnMultiplication(x as int, y as int) as int"})
@BA.ShortName("MathsWrapper")
public class MathsWrapper extends AbsObjectWrapper<Maths> {

    private BA ba;
    private String eventName;
    private Maths mat;

    public void Initialize(final BA ba, String EventName) {
        this.ba = ba;
        this.eventName = EventName;
        mat = new Maths() {
            @Override
            public int addition(int x, int y) {
                Integer result =  (Integer) ba.raiseEvent2(MathsWrapper.this,false,eventName+"_onaddition",false,x,y);
                if (result == null)
                    return -1;
                return result;
            }

            @Override
            public int subtraction(int x, int y) {
                BA.Log("in subtraction method");
                return 0;
            }

            @Override
            public int multiplication(int x, int y) {
                BA.Log("in multiplication method");
                return 0;
            }

            @Override
            public void saySomething() {
                BA.Log("in saysomething method");
                ba.raiseEvent(MathsWrapper.this,eventName+"_onsaysomething", new Object[]{});
            }
        };
        setObject(mat);
    }


    @BA.RaisesSynchronousEvents
    public void SaySomething(){
        mat.saySomething();
    }

    @BA.RaisesSynchronousEvents
    public int Addition(int x, int y){
        return mat.addition(x, y);
    }

}

In B4A I call the addition-method:
B4X:
Starter.ptMath.Initialize("ptm")
Dim result as Int = Starter.ptMath.Addition(50,40) 'edited
Log(result)

'B4A-events
Sub ptm_OnSaySomething()
    Log("mysaysomething")
End Sub

Sub ptm_OnAddition(x As Int, y As Int) As Int
     return MyAdditionMethod(x,y) 'edited
End Sub

Sub MyAdditionMethod(x As Int, y As Int) as Int 'edited
    Dim z As Int = x + y
    Log("This is the result " & z)
    return z
End Sub

So where do I put the implementation code of the abstract Addition method? Shall I implement it in the wrapper or should it be done in B4A?

I am bit confused o_O:eek::)

Edited: Played around again with above code. Only needed to change a few things in the B4A-code (see 'edited notes) and now everything seems to work fine. At this point, I guess my implementation code (the method body) of the abstracted method corresponds to the code in the sub MyAdditionMethod in B4A. So if a user needs to implement the method body, it can be done directly in B4A and not necessarily in the Java wrapper. That is is good news. I hope I got this right.
 
Last edited:

moster67

Expert
Licensed User
Longtime User
I think I managed to get every piece of the puzzle in their right places. It seems to work now. See my edit in my previous post.
 
Top