Method Overriding

5. Object-Oriented

What is Polymorphism? The polymorphism is a combination of the two Greek words poly, which means many and morph means morphing into different forms or shapes. Together, polymorphism means the same entity can be used in various forms. In the programming aspect, the same method can be used in different classes. This technique makes programming more intuitive and more accessible. For example – We have Shape class to define the shape of the object. The shape can be the circle, rectangle, square, straight line, etc. So here the goal is common, but the approach is different. The method overriding is a technique to achieve polymorphism. Sometimes, we want a subclass object to give different results for the same method when subclass object invokes it. This can be done by defining the same method again in subclass. The method has the same name, same arguments, and the same return type. When that method is called, the subclass’s method is executed instead of the method defined in the superclass. Method Overriding When we declare the same method in the subclass, which is previously defined in the superclass is known as the method overriding. The subclass can define the same method by providing its own implementation, which is already exists in the superclass. The method in the superclass is called method overridden, and method in the subclass is called method overriding. Let’s understand the method overriding in the following example. Method Overriding Example We define two classes; first, is a subclass called Human, and the second is a superclass Boy. The Boy subclass inherits the Human superclass. The same method void showInfo() in both classes is defined with the different implementation. The subclass has its own definition of the void showInfo(). Let’s have a look at the following code snippet. Example – Output: Explanation: In the above example, we defined a method with the same name in both, subclass and superclass. The purpose of method overriding is to give the own implementation of subclass method. When we created the object of the Boy subclass, it executed the subclass method and printed the Man is running instead of Human is running. If we create the object of a parent class, then it will be always invoked the parent class method. Let’s take another example where we create two Classes called College and Student with common method void student_details(). Let’s have a look at the following code snippet. Example – 2 Output: Explanation: In the above example, we create two classes – College as a parent class and Student as a child class. The method stu_details defined in both classes with the same parameters and same return types. Now, the College superclass is inherited by the Student subclass and the stu_details() method is overridden in the subclass. We created the object of Student and to invoked the stu_details() with suitable arguments. It executed the subclass method, and then it printed the result. Same as we created the object of College superclass object invoked its methods and printed the different results. Method Overriding using super Keyword We can invoke the parent class method without creating its object. It can be done by using the super keyword in the subclass. The parent class data member can be accessed in the subclass by using the super keyword. Let’s understand the following example. Example – Output: Explanation: In the above program, we accessed the Human class method in child class using the super keyword. Now, we don’t need to instantiate the parent class. We only created the object of subclass, which invoked the run() method of child class and parent class. Note – When we created the child class object and invoked the method, it executes the parent class (if accessed by super keyword) method first, then the child class method. Advantage of method overriding The main benefit of the method overriding is that the subclass can provide its own implementation to the same method as per requirement without making any changes in the superclass method. This technique is much when we want to subclass method to behave differently also with the same name. Rules of Method overriding in Dart The few rules of method overriding are given below. These points must be kept in mind while declaring the same method in subclass.

October 12, 2024 / 0 Comments
read more

Methods

5. Object-Oriented

A Dart method is the collection of statements that consists of some characteristics to class object. It provides the facility to perform some operation and it can be invoked by using its name when we need in the program. Methods divide the large task into small chunks and perform the specific operation of that program. This process increases the code reusability and enhances the modular approach of program. The methods can be defined with parameters that are passed as information to complete the specific task, after that it can return value to where it called or return nothing. Methods that define in the class either instance method or class methods. Instance Methods A method that can be accessed by using the instance of class is called instance methods. The instance methods can be no arguments or with arguments. The instance method can access by instance variable this keyword. Creating Instance Methods An instance method is defined by a name and valid return type. It may have list of parameters. The syntax is given below. Syntax: Calling instance Method Instance methods can be accessed by using the class object, thus we required to create object to use it. The syntax is given below. Syntax: Class Methods The class method is declared with the static keyword. It can be accessed by using the class name instead of the class object. These methods are common to all instances of that individual class. The static methods only can access the static variables. Creating Class Methods A static method is declared by using static keyword followed by method name with return type. The syntax is given below. Syntax: Calling Class Method The class method can be called directly by using the class name instead of class object. The syntax is given below. Syntax:

