Advertisement
Hiteshw11

Calculate Area Of Circle & Square Using Inheritance and Overriding

Nov 26th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.75 KB | Source Code | 0 0
  1. /* Create a base class `Shape` with a method `area` that returns 0. Create derived classes `Circle` and `Square` that override the `area` method to calculate the area of a circle and a square, respectively.
  2.  
  3. #### Expected Output:
  4. ```dart
  5. Shape Area: 0
  6. Circle Area: 78.5
  7. Square Area: 25.  */
  8.  
  9.  
  10.  
  11.  
  12.  
  13. void main() {
  14. var a = Shape();
  15. print(a.area(3));
  16. var b =  Circle();
  17. print(b.area(5));
  18. var c = Square();
  19. print(c.area(5));
  20. }
  21.  
  22. class Shape
  23. {
  24.  
  25. area(double s)
  26. {
  27. return s;
  28.  
  29.  
  30. }
  31.  
  32. }
  33.  
  34. class Circle extends Shape
  35. {
  36.  
  37.  
  38. Circle():super();
  39.  
  40. @override
  41. area(data1)
  42. {
  43.    double b =3.14*data1*data1;
  44.   return b;
  45. }
  46.  
  47. }
  48.  
  49. class Square extends Shape
  50. {
  51.  
  52. Square():super();
  53.  
  54. @override
  55. area(data2)
  56. {
  57.  
  58. double c = data2*data2;
  59. return c;
  60.  
  61. }
  62.  
  63. }
Tags: dart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement