dart-lang / dart-lang/language

Address encapsulation challenges in abstract classes

Open
#3,340 7 comments 3 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

When transitioning from other programming languages to Dart, I occasionally find myself in a situation where I'd like to define an abstract class with hidden implementation methods. These methods are intended to be implemented differently in concrete classes, but they shouldn't be exposed as part of the public API; they are meant to be treated as implementation details.

I've been searching for a workaround to achieve the desired level of encapsulation:

`abstract.dart`
```dart
abstract class Abstract {
int _aInt();
int aInt() => _aInt();
}
```

`concrete.dart`
```dart
class Concrete extends Abstract {
@override
int _aInt() => 1; // The method doesn’t override an inherited method.
}
```

`main.dart`
```dart
void main() {
// NoSuchMethodError: tried to call a non-function, such as null: 'core.NoSuchMethodError.withInvocation'
print(Concrete().aInt());
}
```

Is anyone aware of a workaround for this issue in Dart? I would greatly appreciate any guidance or suggestions.

Contributor guide

Open the contributing guide

Research direction

Start with the abstract.dart, concrete.dart, and main.dart examples in the issue, then read the Dart language specification sections covering abstract classes, private members, and method overriding. Determine whether the requested encapsulation is supported or requires a language change; done means a documented resolution or a clearly scoped proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.