October 12, 2024 / 0 Comments
read more

Super Constructor

5. Object-Oriented

The child class can inherit all properties (methods, variables) and behavior of parent expect parent class constructor.& The superclass constructor can be invoke in sub class by using the super() constructor. We can access both non-parameterized and parameterized constructor of superclass. Accessing the constructor of superclass is slightly different in the Dart. The syntax is given below. Syntax: Implicit super As we know that the constructor is automatically called when we instantiate a class. When we create the object of sub class, it invokes the constructor of sub class which implicitly invokes the parent class’s default(non-parameterized) constructor. We can use super() constructor in our subclass to invoke superclass constructor. Let’s understand the following example. Example – Output: Explicit super If the superclass constructor consists of parameters then we require to call super() constructor with argument in to invoke superclass constructor in subclass explicitly. Let’s understand the following example. Example – Output:

October 12, 2024 / 0 Comments
read more

Dart Inheritance

5. Object-Oriented

Dart inheritance is defined as the process of deriving the properties and characteristics of another class. It provides the ability to create a new class from an existing class. It is the most essential concept of the oops(Object-Oriented programming approach). We can reuse the all the behavior and characteristics of the previous class in the new class. Suppose we have a fleet of cars, and we create three classes as Duster, Maruti, and Jaguar. The methods modelName(), milage(), and man_year() will be same for all of the three classes. By using the inheritance, we don’t need to write these functions in each of the three classes. As you can see in the above figure, if we create class Car and write the common function in each of the classes. Then, it will increase duplication and data redundancy in the program. The inheritance is used to avoid this type of situation. We can avoid data redundancy by defining the class Car with these functions in it and inheriting in the other classes from the Car class. It enhances the re-usability of code. We just need to write function one time instead of multiple times. Let’s have a look at the following image. The syntax is given below. Syntax – The child class inherits functions and variables, or properties of parent class using the extends keyword. It cannot inherit the parent class constructor; we will discuss this concept later. Types of Inheritance The inheritance can be mainly four types. These are given below. Single Level Inheritance In the single inheritance, a class is inherited by a single class or subclass is inherited by one parent class. In the following example, we create Person which inherits Human class. Let’s understand the following example. Example – Output Explanation: In the above code, we create parent class Bird and declared the fly() function in it. Then, we created the child class called Parrot, which inherited the parent class’s property using the extends keyword. The child class has its own function speak(). Now the child class has two functions fly() and speak(). So we created the object of child class and access both functions. It printed the result to the console. Multilevel Inheritance In the multiple inheritance, a subclass is inherited by another subclass or creates the chaining of inheritance. Let’s understand the following example. Example – Output Explanation: In the above example, we created another new class Eagle and inherited the Parrot class. Now the parrot is the parent class of Eagle, and class Eagle acquired all functions of both parent classes. We created the object of the child class and accessed all properties. It printed the output to the screen. Note – Dart doesn’t support multiple inheritance because it creates complexity in the program. Hierarchical Inheritance In the hierarchical inherence, two or more classes inherit a single class. In the following example, the two-child classes Peter and James inherit the Person class. Example – Output

October 12, 2024 / 0 Comments
read more

Dart super Keyword

5. Object-Oriented

The super keyword is used to denote the instant parent class object of the current child class. It is used to invoke superclass methods, superclass constructor in its child class. The super keyword’s main objective is to remove the confusion between parent class and subclass with the same method name. It is also used to refer to the superclass properties and methods. Usage of static Keyword Using a super keyword with variables This situation arises when the child class has the same name variables as superclass variables. So there is a chance of ambiguity for Dart compiler. Then super keyword can access the superclass variables in its child class. The syntax is given below. Syntax: We can more understand by the following example. Example – Output Explanation: In the above code, we defined the superclass as Car, which has a speed variable, and then it inherited by the subclass Bike. The sub class also has variable speed, so we used the super keyword to access the parent class variable. We created the object of child class b and called a display method that printed the value of superclass variable. If we use the print(speed) instead of print(super.speed), it will print the value the subclass variable. You can copy the above code, paste it into your dartpad or notepad, and print the value without using the super keyword. You can see the difference between both results. Using the super keyword with parent class method As discussed earlier, the super keyword is used to access the parent class method in child class. If the child class and parent class consist of the same name, then we can use the super keyword to use the parent class method in child class. The syntax is given below. Syntax: Let’s understand the following example Example – Output Explanation – In the above code, we created the function with the same name in both parent class and child class. The display() method is present in both parent class and child class that means It is a situation of method overriding. So we created a message() method in the child class, inside it, we called parent class method using the super keyword, and then created the object of the child class. Using the object, we called the message() method that printed both display() methods statements to the screen. Note – The super can be used only if the subclass method overrides the superclass method. If it doesn’t then we don’t require using super keyword. Using super keyword with constructor We can also use the super keyword to access the parent class constructor. The super keyword can call both parameterized and non-parameterized constructors depending on the situation. The syntax is given below. Syntax: Let’s understand the following example. Example – Output Explanation The syntax of Dart language is too close to C# language. We called the parent class constructor using the super keyword, which is separated by : (colon). The delegate a superclass constructor, we should put the superclass as an initializer.

October 12, 2024 / 0 Comments
read more

Dart static Keyword

5. Object-Oriented

The static keyword is used to declare the class variable and method. It generally manages the memory for the global data variable. The static variables and methods are the member of the class instead of an individual instance. The static variable or methods are the same for every instance of the class, so if we declare the data member as static then we can access it without creating an object. The class object is not required to access the static method or variable we can access it by putting the class name before the static variable or method. Using the class name, we can call the class method from the other classes. Dart Static Variable A variable which is declared using the static keyword inside the class is called Dart static keyword. These are the member of the class instead of a specific instance. The static variables are treated the same for all instances of the class; it means a single copy of the static variable is shared among all instances of classes. It allocates memory once and at the class loading and uses throughout the program. Point to Remember – Declaring Static Variable Dart provides the static keyword to declare the static variable. It is declared by using the static keyword followed by the variable name. The syntax is given below. Syntax: Accessing Static Variable We can access the static variable by using the class name itself instead of creating an object of it. The syntax is given below. Syntax: Dart Static Method The concept of the static method is also similar to static variable. The static methods are the member of the class instead of the class instance. The static methods can use only static variables and can invoke the static method of the class. We don’t need to create an instance of class the access it. The static method is useful when we want to use it in other classes. Points to Remember Declaring Static Methods We can declare the static method by using the static keyword followed by the method name with the return type. The syntax is given below. Syntax: Calling Static Method The static methods can be called by using the class name, which they belong to instead of creating an object. Syntax: Let’s understand the following example. Example – Output Explanation: In the above code, we declared the class called Student, which has three fields including static variable stdBranch and one method showStdInfo(). We created two instances of class Student and assigned values to the class variables. The static variable stdBranch accessed by using the class name and assigned value. Then, we called the showStdInfo() function by objects std1 and stu2. It printed details of the student as an output.

October 12, 2024 / 0 Comments
read more

Dart this Keyword

5. Object-Oriented

The this keyword is used to refer the current class object. It indicates the current instance of the class, methods, or constructor. It can be also used to call the current class methods or constructors. It eliminates the uncertainty between class attributes and the parameter names are the same. If we declare the class attributes same as the parameter name, that situation will create ambiguity in the program, then the this keyword can remove the ambiguity by prefixing the class attributes. It can be passed as an argument in the class method or constructors. Let’s understand the following example of how this keyword works. Example – Without using this keyword Output Explanation: In the above program, we created a class called Mobile, which has two attributes modelname and man_year. Then, we created a constructor and passed parameters the same as the class attributes name. In the constructor body, the class variables (attributes) on the left side are assigned by the constructor parameters with the same name. When we create the instance of a class, the constructor automatically called the constructor and printed the result. The Dart compiler might get confused if there are a number of the same name parameters. The result is the compiler will create ambiguity. That’s why we use the this keyword to refer the current class object. Example – 2: Using this keyword Output Explanation: The above example is the same as the previous program, but the only difference of this keyword. We have used the this keyword to the different instance or class variable from the local variable. Points to Remember Local Variable Local variables are defined in the methods, constructors, or blocks. It is created when we create a method or constructor, and it has scope only inside them. We cannot use a local variable outside the method, constructor, or block. Class Variable Class variable is also known as the static member variable, which is used to declare using the static keyword. It is declared in the class, but outside a constructor, method or a block. All instances share one copy of the class variable or we can say that class variables are common to all instances of that class. Instance Variable Instance variable is also known as the non-static, variable which is used to declare without the static keyword. The instance variables are specific by an object. These variables can be accessed using the instance of that class. Difference Between Class Variable and Instance Variable The following are the difference between the class variable and instance variable Sr. Class Variable Instance Variable 1. The class variable is declared using the static keyword in a class, but not in method and constructor. The instance variable is declared in a class without using the static keyword. 2. The class variable can be accessed using the class name.Syntax:ClassName.variableName The instance variable can be accessed using the instance of that class.Syntax:ObjectRefernce.variableName 3 The class variables are common to all instances of that class. All instances of the class share one copy of the static variable. The instance variables are not common to all instance of class. Each object of particular will preserve its own copy of the instance variables. 4 These are created when the program is started and destroys when the program is terminated. The instance variables are created when an object of the particular class created using the new() keyword and destroys when the object is destroyed.

October 12, 2024 / 0 Comments
read more

What is constructor?

5. Object-Oriented

A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return type. The generative constructor is the most general form of the constructor, which is used to create a new instance of a class. It is option to declare within the class. All class have own constructor but if we don’t declare or forget then Dart compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored. Example – Suppose we have a class name Student and we will create an object of it as follow. It invoked the default constructor of the Student class. Creating Constructor in Dart As we have mentioned, a constructor has the same name as its class name and it doesn’t return any value. Suppose if we have class Student then the constructor name should be also Student. Syntax: We must remember the following two rules while creating a constructor. Let’s understand the following example. Example – Output Explanation: In the above example, we created a constructor function Student() which is same as the class name. We passed two passed parameter in the constructor and when we instantiated an object of Student class and passed value it automatically called the constructor then printed the result. Types of Constructors There are three types of constructors in Dart as given below. Default Constructor or no-arg Constructor A Constructor which has no parameter is called default constructor or no-arg constructor. It is automatically created (with no argument) by Dart compiler if we don’t declare in the class. The Dart compiler ignores the default constructor if we create a constructor with argument or no argument. The syntax is given below. Syntax: Let’s understand the following example. Example – Output Parameterized Constructor We can also pass the parameters to a constructor that type of constructor is called parameterized constructor. It is used to initialize instance variables. Sometimes, we need a constructor which accepts single or multiple parameters. The parameterized constructors are mainly used to initialize instance variable with own values. The syntax is given below. Syntax: Let’s understand the following example. Example – Output Explanation – In the above example, we declared a parameterized constructor which has two parameter name and age. We created an object of the Student class and passed the appropriate value to the constructor. It printed the name and age as an output to the screen. Named Constructors The named constructors are used to declare the multiple constructors in single class. The syntax is given below. Syntax: Let’s understand the following example. Example – Output

October 12, 2024 / 0 Comments
read more

Classes and Object

