Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Abstract Class & Abstract Methods
  • Abstract Class & Abstract Methods : Why do we use them ?
  • Abstract Class & Methods in Flutter
  • Official Documentation

Was this helpful?

  1. Dart Fundamentals-2

Abstract Class and Abstract Methods

PreviousStatic Variables and MethodsNextInterface

Last updated 4 years ago

Was this helpful?

Abstract Class & Abstract Methods

void main() {
  // Abstract Class & Methods

  var circle1 = Circle(3);

  print(circle1.calarea());
}

abstract class Shape {
  void draw();

  double calarea();
}

class Circle extends Shape {
  int radius;

  final pi = 3.14;

  @override
  void draw() {
    print("Drawing a Circle");
  }

  @override
  double calarea() {
    return (this.pi * this.radius * this.radius);
  }

  Circle(this.radius);
}

Abstract Class & Abstract Methods : Why do we use them ?

import 'dart:math';

void main() {
  var circle1 = Circle(5);

  print(circle1.calarea());
}

class Circle {
  // instance variables
  double radius;
  final pi = 3.14;

  //methods
  void draw() {
    print("Drawing a circle ");
  }

  double calarea() {
    return this.pi * pow(this.radius, 2);
  }

  //Constructor
  Circle(this.radius);
}

import 'dart:math';

void main() {
  var square1 = Square(6);

  print(square1.calarea());
}

class Square {
  // instance variables
  double length;

  //methods
  void draw() {
    print("Drawing a Square");
  }

  double calarea() {
    return pow(this.length, 2);
  }

  //Constructor
  Square(this.length);
}

import 'dart:math';

void main() {
  // Abstract Class & Methods

  // You can't make an instance of an abstract class

  // Abstract class always meant to extended by some other class

  // When any class extend an abstract , it must override it's
  // abstract methods

  var circle1 = Circle(3);

  var square1 = Square(3);

  print(circle1.calarea());

  print(square1.calarea());
}

abstract class Shape {
  void test() {
    print("test");
  }

  void draw();

  double calarea();
}

class Circle extends Shape {
  int radius;

  final pi = 3.14;

  @override
  void draw() {
    print("Drawing a Circle");
  }

  @override
  double calarea() {
    return (this.pi * this.radius * this.radius);
  }

  Circle(this.radius);
}

class Square extends Shape {
  double length;

  @override
  void draw() {
    print("Drawing a Square");
  }

  @override
  double calarea() {
    return pow(this.length, 2);
  }

  Square(this.length);
}

You can't declare an abstract method inside a simple class . You can only declare abstract methods inside abstract class

❌ Complier shows an error

✅ Compiler is happy

You can have an abstract class with only normal methods , but that defeats the purpose of using an abstract class

Abstract Class & Methods in Flutter

Official Documentation