Skip to main content

Inheritance In Dart

So now continuing to the Blog series I will talk about the inheritance concept in this post. In previous blogs, we have seen how to create classes and objects and we can create as many classes as we want.

But sometimes we observe that many classes have some properties same as another class. We should keep in mind a fact that creation of an object in memory is a very expensive task and we have to minimize it so one of the methods to do so is Inheritance. 

So there are two terms: Parent Class and Child Class. The parent class has some features and the child class has similar features along with some extra specific features and the child can be inherited from the parent class so that there is no redundancy.

Image source Google
So talking about the above image we can see that the vehicle is the parent class and then the car, motorcycle, truck are child class of vehicle and following that again station wagon, jeep are the child class of car and so on.


As we know everything in dart is an object so in the above hierarchy there is one more invisible class even above the vehicle class, that class is known as Object Super Class. All other classes are implicitly inherited from that object class. But we don't have to do anything extra to inherit from object class as the process is implicit.

The keyword used to inherit one class from other is extends.

class Vehicle {
//Properties
String name, color;
int modelNo;
//Methods
void showMaxTyres() => print('8');
}

class Car extends Vehicle {
//specific properties of child class
int mileage;
}

 So here we can use all the properties and methods of the class vehicle in the class Car also along with the extra properties of the class Car itself.

But now comes the point that what happens if some properties or methods of the child is different from the parent class. For example, we can see in above code that  class vehicle has a method showMaxTyres which prints 8 as we know a vehicle can also contain truck which can have 8 tyres also but then comes the class Car and from the above code if we call the showMaxTyres for car object then it will again print 8 which not the case, in general, it should be 4. 

So to overcome this problem we have an option to override.

class Vehicle {
String name, color;
int modelNo;
void showMaxTyres() => print('8');
}

// As we assumed the max no of tyres in a vehicle can be 8 but in case of cars it
can be only 4 so we have to override the method.

class Car extends Vehicle {
int mileage;


//overriding the method for Car class
@override
void showMaxTyres( ) {
print("4");
}
}

Now if we call showMaxTyres method for an object of Car Class then 4 will be printed.

**There is one more benefit of override that there is a method in the Object superclass named  
.toString  and we can override this method and can use it to perform any task we want without declaring any extra method for that task.


Now we will see how we can inherit a class from another class which has Containers.

So suppose we have a parent class which contains constructor then, to inherit a child class from that class we will have to define a constructor for the child class also. Not only this we will have to call the constructor for the parent class also at the time we are creating the constructor for the child class.

class Vehicle{
String name;
int model;

//constructor for the parent class
Vehicle(this.name, this.model);
}

class Car extends Vehicle{
//extra property for child class
num mileage;

//constructor for child class
Car(String name, int model, this.mileage):super(name, model);
}

As we can see here that the constructor for the parent class is also called at the time of creating child class constructor. Here super means the class from which it has been inherited.
Also, one more thing that you might have noticed is that the child class must have at least as many arguments in the constructor as the parent class has.

So, this was about the concept of  Inheritance and we are moving closer to start our journey with flutter app development. In the next post, I will be talking about the abstract class and interface. Till then if you have any doubt feel free to ask in the comment section. Any suggestion is highly appreciated. Follow for more updates.


Comments

Popular posts from this blog

IC Recognition from a Printed Circuit Board

In this post, I will tell you about a project that I and my friend did last year. That was IC detection from a printed circuit board and then recognize it using optical character recognition. This is the application of Open Computer Vision (OpenCV). First of all, I will show you a flowchart that will describe our methodology and then I will discuss in detail. There are two main parts of our algorithm: Localizing each IC on the PCB, extracting and saving it. Then we use Tesseract OCR engine to read the labels of each detected IC. A. PREPROCESSING Before we apply any algorithms to our image we have to process it to obtain a proper  image. To save processing time we will resize the image maintaining the aspect ratio.  After that remove noise from the image using Gaussian Blur. B. SEGMENTATION First, we convert the RGB image to HSV (Hue, Saturation, Value) color space. The H channel creates a mask for all the integrated circuits on the board. We...

Constructors In Dart Language

In the previous post, I have discussed Classes and Objects and continuing to that in this post I will cover Constructors. So what a constructor is? As in the previous post, we have created an object car inside the main function using the new keyword, it only creates a generic object without any properties or we can say it is just a spot in memory. And in the next few lines when we assign values to their attributes then it is completely constructed. Although, constructing directly inside the main function is a good idea but there is another way also by which we can form a constructor inside the class itself. But stop I am forgetting something. Before we go deep into constructors I want to tell you something about a very important thing this . this is used to denote the current value. Maybe it is not making sense at this time but it will be clear once we use it. Now coming back to constructors, it is a function or method like entity but without any return type. There are thr...