5. Object-Oriented

Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that binds/encapsulates the data and functions together; that can be accessed by creating an object. A class can refer to as user-define data type which defines characteristics by its all objects. We can assume a class as a sketch (prototype) or a car. It contains all the details about model name, year, features, price, etc. Based on these properties of the car, we can build the car. Here the car is an object. There can be many cars so we can create many objects of cars to access all the properties. Defining a Class in Dart Dart provides class keyword followed by a class name is used to define a class; all fields and functions are enclosed by the pair of curly braces ({}). The syntax is given below. Syntax: Here, the ClassName represents the actual name of the class, which is defined by the user. In curly braces, we provide a class definition. A class can consist of fields, constructors, getters setters, and methods. Note – According to the naming convention rule of identifiers, the first letter of the class name must be capital and use no separators. Let’s understand the following example. Example – In the above example of class, we declared a class called Student. This class has three fields stdName, stdAge, and stdRoll_nu. The showStdInfo() is a class function which prints the fields of class. To access the properties of the class, we need to create its object. Dart Object Dart is object-oriented programming, and everything is treated as an object in Dart. An object is a variable or instance of the class used to access the class’s properties. Objects have two features – state and behavior. Suppose a man is an object with a state (name, age, health) and behavior (walking, running, and sleeping). Programming objects are theoretically similar to the real-life objects; they also have state and behavior. An object is created from a template which is known as class. The fields of the classes are stored as object states, whereas the method represents an object’s behavior. Creating Class Objects in Dart After creating the class, we can create an instance or object of that class which we want to access its fields and functions. The new keyword is used to declare class followed by the class name. The general syntax of creating an object of a class is given below. Syntax: Here, object_name and class_name signifies as the actual object name and class name respectively. If the class constructor is parameterized then constructor arguments must be passed value. Let’s understand the following example. Example – We have created the object called std of the class Student but only creating an object not enough. We have to access the properties by using the newly created object. Assessing Instance Variable and Function After creating an object, we can access the fields and methods of the class. The class property name is separated by the (.) operator with the instance name. The syntax is given below. Syntax: Let’s understand the following example. Example – Output Explanation: In the above example, we created a class called Student, which consisted of the student name, age, roll number, and showStdInfo() function to show the student details. Then, we created a Student class object and assigned the values to each field by using the (.) operator. We called the showStdInfo() function that displayed the details as an output to screen. Benefit of Objects There are various benefits of using object-oriented programming. Below are the few benefits.

October 12, 2024 / 0 Comments
read more

Object-Oriented Concepts

5. Object-Oriented

Dart is an object-oriented programming language, and it supports all the concepts of object-oriented programming such as classes, object, inheritance, mixin, and abstract classes. As the name suggests, it focuses on the object and objects are the real-life entities. The Object-oriented programming approach is used to implement the concept like polymorphism, data-hiding, etc. The main goal of oops is to reduce programming complexity and do several tasks simultaneously. The oops concepts are given below. Below is the brief introduction of these oops concepts. Class Dart classes are defined as the blueprint of the associated objects. A Class is a user-defined data type that describes the characteristics and behavior of it. To get all properties of the class, we must create an object of that class. The syntax of the class is given below. Syntax: Object An object is a real-life entity such as a table, human, car, etc. The object has two characteristics – state and behavior. Let’s take an example of a car which has a name, model name, price and behavior moving, stopping, etc. The object-oriented programming offers to identify the state and behavior of the object. We can access the class properties by creating an object of that class. In Dart, The object can be created by using a new keyword followed by class name. The syntax is given below. Syntax: Inheritance Dart supports inheritance, which is used to create new classes from an existing class. The class that to be extended is called parent /superclass, and the newly created class is called child/subclass. Dart provides extends keyword to inherit the properties of parent class in child class. The syntax is given below. Syntax: Polymorphism Polymorphism is an object-oriented programming concept where one thing has many forms. It can be two types – Runtime polymorphism and Compile time polymorphism. For example – A function has the same name but with a different behavior or class. Another example is the shape() class, and all the class inherited from the Rectangle, Triangle, and circle. Interfaces The interface is defined as a blueprint of the class. We can declare methods and variables inside the interface just like the class but in interface only abstract declaration of methods is provided. We can only define the function signature but not its body. Another class can implement the interface. It is basically used for data-hiding. Abstract Class A class that contains one or more abstract method is called an abstract class. We can declare the abstract class using the abstract keyword followed by class declaration. The syntax is given below. Syntax: The above introductions give the basic idea of oops concepts. We will have a detailed discussion in upcoming tutorials.

October 12, 2024 / 0 Comments
read more

Posts pagination

Previous 1 … 114 115 116 … 445 Next