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