Error Mixin : Abstract classes cannot be instantiated in Dart - dart

I was learning dart but it was still an error when I entered into the mixin, I don't know the fault where it is always an error when:
Abstract classes cannot be instantiated
I want to implement a mixin for Cat, Elang and Hiu with a subclass of Mamalia, Burung, Ikan
This is the Github code:

The error are rather clear. You cannot make an instance of an abstract class since an abstract class per definition is a class which can define methods and fields which are yet to be implemented and the class is therefore not complete.
The purpose of abstract classes is to let other classes extend from them and implement the missing methods.
I don't know why all your classes are marked as abstract but you can just remove the abstract keyword from the classes: Mamalia, Ikan and Burung and it should work since all of these classes are not needed to be abstract.

Abstract Class -
Use the abstract modifier to define an abstract class—a class that can’t be instantiated. Abstract classes are useful for defining interfaces, often with some implementation.
Don't instantiate Mamalia, Burung or Ikan classes. Instead instantiate Cat, Elang and Hiu.
Mamlia mamal = Cat(); // Will allow accessing methods defined only in Mamal class
Cat cat = Cat() // Will allow accessing all the methods defined in Cat class

Related

What is the difference between 'implements' keyword and 'extends' keyword in dart?

I couldn't understand the difference between these two keywords.
By using extends we can get features from parent class. I think implements does that too.
First I thought the difference is overriding methods but with extends I can do that.(I might be wrong)
Is the difference of these two keywords about overriding methods or what? Thank you
extends means we get the implementation of a given class and we can then override members if we want our own implementation for certain variables or methods. You can also add new variables and methods.
implements means you get nothing from the class you implement from. But you promise that your class will be compatible with the interface of the class you are implementing. So no, you are not getting any implementation from the super class and you need to implement everything or declare your class abstract.

"extends" versus "implements" versus "with"

I want to understand the difference between extends, implements and with. When do I use each keyword?
Extends:
Use extends to create a subclass, and super to refer to the superclass.
Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.
You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.
Implements:
Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.
Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.
When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.
With (Mixins):
Mixins are a way of reusing a class’s code in multiple class hierarchies.
With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.
They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.
extend can only be used with a single class at the time, BUT... you can easily extend a class which extends another class which extends another class which...! ;)
In fact, most Flutter widgets are already built like that.

What does the keyword "dynamic" do to a procedure?

What happens to a procedure when it is declared with the keyword dynamic?
And what is the effect of declaring it with the keyword static?
This question can be answered by reading the documentation.
The dynamic keyword introduces a method that can be overridden polymorphically. Semantically it is interchangeable with virtual, but the is implemented in a different manner. Read about it here: http://docwiki.embarcadero.com/RADStudio/en/Methods#Virtual_and_Dynamic_Methods
To make a method virtual or dynamic, include the virtual or dynamic
directive in its declaration. Virtual and dynamic methods, unlike
static methods, can be overridden in descendent classes. When an
overridden method is called, the actual (run-time) type of the class
or object used in the method call--not the declared type of the
variable--determines which implementation to activate.
To override a method, redeclare it with the override directive. An
override declaration must match the ancestor declaration in the order
and type of its parameters and in its result type (if any).
...
In Delphi for Win32, virtual and dynamic methods are semantically
equivalent. However, they differ in the implementation of method-call
dispatching at run time: virtual methods optimize for speed, while
dynamic methods optimize for code size.
In general, virtual methods are the most efficient way to implement
polymorphic behavior. Dynamic methods are useful when a base class
declares many overridable methods that are inherited by many
descendent classes in an application, but only occasionally
overridden.
Class static methods are like class methods in that they are invoked on the class rather than an instance. The difference between class static and class methods is that class methods are passed a Self pointer that contains the class, and class static methods are not. This means that class methods can be polymorphic and class static methods cannot. Read about it here: http://docwiki.embarcadero.com/RADStudio/en/Methods#Class_Static_Methods
Like class methods, class static methods can be accessed without an object reference. Unlike ordinary class methods, class static methods have no Self parameter at all. They also cannot access any instance members. (They still have access to class fields, class properties, and class methods.) Also unlike class methods, class static methods cannot be declared virtual.
With all due respect, I refer you to this question: How can I search for Delphi documentation?

mixin whose super class is not Object

i want to do something like the following so bad
abstract class A{}
abstract class B extends A{}
abstract class C extends A{}
abstract class D extends B with C{} //C cannot be used as a mixin because it extends a class other than object
is there any solution other than copying the content of C in D?
the real names of my classes, to give you an idea of what i am trying to do
//A Observable
//B DynamicObservable
//C ObservableWithValidationErrors
//D DynamicObservableWithValidationErrors
There are some restrictions on the class you can use as mixin (See Mixins in Dart - Syntax and semantics).
However, in this proposal, a mixin may only be extracted from a class that obeys the following restrictions:
The class has no declared constructors.
The class’ superclass is Object.
The class contains no super calls.
Those restrictions may be removed in the future.
The semantics are deliberately restricted in several ways, so as to reduce disruption to our existing implementations, while allowing future evolution toward a full-fledged mixin implementation. This restricted version already provides considerable value.
In some circumstances it might be possible to restructure your class to use multiple mixins instead:
abstract class Observable{}
abstract class Dynamic{}
abstract class ValidationErrors{}
abstract class DynamicObservable extends Observable with Dynamic{}
abstract class ObservableWithValidationErrors extends Observable with ValidationErrors{}
abstract class DynamicObservableWithValidationErrors extends Observable with Dynamic, ValidationErrors{}
Of course, if Dynamic or ValidationErrors cannot be isolated this way, and rely on inheriting from Observable, this will not be possible.

How do I use Mixins without inheritance?

Is it possible to apply a mixin to a class without the target class inheriting from any other class? For example, can I implement the following:
class User with Persistence {
// implementation
}
Most of your examples of Mixins in dart seem to be coupled with inheritance.
Thanks in advance!
You have to inherit from another class if you want to use a mixin. However, you can simply inherit from Object:
class User extends Object with Persistence {
// implementation
}
But really, you can just inherit from Persistence as well which will have the same effect:
class User extends Persistance {
// implementation
}
Gilad Bracha explains that the syntax is specifically designed this way:
I think it is important to understand the semantic model here. "with"
is the mixin application operator, and it takes two parameters: a
superclass and a mixin, and yields a class. Saying "with Foo" in
isolation makes as much sense as saying >> 2 (you could interpret both
as curried functions, but that is very far from Dart). When you write
"C extends S with M", you are specifying a superclass following the
extends keyword, just as you do when you write "C extends K" except
that the superclass is not specified via an identifier but via a mixin
application. So the superclass would be "S with M".
As Lasse points out, as practical matter it doesn't restrict you, but
having the syntax reflect the underlying structure is important.

Resources