How to use generic in DART language
In this article, i will explain about generic in DART language.
Generic in DART
Generic are the most powerful feature of DART. Generic allow you to define type-safe data structure, types is optional in DART. The <> notation marks is used for generics. Generics are mostly used for providing information about type. Let us suppose you can define a interface of generic type, you can also gives us a integer type value, or string type value depending on implementation. Generics is heavily used in list. The example of generic is defined below:
Example of generic
interface DefGeneric<T>{}//Here generic type interface is declare
interface IntGeneric extends DefGeneric<int>{int Sum();}//Here int type generic define
interface StringGeneric extends DefGeneric<String>{String value();}//Here string type generic define
class UseGeneric implements IntGeneric , StringGeneric {
int sum(){
int a=5,b=6;
print("The value of ${a} + ${b} is ${a+b}");
}
String value()
{print("I am a string");}
}
void main() {
UseGeneric obj = new UseGeneric();
obj.sum();
obj.value();
}
|
Output:
Ask Your Question
Got a programming related question? You may want to post your question here