# Abstract Class and  Abstract Methods

### &#x20;Abstract Class & Abstract Methods&#x20;

{% embed url="<https://youtu.be/hhXCwnFD_Bg>" %}

```dart
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);
}


```

{% hint style="success" %}

* [x] You can't make instances of abstract class  . Abstract class are always extended by some other class&#x20;
* [x] Abstract classes may contain some abstract methods as well as normal methods&#x20;
* [x] When you extend a abstract class you must override  it's abstract methods, though it's not mandatory to override other normal methods&#x20;
* [x] Abstract Classes are useful for code organisation and consistency .You can define the functionality you need  as abstract methods  inside abstract class&#x20;
  {% endhint %}

### &#x20;Abstract Class & Abstract  Methods : Why do we use them ?&#x20;

{% embed url="<https://youtu.be/UxDsDmD47eM>" %}

{% tabs %}
{% tab title="Separate  Circle Class " %}

```dart
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);
}


```

{% endtab %}

{% tab title="Separate Square Class" %}

```dart
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);
}


```

{% endtab %}

{% tab title="Circle & Square Class with abstract class & methods" %}

```dart
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);
}
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
You can't declare an abstract method inside a simple class . You can only declare abstract methods inside abstract class&#x20;
{% endhint %}

{% tabs %}
{% tab title="Trying to declare an abstract method inside a normal class " %}
❌ Complier shows an error&#x20;

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-ME6I_ikyM8oKePKX3PG%2F-ME6IhVV1grm8PdTkr7Y%2FScreenshot%202020-08-07%20at%2011.02.48%20AM.png?alt=media\&token=403bfda9-d8aa-4ab9-a303-d637865ea7a3)
{% endtab %}

{% tab title="Change the normal class to abstract " %}
✅ Compiler is happy&#x20;

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-ME6IsLdBvVvQ_KIS-az%2F-ME6J2oQ6idj3D_4Y4Ab%2FScreenshot%202020-08-07%20at%2011.06.13%20AM.png?alt=media\&token=4bfbe049-1558-4dc9-84ae-0af7e972ade1)
{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can have an abstract class with only normal methods , but that  defeats the purpose of using an abstract class&#x20;
{% endhint %}

### Abstract Class & Methods in Flutter&#x20;

{% embed url="<https://youtu.be/VMnCZob0V68>" %}

### Official Documentation&#x20;

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MEAje0dwOmU4WpLlB_2%2F-MEAjs5FlHcj_dmfrS_Z%2FScreenshot%202020-08-08%20at%207.46.07%20AM.png?alt=media\&token=a53acf10-5d07-4241-b291-ed42d7045fbd)
