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

Was this helpful?

  1. Dart Fundamentals-2
  2. Inheritance

Method Overriding

PreviousInheritance, Types of Inheritance , TerminologyNext@override annotation

Last updated 4 years ago

Was this helpful?

When a Child class redefines the method of its Parent class then it is called Method Overriding

Overriding is done so that a child class can give its own implementation to the method which is already provided by the parent class.

void main() {
  
  
  // Method overidding 

  var manager1 = Manager();
  
  
  manager1.id =1;
  manager1.firstname="John";
  manager1.lastname="Doe";
  manager1.dayworked=30;
  
  manager1.project ="Alpha";
  
  print(manager1.salary(20));

  
  
  
  
}

class Employee {
  int id;
  int dayworked;
  String firstname;
  String lastname;

  String fullname() {
    return this.firstname + " " + this.lastname;
  }

  int salary(int perDaywage) {
    return (this.dayworked * perDaywage);
  }
}




// Manager Class 


class Manager extends Employee {
  
  
  //property 
   String project;
 
  
  //Method 
  
  String manageproject(){
    
    return "Managing project " + " " + this.project; 
  }
  
  
   int salary(int perDaywage) {
    return ((this.dayworked * perDaywage)+30);
  }
  
  
}


The number and data type of function’s argument must match while overriding the method

Here in the child class , the return type and the argument datatype of the method is changed from "int" to "double" , so the compiler shows an error

You can visualise method overriding like below -