B4J Question Create generic object

PatrikCavina

Active Member
Licensed User
Longtime User
Hi to all,
I would know if it's possible create a generic object like 'Node' that can contain other objects of the same kind.

Example:
B4X:
    Dim tb As TableView
tb.Initialize("")
Dim n As Node = tb
 

PatrikCavina

Active Member
Licensed User
Longtime User
I'll try to explain me with an example:

I've three class
- Dog
- Cat
- Animal

And i want to reproduce the same syntax of the first example

B4X:
Sub Process_Globals
  
    Private myDog As Dog
    Private myCat As Cat
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
    myDog.Initialize
    myCat.Initialize
   
    myDog.Age = 12
    myCat.Age = 14
       
    Dim myAnimal As Animal = myDog
    myAnimal.Age = 5
   
    Log(myDog.Age) '<-------- Result = 5
   
End Sub

Where Animal Class is a generic class, that can take myDog and myCat object and manage them.
Like Node object can be any kind of node like button, label, ecc.. and manage them.
I hope I have been clearer.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You are talking about custom types!

Just type "type" in the search engine!
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I dont think it will work like u wrote above. An object cannot be handled as 2 different objects. If cat and dog are different then animal cannot be one time a cat and another time a dog. You should do it in a different way like

Type animal(age as int, name as string, color as int)

Dim dog as animal
Dim cat as animal

...
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
With one big limit: you can set properties (without any validation), this way, but you cannot have methods.

you can create a class for Animal and have also methods and properties and declare cat and dog as animal and do whatever you want.
i understand what @PatrikCavina wants. he wants to have a class of animal and then an extended class for dog and for cat. where dog and cat have her own properties and methods but both can be handled as "Animal" like you can do in JAVA.

B4X:
public class Dog extends Animal{
'...
}

but as far as i know it is not possible in B4X, however, you can implement a similar option in b4x that will give you the same or almost the same result.
 
Upvote 0

PatrikCavina

Active Member
Licensed User
Longtime User
Thank you to all for answers.

he wants to have a class of animal and then an extended class for dog and for cat. where dog and cat have her own properties and methods but both can be handled as "Animal" like you can do in JAVA.
It was excatly what i wanted.
I didn't remember the name of mechanisms, that if i understand, should be the inheritance and polymorfism.
Inheritance isn't implemented in b4j because it use composition and polymorfism is covered through Duck Type.
So what i wanted to do isn't possible.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
As mentioned in previous post there are several ways.

Have scribbled attached example, with a superclass Bird and two subclasses Parrot & Penguin.
The subsclasses inherit all the attributes and methods from Bird beyond their own attributes and methods.

Snippet from Class Main
B4X:
Public RedParrot As Parrot 'Subclass from Bird
Public FunnyPenguin As Penguin 'Subclass from Bird

RedParrot.Initialize("RedParrot", "Bird", "1958-08-09", "RED")
RedParrot.Fly 'Parrot RedParrot is flying.
Log($"Name: ${RedParrot.Bird.Name}"$) 'RedParrot

FunnyPenguin.Initialize("FunnyPenguin", "Bird", "2016-06-10")
FunnyPenguin.Swim 'Penguin FunnyPenguin is swimming.
Log($"Name: ${FunnyPenguin.Bird.Name}"$) 'FunnyPenguin
Log($"DateOfBirth: ${FunnyPenguin.Bird.DateOfBirth}"$) '2016-06-10

In each of the subclasses using Setter&Getter Subs to access the superclass Bird, i.e FunnyPenguin.Bird.Name
B4X:
Sub Class_Globals
   Private mBird As Bird
...
End Sub

Public Sub Initialize(Name As String, ...)
   mBird.Initialize(Name,...)
End Sub

Public Sub setBird(Value As Bird)
   mBird = Value
End Sub
Public Sub getBird As Bird
   Return mBird
End Sub

...

Note: First time build a solution like this, so if better ways to do so, please share.
 

Attachments

  • B4JHowToPolymorphism.zip
    2.9 KB · Views: 276
Upvote 0
Top