Object-Oriented Features
Dart's object model: constructors from factory to primary, extends versus implements, mixins, class modifiers, extension types, equality and enums. Interviewers probe reuse without inheritance.
part ofDartoverview, primer and where to startread it →on this pageshowhide
explore
- Constructors & Initializers5 questions
- Extends & Implicit Interfaces5 questions
- Mixins & Linearization5 questions
- Class Modifiers5 questions
- Extension Methods & Types5 questions
- Equality & Operators5 questions
- Enhanced Enums5 questions
questions
page 2 of 2In Dart 3, what do `abstract interface class` and `abstract final class` declare, and which class modifier combinations does the compiler reject?
basics
~20 sabstract interface class is Dart's pure interface: implementable elsewhere, never extended or constructed. abstract final class cannot be instantiated directly or subtyped outside its library, suiting static-only holders. Rejected: two of base/interface/final, sealed with any of them or abstract, and mixin with interface, final or sealed.
In Dart 3.13, what do primary constructors and the concise new and factory constructor forms change, and what breaks when a package adopts language version 3.13?
basics
~20 sDart 3.13 lets a class header declare fields and the main constructor, as in class Money(final int cents, final String currency);, and lets body constructors use new or factory without the class name. Adopting it makes final on ordinary parameters an error.
In Dart, how can an enum implement interfaces and apply mixins, and how do you write generic code bounded by Enum?
basics
~20 sAn enum can implement interfaces such as Comparable and apply mixins whose fields are final. Generic code uses T extends Enum to read index and name, but must receive the values list as an argument, since T.values does not compile.
In Dart, what do `is`, `as` and pattern matches check when a value has an extension type, and why is that weaker than a wrapper class?
basics
~20 sExtension types are erased, so is, as, switch and if-case checks test the representation object's runtime type. UserId(7) is int and 7 is UserId are both true, and 7 as UserId skips any constructor validation — protection that exists only at compile time.
In Dart, when does an overridden noSuchMethod actually run, and why can a class with one implement an interface without implementing its members?
basics
~20 snoSuchMethod runs when a call reaches a member the object lacks: via a dynamic receiver, or via a member its static type declares but its class leaves unimplemented while overriding noSuchMethod. Such classes get compiler-generated forwarders, so they compile without implementing the interface.
showing 31–35 of 35