program to demonstrate Generics (example of generic class) // Using Generics class Data { T data; Data(this.data); } void main() { // create an object of type int and double Data intData = Data(10); Data doubleData = Data(10.5); // print the data print("IntData: ${intData.data}"); print("DoubleData: ${doubleData.data}"); } ________________________________________________________________________________________________________ // Define generic method T genericMethod(T value) { return value; } void main() { // call the generic method print("Int: ${genericMethod(10)}"); print("Double: ${genericMethod(10.5)}"); print("String: ${genericMethod("Hello")}"); } ________________________________________________________________________________________________________ program to define and calling generic method with parameters // Define generic method T genericMethod(T value1, U value2) { return value1; } void main() { // call the generic method print(genericMethod(10, "Hello")); print(genericMethod("Hello", 10)); } ________________________________________________________________________________________________________ // Define generic class with bounded type class Data { T data; Data(this.data); } void main() { // create an object of type int and double Data intData = Data(10); Data doubleData = Data(10.5); // print the data print("IntData: ${intData.data}"); print("DoubleData: ${doubleData.data}"); // Not Possible // Data stringData = Data("Hello"); } ________________________________________________________________________________________________________ example of generic class // abstract class Shape abstract class Shape { // abstract method area double get area; } // class Circle which implements Shape class Circle implements Shape { // field radius final double radius; // constructor Circle(this.radius); // implementation of area method @override double get area => 3.14 * radius * radius; } // class Rectangle which implements Shape class Rectangle implements Shape { // fields width and height final double width; final double height; // constructor Rectangle(this.width, this.height); // implementation of area method @override double get area => width * height; } // Generic class Region class Region { // field shapes List shapes; // constructor Region({required this.shapes}); // method totalArea double get totalArea { double total = 0; shapes.forEach((shape) { total += shape.area; }); return total; } } void main() { // create objects of Circle and Rectangle var circle = Circle(10); var rectangle = Rectangle(10, 20); // create a list of Shape objects var region = Region(shapes: [circle, rectangle]); // print the total area print("Total Area of Region: ${region.totalArea}"); } ________________________________________________________________________________________________________