Inheritance, Inheritance, Inheritance

AnandGupta

Expert
Licensed User
Longtime User
I too want it.
Have the habit of it in other languages.
 

aeric

Expert
Licensed User
Longtime User
I seen in previous posts Erel mentioned that Composition is preferred and supported. I only started to use B4J so I don’t know how Inheritance is useful. I don’t really use Inheritance /SubClassing in VB6 or VB.NET. Even Polymorphism I don’t remember when was the last time I use CallSub.
 

LucaMs

Expert
Licensed User
Longtime User
About Composition... I don't understand how this can replace inheritance :(

This could be one way:

Composition.jpg


clsMan instance declared as public in clsEmployee (it's a bit risky but it can be done).
Using "com" as a prefix (comMan - component) can be helpful.
 

LucaMs

Expert
Licensed User
Longtime User
clsMan instance declared as public in clsEmployee (it's a bit risky but it can be done).
By declaring the "parent object" public, however, you have a big problem if you want to override a member; there would be two methods with the same name, one in the child class, one in the exposed object.

This problem does not exist by declaring the object as private; but in this case it will be necessary to create many "delegating" members.
 

aeric

Expert
Licensed User
Longtime User
I am not sure I understand correctly. I rarely use classes and inheritance. Usually I work with database tables. Now I am learning to use Type keyword. :)

1631951427716.png
 

Attachments

  • InheritanceTest.zip
    4.6 KB · Views: 107

aeric

Expert
Licensed User
Longtime User
Or set the Parent class variable as Private.

B4X:
Sub Class_Globals
    Private mName As String
    Private mAge As Int
End Sub
 

Attachments

  • InheritanceTest2.zip
    4.6 KB · Views: 106

LucaMs

Expert
Licensed User
Longtime User
What you did is not use Inheritance (also because you couldn't, there is no Inheritance in B4X) but Composition.

You had to write ALSO in the "child" classes the same members of the "parent" class and then from within these you called the members of the "parent" (in practice you have delegated the execution to the "parent").

That is, you did what I wrote here:
but in this case it will be necessary to create many "delegating" members.
 

AnandGupta

Expert
Licensed User
Longtime User
Call it inheritance, encapsulation .. etc. I have no qualms for it. What I want is, I am trying to state in below pseudo codes,

B4X:
class line
    var length

method draw
    ' draws the line of given 'length'

B4X:
class square from line
    var area

method draw
    ' draws the square of given 'length'

Note, 'length' is in 'line' class and both have 'draw' methods. square class inherits line class.

Now I use them as,

B4X:
oSquare := class square()
oSquare.length := 10
oSquare.draw()

If we can get it in b4X then I think most of our codes will be concise and clean.
I am using this type of codes in my other programming languages.
 
Top