Education
Software Development Executive - II
Last updated onNov 1, 2023
Last updated onOct 31, 2023
Although Dart and Flutter have had a profound impact on how we construct intuitive, performant applications, there's always room to expand our knowledge. With that spirit, let's talk about one of Dart's integral concepts - Concrete classes.
In Dart, a class serves as the blueprint for creating objects. A class encapsulates data for the object using variables, also known as properties. A class gives methods that allow one to maintain what should happen when certain class functionalities are invoked. Concrete classes in Dart are the classes where all methods are typically implemented.
Before we dive into what a concrete class is, let's understand some foundational concepts in object-oriented programming.
A class in an object-oriented paradigm forms the blueprint for creating objects. An object represents an instance of a class. Classes in Dart, whether abstract or concrete, embody this principle, providing a template to create objects.
Coming to the concepts of abstract class and concrete class, here is the difference:
An abstract class serves as a master template. It includes methods but lacks implementation of those methods. The classes that extend this abstract class must implement these left-out methods. Not doing so will result in a compilation error. An abstract class cannot be instantiated.
1abstract class Animal { 2 void makeNoise(); 3}
A concrete class is a typical class that can be instantiated. It comprises properties and methods, and unlike abstract classes, all of its methods are implemented. Because all of its methods are defined, we can create objects from a concrete class.
In essence, an abstract class could be likened to a theoretical concept, whereas a concrete class is practical, with all the tools at its disposal.
The concept of a concrete class is inherent in object-oriented programming and is not confined to Dart or Flutter. A concrete class is a standard class where each method carries its specific implementation. No method in a concrete class should lack an implementation.
Understanding this is crucial in Dart programming, especially when working with abstract and concrete classes. Remember, unlike an abstract class, a concrete class must not contain any abstract methods as defined by the abstract keyword.
Comprehending the properties of a concrete class is key to writing effective, efficient Dart code. Let's Frame this important concept in a Dart and Flutter context.
Dart presents an elegant approach to using concrete classes. Firmly rooted in object-oriented principles, Dart allows for comprehensive and efficient programming using concrete classes.
Consider the following example:
1class Circle { 2 double radius; 3 4 Circle(this.radius); 5 6 double calculateArea() { 7 return 3.14 * radius * radius; 8 } 9}
Here, Circle serves as a concrete class— all the methods within this class are completely defined.
It's with such concrete classes that we can create objects or instances. This is how we can create an instance of Circle:
1void main() { 2 var myCircle = Circle(5.0); 3 print("Area of my circle is: ${myCircle.calculateArea()}"); 4}
In this example, myCircle is an instance of the Circle concrete class we defined earlier. We've defined a method within this class to calculate the area of a circle, and then we can use this method with the instance of our class.
Defining Concrete Classes in Dart is a straightforward process. A concrete class is defined simply with the class keyword followed by the class name. The properties and methods within that class are then declared and defined.
1class Shape { 2 void draw() { 3 print('Drawing a Shape'); 4 } 5}
To elaborate, we start with the class keyword, followed by the class name Shape. Inside this class, we define a method called draw() that prints some string when called.
To use this class, we simply create an instance and call the methods or properties defined on it.
1void main() { 2 var shape = Shape(); 3 shape.draw(); 4}
In the example above, we first create an instance of our Shape class and call it shape. We then call the draw() method we defined within our class. When we call shape.draw(); it will print “Drawing a Shape”.
Concrete classes in Dart share general properties with other classes in object-oriented programming, yet they bear a few unique characteristics as well.
The primary property of a concrete class is that all its methods have a complete implementation. That means with concrete classes, we can instantiate them, and every method declared in the class has a clear, working implementation.
Let's illustrate that by creating a 'Circle' class that extends our earlier 'Shape' class and overwrites the 'draw' method:
1class Circle extends Shape { 2 @override 3 void draw() { 4 print('Drawing a Circle'); 5 } 6}
And use this concrete class:
1void main() { 2 var circle = Circle(); 3 circle.draw(); 4}
This code will print 'Drawing a Circle'.
This characteristic sets concrete classes apart from abstract classes, where some methods lack a definition inside the abstract class, compelling the classes that extend the abstract classes to provide the implementation.
Understanding the difference between abstract and concrete classes in Dart is key to leveraging them appropriately in your Flutter projects. As we've seen, a concrete class can be instantiated and used directly, while an abstract class serves as a blueprint and cannot be used to create objects.
Let's elaborate on an example:
abstract class 'Animal': It would have the abstract method 'makeNoise' that every class extending it must implement.
1abstract class Animal { 2 void makeNoise(); 3}
Now if we have a concrete subclass 'Dog' that extends this Animal abstract class:
1class Dog extends Animal { 2 @override 3 void makeNoise() { 4 print('Woof Woof'); 5 } 6}
'Dog' being a concrete class, implements 'makeNoise'.
Knowing when to use abstract classes and when to favour concrete classes can optimize the structure and efficiency of your Flutter code.
Concrete classes not only streamline Flutter development but also enforce a consistent structure in your codes, enhancing readability and maintainability.
One of the primary benefits of using concrete classes lies in their self-containment. Since a concrete class in Dart contains a complete implementation of all methods, it ensures that you can instantiate and use it without relying on any other classes for method implementations.
1class Circle extends Shape { 2 double radius; 3 4 Circle(this.radius); 5 6 @override 7 void draw() { 8 print('Drawing a Circle'); 9 } 10 11 double calculateArea() { 12 return 3.14 * this.radius * this.radius; 13 } 14}
As demonstrated, each concrete class can contain unique properties (such as 'radius' in 'Circle'), and specialized implementations that fit the concrete class's requisite functionality. This specificity lends itself to more robust, efficient, and organized code that is easier to debug and maintain.
Throughout your journey with Dart and Flutter, it's essential to adhere to a few best practices. It always pays off and results in elegant, maintainable, efficient code. Let's find out what those are in terms of concrete classes:
1class Lion extends Animal { 2 @override 3 void makeNoise(){ 4 print("Roar!"); 5 } 6}
With these tips and understanding, your journey with Dart and Flutter Concrete classes will be much more enjoyable and remarkably simplified.
Picking up a new coding concept, let alone fully understanding it, requires time, patience, and persistence. Today, we explored concrete classes, a crucial concept in the Dart language, integral to the Flutter framework. These classes, brimming with fully implemented methods, are your building blocks to more complex structures.
Concrete classes unlock a new level of efficient and intuitive development within the Flutter ecosystem. They offer precise control over classes’ functionality and play a significant role in creating robust and maintainable applications.
Whether you're just dipping your toes into the world of Flutter or ramping up for your next big project, understanding the power of concrete classes is a game-changer. Keep practising, coding, and embracing the journey. Your Flutter knowledge and expertise will only grow from here.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.