Scenario:
For example, I have the following abstract class Synchronizable
:
abstract class Synchronizable {
/// Getter & setter for sync status of the current data
bool get synced;
set synced(bool status);
/// Method that will be called to execute the synchronization process
Future<Either<Error, dynamic>> onSync();
}
As you can see the class has 3 different abstract methods:
- Getter for
synced
bool value - Setter for
synced
bool value - An abstract method
onSync
to perform synchronization
I have extended the class in one dummy implementation for this question:
class DummyImpl extends Synchronizable {
}
The IDE gave me an error/warning to must override abstract methods, but the method count was not 3
, it was 2
.
When I implemented using the prompt, It overrode only 2
methods.
The getter
& the onSync
method.
Note: The error is gone from the class name, meaning the override was successful & did not miss any method to override.
Question(s):
- How do I force override of the
setter
method in this case? - Why is the compiler skipping the
setter
for the override? (Not important but curious about this)
Update:
From the responses I got, It seems that getter/setters are replaced with a member directly by the compiler. However the main question still remains unanswered, how do I force override the any methods.
To reflect the same intention, I have updated the Question title as well. Please respond to solve that part.
Thank you.
CodePudding user response:
Well, a getter and a setter will allow you to do this:
instance.synced = true; // calling setter
and
print(instance.synced); // calling getter
It seems the shortest solution to provide this abilities to your class is to do exactly what your compiler did:
@override
bool synced;
You can set it. You can get it. Both don't do any additional work. But that is not what abstract
enforces anyway. It just enforces it to be overriden, you cannot keep a derived class from doing nothing when overriding your methods. You cannot force anybody to actually put code in there.
Your constraints were satisfied. The derived class has a getter and setter for synced
.
CodePudding user response:
Because your setter has the same name the getter. So basically, the compiler skip the setter after overriding the getter. You can change the name of your setter.