Wish Object-oriented programming to B4X: Inheritance + Polymorphism

b4auser1

Well-Known Member
Licensed User
Page is empty.

I don't think that inheritance will ever be added to the language.
It's difficult to implement or there are other reasons ?

Polymorphism is supported through the usage of CallSub (duck typing).
I use CallSub as all other B4X developers very widely. But there are problems with not checking types compatibilty at compilation phase

https://www.b4x.com/android/forum/t...t-warnings-if-types-are-not-compatible.64341/

It's not just due to using of CallSub of course.
 

Erel

Administrator
Staff member
Licensed User

wonder

Expert
Licensed User
I offer to add to B4x Inheritance + Polymorphism.
+1

In my opinion have the possibility to do something like this could make B4X the best language ever for mobile game development!
B4X:
class GameCharacter
{
    public:
        virtual void attack() { std::cout << "The opponent attacks."; }
}

class Knight : public GameCharacter
{
    public:
        void attack() { std::cout << "The knight thrusts his sword."; }
}

class Archer : public GameCharacter
{
    public:
        void attack() { std::cout << "The archer fires an arrow."; }
}

The current state of affairs is not a deal breaker, but the kind of solutions I have to use aren't the most elegant:
B4X:
'Class module: GameCharacter
Sub Process_Globals
    ...
    Dim KNIGHT = 1 As Int
    Dim ARCHER = 2 As Int
    ...
End Sub

Sub Attack(category as Int)
    Select category
        Case KNIGHT
            Log("The knight thrusts his sword.")

        Case ARCHER
            Log("The archer fires an arrow.")

        Case Else
            Log("The opponent attacks.")

    End Select
End Sub
 
Last edited:

Erel

Administrator
Staff member
Licensed User
This is not the "correct" implementation in B4X.

Better:
B4X:
'GameCharacter class
Sub Class_Globals
   Public mName As String
End Sub

Public Sub Initialize (name As String)
   mName = name
End Sub

'Archer class
Sub Class_Globals
   Private gc As GameCharacter
End Sub

Public Sub Initialize (name As String)
   gc.Initialize(name)
End Sub

Public Sub Attack
   Log($"${gc.mName} the archer fires an arrow"$)
End Sub

'Knight class
Sub Class_Globals
   Private gc As GameCharacter
End Sub

Public Sub Initialize (name As String)
   gc.Initialize(name)
End Sub

Public Sub Attack
   Log($"${gc.mName} the brave knight thrusts his sword."$)
End Sub

'main module (b4j - non-ui):
Sub Process_Globals
   Private characters As List
End Sub

Sub AppStart (Args() As String)
   characters.Initialize
   For i = 1 To 100
     If Rnd(0, 2) = 0 Then
       Dim k As Knight
       k.Initialize("k" & i)
       characters.Add(k)
     Else
       Dim a As Archer
       a.Initialize("a" & i)
       characters.Add(a)
     End If
   Next
   For Each c As Object In characters
     CallSub(c, "Attack") 'duck typing
   Next
End Sub

You need to add all the shared logic to GameCharacter.
 

b4auser1

Well-Known Member
Licensed User
I use the approach described by Erel widely.
There is a small inconvenience if there is more than one level of Inheritance. Class B includes A. Class C includes B and etc.
To achieve methods or properties of base class it's necessary to use several points.
cObject.bObject.aObject.Method
or repeat implementation of all methods of base class in the Inheritor class.
 

Roberto P.

Well-Known Member
Licensed User
I think it would be great improvement for Tools B4X have the ability to have inheritance (both objects and interfaces).

For me, it is the only big limit present in various tools, particularly in B4A. This results in duplication of code for objects that are very similar and that could result from a common object basis.

Erel, it would be great to create objects "outside B4X", in Java, and be able to derive the B4X classes from these classes.

I hope it's been helpful ...

Greetings :)
 

LucaMs

Expert
Licensed User
+1


It's difficult to implement or there are other reasons ?
It is not difficult at all. A programming language is not a random bag of features. I don't see it as a good fit for B4X programming language.
Maybe it can not be "a good fit for B4A" developers (I'm not so sure) but for B4J developers?
 

Dave O

Active Member
Licensed User
@Erel, you mention that "You need to add all the shared logic to GameCharacter".

From outside the classes (e.g. from AppStart), how would you call methods that are defined in the GameCharacter base class?

For example, if the GC class had a vanilla Hello method (used by the Knight and Archer classes without customization), how would you call Hello from AppStart? Can it be made visible and callable directly, or would you need to implement a Hello method in the Knight and Archer classes that then called gc.Hello?

(I'm writing a poker app that will have several types of players (e.g. stupid, average, smart), and I'm trying to decide how to model the classes and methods for them, without endless Select Case blocks. If I can use composition instead of inheritance or interfaces, that sounds promising.)

Thanks!
 
Top