Polymorphism_Dart

 import "dart:io";

/// Polymorphism : Polymorphism is defined as ability to take more than one form.
/// there are two types of polymorphism : Compile time and Run time
/// Compile time polymorphism OR (method Overloading) : class name same, method name same but different
/// argument it is called method overloading OR Compile time polymorphism.
/// Run time polymorphism OR (method Overriding) : Method name same, argument same but different
/// class it is called method overriding OR Run time polymorphism.
///
/// "Compile-time polymorphism" is not supported in dart(Flutter)
/// Below is Run Time polymorphism OR (Method Overriding) Example
/// Example 1
// class Aone {
// void play() {
// print("This is from Class Aone : First Class");
// }
// int ground(int no) {
// return no;
// }
//
// }
// class Btwo extends Aone {
// void play() {
// print("This is from Class Btwo : Second Class");
// }
// int ground(int no1) {
// return no1;
// }
// }
// void main() {
// Btwo object = new Btwo();
// object.play();
// print(object.ground(123));
// }

/// Example 2
// class Vehicle {
// void run() {
// print("Vehicle is Running");
// }
// }
// class Bus extends Vehicle {
// void run() {
// print("Bus is Running");
// }
// }
// void main() {
// Vehicle vehi = new Vehicle();
// vehi.run();
//
// Bus bus = new Bus();
// bus.run();
// }


/// Example 3
class Employee {
void salary() {
print("Employee salary is \$1000");
}
}
class Manager extends Employee {
void salary() {
print("Manager salary is \$2000");
}
}
class Developer extends Manager {
void salary() {
print("Developer salary is \$3000");
}
}
void main() {
Manager manager = new Manager();
Developer developer = new Developer();

manager.salary();
developer.salary();
}

/// NOTE :
//Poly means many and morph means forms. Polymorphism is the ability of an object to take on many forms.
// As humans, we have the ability to take on many forms. We can be a student, a teacher, a parent,
// a friend, and so on. Similarly, in object-oriented programming, polymorphism is the ability
// of an object to take on many forms.
///Note : In the real world, polymorphism is updating or modifying the feature, function,
/// or implementation that already exists in the parent class.
/// Polymorphism by method Overriding
//Method overriding is a technique in which you can create a method in the child class
// that has the same name as the method in the parent class. The method in the child class
// overrides the method in the parent class.
/// Syntax :
//class ParentClass{
// void functionName(){
// }
// }
// class ChildClass extends ParentClass{
// @override
// void functionName(){
// }
// }

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