Access child class constructor from parent class - dart

I would like to get access to a class constructor from it's parent like;
var childClass = Parent().child;
Child child = child.constructor(name, age);
Is that possible? Or is there a way to use a class to return a child class to use with constructor?

What you are looking for is called a "factory constructor":
Use the factory keyword when implementing a constructor that doesn’t always create a new instance of its class. For example, a factory constructor might return an instance from a cache, or it might return an instance of a subtype
https://dart.dev/guides/language/language-tour#constructors
Example:
class Parent {
factory Parent() {
return Child();
}
}

Related

How can I make a constructor use the same default value as it's super?

I want to specify a constructor that does not change the default value of test.
So "test text" should be printed instead of null.
void main() async {
print(B().text);
}
class A{
A({this.text = "test text"});
final text;
}
class B extends A{
B({String text}) : super(text: text);
}
dartpad
Is this possible?
Not as such. If you write a constructor, you can't get the superclass constructor's default value automatically in any way, and you have to either pass the parameter to the superconstructor or not in the initializer list. It can't depend on the value of the subclass constructor's parameter. So, you have to write the superclass constructor parameter default value again.
The one hack you can do (and I don't recommend doing it just for this) is that if the parameters to the subclass constructor is exactly the same as the superclass constructor, you can declare the members of your subclass in a mixin and make the subclass be a mixin application.
So instead of:
class SuperClass {
SuperClass(...args) : ...
...
}
class SubClass extends SuperClass {
SubClass(...args) : super(...args);
members() ...
}
you do:
class SuperClass {
SuperClass(...args) : ...
...
}
mixin _SubClass on SuperClass {
members() ...
}
class SubClass = SuperClass with _SubClass;
That will give SubClass a constructor for each superclass constructor, with the same parameters (including default values), which forwards directly to the superclass constructor.
Don't do this just to avoid writing the default value again!

Is it possible to have a private constructor in dart?

I'm able to do something like the following in TypeScript
class Foo {
private constructor () {}
}
so this constructor is accessible only from inside the class itself.
How to achieve the same functionality in Dart?
Just create a named constructor that starts with _
class Foo {
Foo._() {}
}
then the constructor Foo._() will be accessible only from its class (and library).
A method without any code must be something like this
class Foo {
Foo._();
}
Yes, It is possible, wanna add more information around it.
A constructor can be made private by using (_) underscore operator which means private in dart.
So a class can be declared as
class Foo {
Foo._() {}
}
so now, The class Foo doesn't have a default constructor
Foo foo = Foo(); // It will give compile time error
The same theory applied while extending class also, It's also impossible to call the private constructor if it declares in a separate file.
class FooBar extends Foo {
FooBar() : super._(); // This will give compile time error.
}
But both above functionality works if we use them in the same class or file respectively.
Foo foo = Foo._(); // It will work as calling from the same class
and
class FooBar extends Foo {
FooBar() : super._(); // This will work as both Foo and FooBar are declared in same file.
}
you can create following class in order to get a singleton instance
class Sample{
factory Sample() => _this ??= Sample._();
Sample._(); // you can add your custom code here
static Sample _this;
}
Now in the main function you can call the sample constructor
void main(){
/// this will return the _this instace from sample class
Sample sample = Sample();
}
just use abstract class.
Because you can't instantiate abstract class

How to access abstract superclass implementation when it contains a factory method?

I have an abstract superclass with a factory that returns an instance of a subclass. Is it possible to have a method that is implemented only in superclass? In the following code, for instance, would it be possible to remove Wind::act()?
abstract class Element {
final String action; // what it does
String act() => action; // do it
factory Element() {
return new Wind();
}
}
class Wind implements Element {
final action = "blows";
act() => action; // Why is this necessary?
}
void main() {
print(new Element().act());
}
When removing Wind::act(), there is an error about it missing. Also, when extending rather than implementing the superclass, leaving out the subclass implementation doesn't cause an error. But with a factory method, extending is not an option.
To inherit functionality from Element in Wind, you need to either extend or mix-in Element in Wind. Merely implementing an interface will not inherit any implementation.
So, you need to have class Wind extends Element { ... }.
That's not currently possible because Element has no generative constructor that Wind can use as super-constructor. So, you need to add that too, and make sure to initialize the action field in that constructor.
class Element {
final String action;
Element._(this.action); // Generative constructor that Wind can use.
factory Element() = Wind; // Factory constructor creating a Wind.
String act() => action;
}
class Wind extends Element {
Wind() : super._("blows");
}
The generative constructor doesn't need to be private, but if you are declaring and using all the classes only inside your own library, it might as well be.
Another option is to have a separate ElementBase class containing the action field and act function and an empty-named generative constructor. Mixins are not a good choice in this case, because there is no good way to make action final when mixins can't have constructors.
abstract class Element {
String get action;
factory Element() = Wind;
String act();
}
class ElementBase implements Element {
final String action;
ElementBase(this.action);
String act() => action;
}
class Wind extends ElementBase {
Wind() : super("blow");
}
It's a common problem to want both a generative constructor for subclasses and a factory constructor generating the default implementation in an interface/skeleton class. The List and Map interfaces have this problem, and have solved it by exposing ListBase and MapBase. I think that is the best solution when you are exposing the superclass to other users in other libraries. If it's only used internally by yourself, I'll use the private/non-default-named generative constructor in the superclass.

Abstract methods in Dart API reference

Many methods like complete in class Completer are marked "abstract", but in fact It can be directly invoked without being implemented. I'm really confused. Could anyone help me?
Yes, this can be a bit confusing. While abstract classes cannot be instantiated, it is possible to make them appear to be instantiable by defining a factory constructor. This is what Completer, Future and other abstract classes do:
abstract class Completer<T> {
factory Completer() => new _CompleterImpl<T>();
...
}
You can then invoke methods on the object created by the factory constructor. In the example above, factory Completer() returns a new _CompleterImpl object. Look at the (truncated) code of that class:
class _CompleterImpl<T> implements Completer<T> {
final _FutureImpl<T> _futureImpl;
_CompleterImpl() : _futureImpl = new _FutureImpl() {}
Future<T> get future {
return _futureImpl;
}
void complete(T value) {
_futureImpl._setValue(value);
}
...
}
and you see complete(); that is the method being invoked.

Actionscript 2: A class loaded into a parent class, get parent variables

I have a child class that is loaded into the parent class when the swf begins, like so:
var myvar = 'hello';
public function Parent()
{
this.child = new Child();
};
How can I retrieve the variable 'myvar' from within child?
Child has no reference to the parent, so there is no way to retrieve myvar.
You could create child like this:
public class Child
{
private var parent:Parent;
public Child(parent:Parent)
{
this.parent = parent;
}
...
}
This will allow the child to access it's parent through it's parent property and will be able to see all the public members that belong to parent.

Resources