# Method Overriding

{% embed url="https://youtu.be/2cAbggyzNJo" %}



{% hint style="success" %}
When a _Child class_ redefines the method of its _Parent class_ then it is called Method Overriding
{% endhint %}

{% hint style="success" %}
Overriding is done so that a _child class_ can give its own implementation to the method which is already provided by the _parent class_.
{% endhint %}

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



```

{% hint style="danger" %}
The _**number**_ and _**data type**_ of function’s argument _**must match**_ while overriding the method
{% endhint %}

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDmeoqSq88UEXs-oDbt%2F-MDmjuPtprnySUA-CHrm%2FScreenshot%202020-08-03%20at%2011.15.49%20AM.png?alt=media\&token=4b33b8ae-a9c9-4e2c-91fa-f508ca14af82)

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&#x20;

You can visualise method overriding like below -  &#x20;

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MDms_8lHgmrcvbjMbD6%2F-MDmsvn14mtyygzYHjJt%2Fgiphy.gif?alt=media\&token=3cbd5472-5c95-4768-a546-c977dffa01ff)

