Table of contents
1.
Introduction
2.
Dart Methods
2.1.
Instance Methods
2.2.
Class Methods
2.3.
Implementation
3.
Dart Method Overriding
3.1.
Implementation of Method Overriding
4.
Frequently Asked Questions
4.1.
Can I override a method in the parent class itself?
4.2.
Is it possible to override a static method in Dart?
4.3.
Why can’t we override the constructors of the parent class?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Methods and Method Overriding

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss Dart Methods and Method overriding. Methods are one of the building blocks of any programming language. It is used to group a piece of code that performs a similar task. Then after that, we will move to the concept of method overriding, which is another important concept in Object-oriented programming. 


You may also read the blog Dart Constructors and Super Constructors on the Coding Ninjas Website to learn more stuff about Dart.

Dart Methods

Methods are a fundamental part of any programming language. Methods are famously known as functions in daily usage. These are a piece of code that is bound together and placed at a particular location in the code. Basically, methods are a block of code encapsulated by a header that is the name of the function. The entire code block can be called by invoking that function. The methods can also return a value of any data type. If they do not want to return anything, the return type can be specified as void. We can get increased modularity and readability in our program with the help of methods.  

Instance Methods

There are two categories of methods that are defined inside a Dart class. The first one is the methods that can be invoked with the help of an object/instance of the class, and the second one is the methods that can be directly invoked with the help of the name of the class without involving any instance of the class.

Feel free to refer to the blog Dart Classes and Objects on the Coding Ninjas Website to read more about the concept of classes and objects in Dart.

The following is the syntax to create and call an instance method.

// Creating an instance method, inside a class
class className{
return_data_type method_name(args){
// Body of the method
}
}


// To invoke this method, we can use the following syntax
className obj = new className();
obj.method_name(args);

Class Methods

Class methods are just like normal methods except for the fact they are declared using the static keyword. These methods can be invoked directly using the class name rather than an object name. A static method cannot access the non-static variables of a class.


The following is an example program depicting the use of Class methods in Dart.

Code:

// Dart Static Methods
class Ninja{
  static String ninja_name = "Mr John Humpfrey";
  static void get_ninja_name(){
    print("The name of the ninja is ${ninja_name}.");
  }
}

void main(){
  Ninja.get_ninja_name();

}Output:

The name of the ninja is Mr John Humpfrey.

Implementation

In this section, we will look at a sample Dart program that will show us the usage of Dart methods. In this program, we have a class named Ninja, which has two functions inside it. The method print_function_type_1 is an instance method, therefore can be invoked using an instance/object of the class. This is demonstrated in the main function. The second method, named print_function_type_2, is a class method as it has the static keyword in its declaration. This function can be directly invoked using the name of the class in this case which is Ninja. The same is demonstrated in the main function.

Code:

class Ninja {

    // Example of Instance method
    void print_function_type_1(){
        print("This is an instance function");
    }

    // Example of class method
    static void print_function_type_2(){
        print("This is a class function");
    }

}

void main(){
    var ninja_obj = new Ninja();

    // Calling an instance method with the help of class object
    ninja_obj.print_function_type_1();  
   
    // Calling a class method directly with the class name
    Ninja.print_function_type_2();

}Output:

This is an instance function
This is a class function

Dart Method Overriding

Method overriding is a very important concept in Object-Oriented programming. We can define a method in the subclass with the same name as the method in the parent class. This way, the subclass can provide its own implementation for a method that is present in the parent class as well. 

Implementation of Method Overriding

In this section, we will see an example program which will demonstrate method overriding in Dart.

Code:

class Student{
  static void get_details(){
    print("Get details method of Student class");
  }
}
class Ninja extends Student{
  static void get_details(){
    print("Get details method of Ninja class");
  }
}

void main(){
  Ninja.get_details();

}Output:

Get details method of Ninja class

Frequently Asked Questions

Can I override a method in the parent class itself?

No, you cannot override a method in the parent class. The overridden method must be written in the child's class.


Is it possible to override a static method in Dart?

No, it is not possible to override a method that has been declared static in the parent class.

 

Why can’t we override the constructors of the parent class?

We cannot override the constructors of the parent class because the parent class constructor is not inherited when we use the extends keyword.

Conclusion

In this article, we extensively discussed Dart Methods and Method overriding and saw sample programs containing the respective implementations. You may also read the blog Dart Inheritance on the Coding Ninjas Website to further enhance your knowledge about OOPs.


We hope this blog has helped you enhance your knowledge regarding Dart Methods and Method overriding. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass