Abstract and protected methods
Abstract and protected methods
Are there such things like abstract methods or protected methods within Gambas?
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Abstract and protected methods
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?
Re: Abstract and protected methods
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.
Re: Abstract and protected methods
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.
Re: Abstract and protected methods
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 178 times
Re: Abstract and protected methods
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: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.
' 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.
Re: Abstract and protected methods
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":
Probably you can achieve your aims via design pattern "Strategy":
Re: Abstract and protected methods
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.
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.