×

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )” function to implement the callable class.

Syntax :

class class_name 
{
String call( < arguments > )
{
// body of the function
// return statement 
}
main( )
{
    // calling the instance just like the functions
}
Consider the following example that explains the concept of callable classes in Dart:

Example – 1 :

// definition of the callable class 
class Colour 
{  
  // definition of the call( ) function
  String call( String c_name, int hex_code ) 
    {  
        return( ' The colour name is $c_name and the hexdecimal code is $hex_code ' ) ; 
    }  
}  
void main( ) 
{  
   // creating the object of the class ' colour '
   Colour col = new Colour( ) ; 
   
   // calling the class instance just like a function
   var info = col( ' Black ', 002147 ) ;   
  
   // printing the information about the colour
   print( ' Dart Callable class ' ) ;  
   print( ' The information of the colour is : ' ) ;
  
   print( info ) ;  
}  

Output :

Dart Callable class 
 The information of the colour is : 
 The colour name is  Black  and the hexdecimal code is 2147

Explanation :

In this code, we defined a call( ) function with return type ‘ String ’ and two arguments of type ‘ String ’ and ‘ int ’ in the ‘ Colour ’ class.

This call( ) method accepts the value of the colour name and it hexadecimal value in the arguments and return the print statement. Then, in the main( ) function, an object ‘ col ’ of the class ‘ Colour ’ is created and the instance of the class call( ) is called just like a function.

var infro = col( ' Sharma ',18 ) ;       

Consider the following example in Dart that shows multiple callable classes

Example - 2 :

class Student 
{  
  String call( String name, int age ) 
  {  
      return( ' The student name is $name and the age is $age ' ) ;  
  }  
}  
class Teacher 
{  
  String call( String t_name ) 
  {  
      return( ' The name of the Teacher is : $t_name ' ) ;
  }  
}  
  
void main( ) 
{  
   Student stu = new Student( ) ;  
  
   Teacher tem = new Teacher( ) ;  
  
   // calling the instance of the class ' Student ' just like a function
   var s = stu( ' Yukta Malhotra ', 19 ) ; 
  
   // calling the instance of the class ' Teacher ' just like a function
   var t = tem( ' Dimple Chawla ' ) ;   


   print( ' Multiple Callable classes in Dart ' ) ;  
   
   print( s ) ;  
   print( t ) ;  
}  

Output :

Multiple Callable classes in Dart 
    The student name is  Yukta Malhotra  and the age is 19 
    The name of the Teacher is :  Dimple Chawla  

Explanation :

In the above code, we defined two callable functions in class ‘ Student ’ and class ‘ Teacher ’. The ‘ Student ’ class has ‘ call( ) ’ function that accepts two parameters of data types ‘ String ’ and ‘ int ’. The ‘ Teacher ’ class has a ‘ call( ) ’ function that accepts only one parameter of type ‘ String ’. We called instances of both the classes as callable functions.

var s = stu( ' Yukta Malhotra ', 19 ) ; 
   var t = tem( ' Dimple Chawla ' ) ;   

Related Topics

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.