Abstraction_Dart

import 'dart:io';

/// Data Abstraction : show only functionality and hiding background details this called
/// data abstraction.
// Abstract class
abstract class A {
// Abstract Method
void getData();
// Abstract Method
void showData();
}
// Calling Abstract class
class B extends A {

@override
void getData() {
print("Call from getData");
}

@override
void showData() {
print("Call from showData");
}
}
void main() {
B object = new B();
object.getData();
object.showData();
}

///You can create an object of concrete classes, but you cannot create an object of abstract classes.
///Abstract Class:
/// Abstract classes are classes that cannot be initialized. It is used to define the behavior
/// of a class that can be inherited by other classes. An abstract class is declared using
/// the keyword abstract.
/// Syntax:
// abstract class ClassName {
// //Body of abstract class
//
// method1();
// method2();
// }

///Abstract Method
/// An abstract method is a method that is declared without an implementation.
/// It is declared with a semicolon (;) instead of a method body.
/// Syntax
// abstract class ClassName {
// //Body of abstract class
// method1();
// method2();
// }

///Why We Need Abstract Class
/// Subclasses of an abstract class must implement all the abstract methods of the abstract class.
/// It is used to achieve abstraction in the Dart programming language.

/// Example 1: Abstract Class In Dart
/// In this example below, there is an abstract class Vehicle with two abstract methods start()
/// and stop(). The subclasses Car and Bike implement the abstract methods and override them
/// to print the message.

// abstract class Vehicle {
// // Abstract method
// void start();
// // Abstract method
// void stop();
// }
//
// class Car extends Vehicle {
// // Implementation of start()
// @override
// void start() {
// print('Car started');
// }
//
// // Implementation of stop()
// @override
// void stop() {
// print('Car stopped');
// }
// }
//
// class Bike extends Vehicle {
// // Implementation of start()
// @override
// void start() {
// print('Bike started');
// }
//
// // Implementation of stop()
// @override
// void stop() {
// print('Bike stopped');
// }
// }
//
// void main() {
// Car car = Car();
// car.start();
// car.stop();
//
// Bike bike = Bike();
// bike.start();
// bike.stop();
// }

///Note: The abstract class is used to define the behavior of a class that can be inherited by
/// other classes. You can define an abstract method inside an abstract class.

///Example 2: Abstract Class In Dart
/// In this example below, there is an abstract class Shape with one abstract method area()
/// and two subclasses Rectangle and Triangle. The subclasses implement the area() method and
/// override it to calculate the area of the rectangle and triangle, respectively.

// abstract class Shape {
// int dim1, dim2;
// // Constructor
// Shape(this.dim1, this.dim2);
// // Abstract method
// void area();
// }
//
// class Rectangle extends Shape {
// // Constructor
// Rectangle(int dim1, int dim2) : super(dim1, dim2);
//
// // Implementation of area()
// @override
// void area() {
// print('The area of the rectangle is ${dim1 * dim2}');
// }
// }
//
// class Triangle extends Shape {
// // Constructor
// Triangle(int dim1, int dim2) : super(dim1, dim2);
//
// // Implementation of area()
// @override
// void area() {
// print('The area of the triangle is ${0.5 * dim1 * dim2}');
// }
// }
//
// void main() {
// Rectangle rectangle = Rectangle(10, 20);
// rectangle.area();
//
// Triangle triangle = Triangle(10, 20);
// triangle.area();
// }

///Constructor In Abstract Class:
/// You can’t create an object of an abstract class. However, you can define a constructor
/// in an abstract class. The constructor of an abstract class is called when an object
/// of a subclass is created.

///Example 3: Constructor In Abstract Class
/// In this example below, there is an abstract class Bank with a constructor which takes two
/// parameters name and rate. There is an abstract method interest(). The subclasses SBI and
/// ICICI implement the abstract method and override it to print the interest rate.

// abstract class Bank {
// String name;
// double rate;
//
// // Constructor
// Bank(this.name, this.rate);
//
// // Abstract method
// void interest();
//
// //Non-Abstract method: It have an implementation
// void display() {
// print('Bank Name: $name');
// }
// }
//
// class SBI extends Bank {
// // Constructor
// SBI(String name, double rate) : super(name, rate);
//
// // Implementation of interest()
// @override
// void interest() {
// print('The rate of interest of SBI is $rate');
// }
// }
//
// class ICICI extends Bank {
// // Constructor
// ICICI(String name, double rate) : super(name, rate);
//
// // Implementation of interest()
// @override
// void interest() {
// print('The rate of interest of ICICI is $rate');
// }
// }
//
// void main() {
// SBI sbi = SBI('SBI', 8.4);
// ICICI icici = ICICI('ICICI', 7.3);
//
// sbi.interest();
// icici.interest();
// icici.display();
// }

///Key Points To Remember
/// You can’t create an object of an abstract class.
/// It can have both abstract and non-abstract methods.
/// It is used to define the behavior of a class that other classes can inherit.
/// Abstract method only has a signature and no implementation.

/// Abstraction Example
// void main()
// {
// print("Enter the value of Number 1 : ");
// var no1 = int.parse(stdin.readLineSync()!);
// print("Enter the value of Number 2 : ");
// var no2 = int.parse(stdin.readLineSync()!);
//
// Addition additionObject = new Addition(no1, no2);
// Subtraction subtractionObject = new Subtraction(no1, no2);
// Multiplication multiplicationObject = new Multiplication(no1, no2);
// Division divisionObject = new Division(no1, no2);
//
// additionObject.calculation();
// subtractionObject.calculation();
// multiplicationObject.calculation();
// divisionObject.calculation();
//
// }
// abstract class Mathematics
// {
// int num1, num2;
// Mathematics(this.num1, this.num2);
//
// void calculation();
// }
// class Addition extends Mathematics
// {
// Addition(super.num1, super.num2);
//
// @override
// void calculation() {
// var sum = num1 + num2;
// print("Addition is : ${sum}");
// }
// }
// class Subtraction extends Mathematics
// {
// Subtraction(super.num1, super.num2);
//
// @override
// void calculation() {
// var sub = num1 - num2;
// print("Subtraction is : ${sub}");
// }
// }
// class Multiplication extends Mathematics
// {
// Multiplication(super.num1, super.num2);
//
// @override
// void calculation() {
// var mul = num1 * num2;
// print("Multiplication is : ${mul}");
// }
// }
// class Division extends Mathematics
// {
// Division(super.num1, super.num2);
//
// @override
// void calculation() {
// var div = num1 / num2;
// print("Division is : ${div}");
// }
// }

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter