Dart : abstract Class

Dart : abstract Class

·

1 min read

Abstract

  • To define a class that doesn't require a full, concrete implementation of its entire interface, use the abstract modifier.

  • Abstract classes cannot be constructed from any library, whether its own or an outside library.

  •   abstract class Vehicle {
        void moveForward(int meters);
    
      }
    
  •   abstract class Animal {
        void makeSound(); // Abstract method
      }
    
      class Dog extends Animal {
        @override
        void makeSound() {
          print('Woof!');
        }
      }
    
      class Cat extends Animal {
        @override
        void makeSound() {
          print('Meow!');
        }
      }
    
      void main() {
    
        Animal dog = Dog(); // OK
        Animal cat = Cat(); // OK
    
        dog.makeSound(); // Output: Woof!
        cat.makeSound(); // Output: Meow!
    
      }
    
  •   Woof!
      Meow!
    
      Exited.