Abstract and protected methods

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Abstract and protected methods

Post by Witchi »

Are there such things like abstract methods or protected methods within Gambas?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Abstract and protected methods

Post by cogier »

Witchi wrote: Thursday 24th February 2022 1:25pm by Witchi » Thursday 24th February 2022 1:25pm

Are there such things like abstract methods or protected methods within Gambas?
Wow! I had to look that up. I am still not sure what it all means, but you can 'Inherit' items into a class and add methods, variables and events, if that helps. What are you trying to achieve?
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Abstract and protected methods

Post by AMUR-WOLF »

Witchi wrote: Thursday 24th February 2022 1:25pm Are there such things like abstract methods or protected methods within Gambas?
No. If you need abstract method, you can do this:
Public Sub Method() 

  Error.Raise("Not implemented method invocation!")
 
End
Methods are only private or public.

Gambas, like its parent Visual Basic, is designed for rapid desktop GUI developing. It has poor OOP capabilities, but enough for simple desktop apps and thick clients.
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Re: Abstract and protected methods

Post by Witchi »

cogier wrote: Thursday 24th February 2022 2:33pm
Witchi wrote: Thursday 24th February 2022 1:25pm by Witchi » Thursday 24th February 2022 1:25pm

Are there such things like abstract methods or protected methods within Gambas?
Wow! I had to look that up. I am still not sure what it all means, but you can 'Inherit' items into a class and add methods, variables and events, if that helps. What are you trying to achieve?
Abstract methods must be re-implemented within sub-classes, so the base class can call it, but the code comes from the subclass. So you can implement different things in every subclass, which the base class can call.

Protected methods are methods which are only accessible from sub-classes. Public methods are visible for every other class, private methods are only visible within its own class. So a way between both are protected methods, which cannot be executed by external code, but from the own class and sub-classes.

Both are essential things for OOP. :(
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Re: Abstract and protected methods

Post by Witchi »

AMUR-WOLF wrote: Thursday 24th February 2022 4:24pm
Witchi wrote: Thursday 24th February 2022 1:25pm Are there such things like abstract methods or protected methods within Gambas?
No. If you need abstract method, you can do this:
Public Sub Method() 

  Error.Raise("Not implemented method invocation!")
 
End
That is not the same. Here I can override the Method(), but if I call it from the declaring class, it won't execute method overridden by the subclass. You can see that within the attached example project. I have to define a lot of things as public (instead of protected) in the base class and also set these properties from within the subclass constructor _new() to change the behaviour of the base class for every sub-class.
Attachments
abstract-method.zip
example project for abstract methods
(14.32 KiB) Downloaded 152 times
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Abstract and protected methods

Post by AMUR-WOLF »

Witchi wrote: Thursday 24th February 2022 10:37pm That is not the same. Here I can override the Method(), but if I call it from the declaring class, it won't execute method overridden by the subclass. You can see that within the attached example project. I have to define a lot of things as public (instead of protected) in the base class and also set these properties from within the subclass constructor _new() to change the behaviour of the base class for every sub-class.
Yes, that is not the same. Classes in Gambas are 100% realistic and 100% complete. Each base class is ready to use, so, if you invoke a method from base class it behaves as it did in base class. If you want to change this behaviour, you still can do it, but explicitly:
' Gambas class file

Inherits AbstractBaseClass

' Explicitly change behaviour of base class
Public Sub helloWorld() As String

  Return printme()

End

Public Sub printme() As String

  Return "Hello from subclass B"

End



Also, when you create an object of child class, a constructor of base class is invoked first (unlike in Java).

Abstractions are not essential for OOP, but help to build huge applications with heavy architecture. Sometimes I think that idea of 100% realism and 100% completeness is Zen. But I really need protected members, interfaces and generics.
Last edited by AMUR-WOLF on Friday 25th February 2022 7:31am, edited 1 time in total.
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Abstract and protected methods

Post by AMUR-WOLF »

In Java, hierarchies of classes caused problems while long distance code maintaining. So, design pattern "Strategy" was invented (story).

Probably you can achieve your aims via design pattern "Strategy":
pattern-strategy.zip
(14.89 KiB) Downloaded 178 times
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Re: Abstract and protected methods

Post by Witchi »

That is right, with Strategy I could emulate my abstract method problem. Thank you.

Is there a guide for Gambas object model? Like a document which compares OO things in Gambas with other languages and some work-arounds? I have a lot of questions in that way. So I find the constructor parameter thing very strange in Gambas or what is with re-declaration of variables in sub-classes? Such things will be tricky, if you have already develop apps in C++, Java or PHP.
Post Reply