Abstract and protected methods

Post

Posted
Rating:
#1 (In Topic #825)
Trainee
Are there such things like abstract methods or protected methods within Gambas?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’

Witchi said

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?
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

Witchi said

Are there such things like abstract methods or protected methods within Gambas?

No. If you need abstract method, you can do this:

Code (gambas)

  1. Public Sub Method()
  2.  
  3.   Error.Raise("Not implemented method invocation!")
  4.  
  5.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee

cogier said

Witchi said

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.  :(
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee

AMUR-WOLF said

Witchi said

Are there such things like abstract methods or protected methods within Gambas?

No. If you need abstract method, you can do this:

Code (gambas)

  1. Public Sub Method()
  2.  
  3.   Error.Raise("Not implemented method invocation!")
  4.  
  5.  

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.

Attachment

example project for abstract methods

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

Witchi said

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:

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Inherits AbstractBaseClass
  4.  
  5. ' Explicitly change behaviour of base class
  6. Public Sub helloWorld() As String
  7.  
  8.   Return printme()
  9.  
  10.  
  11. Public Sub printme() As String
  12.  
  13.   Return "Hello from subclass B"
  14.  
  15.  


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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’
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":
Attachment
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.