×

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 Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 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 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 Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 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.