PermalinkDoes dart support multiple inheritances?
Dart does not support multiple inheritances directly. This means that a class cannot inherit from multiple classes at the same time. However, Dart does provide a way to achieve similar functionality using mixins.
Mixins allow you to reuse code across multiple class hierarchies without requiring multiple inheritances
with
keyword helps to allow this.
class Animal {
void move() {
print('Animal is moving.');
}
}
class CanSwim {
void swim() {
print('Animal can swim.');
}
}
class Dolphin extends Animal with CanSwim {}
class Duck extends Animal with CanSwim, CanFly {}
void main() {
Dolphin dolphin = Dolphin();
dolphin.move(); // Output: Animal is moving.
dolphin.swim(); // Output: Animal can swim.
Duck duck = Duck();
duck.move(); // Output: Animal is moving.
duck.swim(); // Output: Animal can swim.
duck.fly(); // Output: Animal can fly.
}
Permalinkinheritance, private, abstract class in dart
In Dart, inheritance
is the mechanism by which one class can inherit properties and methods from another class. The class that is being inherited from is called the base class or superclass, and the class that is inheriting from it is called the derived class or subclass.
Inheritance example
class Animal {
String name;
Animal(this.name);
void makeNoise() {
print('$name is making noise.');
}
}
class Dog extends Animal {
Dog(String name) : super(name);
void wagTail() {
print('$name is wagging its tail.');
}
}
private
In Dart, you can use the underscore character (_) to create private class members, including private properties and methods. Private members can only be accessed within the same library (which is a collection of Dart code files).
Private Code Example
class Person {
String _name;
Person(this._name);
void _doSomething() {
print('$_name is doing something.');
}
}
void main() {
Person person = Person('Alice');
person.introduce();
print(person._name);
}
abstract classes
In Dart, you can also define abstract classes, which are classes that cannot be instantiated on their own but can be used as a base class for other classes. Abstract classes can define abstract methods, which are methods that have no implementation and must be implemented by any concrete subclass.
abstract class Animal {
String name;
Animal(this.name);
void makeNoise();
}
class Dog extends Animal {
Dog(String name) : super(name);
void makeNoise() {
print('$name is barking.');
}
}
void main() {
Dog dog = Dog('Fido');
dog.makeNoise(); // Output: Fido is barking.
}
PermalinkMethod overload VS overriding
Method overloading and overriding are two important concepts in object-oriented programming (OOP) that are also supported in Dart.
Method overloading refers to the practice of defining multiple methods with the same name in the same class, but with different parameters. In Dart, method overloading is achieved by defining methods with the same name but different parameter types or numbers.
Method overloading example
class Calculator {
int add(int x, int y) {
return x + y;
}
double add(double x, double y) {
return x + y;
}
}
Method overriding,
on the other hand, refers to the practice of defining a method in a subclass that has the same name and signature as a method in its superclass, but with a different implementation.
In Dart, method overriding is achieved by defining a method in a subclass with the same name and signature as a method in its superclass and using the @override annotation to indicate that it is an override.
Method overriding example
class Animal {
void makeSound() {
print('The animal makes a sound');
}
}
class Cat extends Animal {
@override
void makeSound() {
print('The cat meows');
}
}
PermalinkThe difference between abstract and interface
In Dart, both abstract classes and interfaces provide a way to define a set of methods that a class must implement. However, there are some differences between the two concepts.
An abstract class can have abstract and non-abstract methods, while an interface can only have abstract methods. An abstract method is a method without an implementation in the class that defines it, while a non-abstract method has an implementation. Abstract classes can also have instance variables, while interfaces cannot.
A class can only extend one abstract class, but it can implement multiple interfaces.
When a class extends an abstract class, it inherits the abstract methods of the superclass and can provide an implementation for them. When a class implements an interface, it is required to provide an implementation for all the methods defined in the interface.
Here's an example to illustrate the difference
abstract class Animal {
void makeSound(); // abstract method
void eat() {
print('The animal is eating');
}
}
// Define an interface called Mammal
abstract class Mammal {
void giveBirth();
}
// Define a class called Dog that extends Animal and implements Mammal
class Dog extends Animal implements Mammal {
@override
void makeSound() {
print('The dog barks');
}
@override
void giveBirth() {
print('The dog gives birth to puppies');
}
}
void main() {
// Create an instance of the Dog class
var dog = Dog();
// Call the makeSound() and eat() methods on the Dog instance
dog.makeSound(); // Output: The dog barks
dog.eat(); // Output: The animal is eating
// Call the giveBirth() method on the Dog instance
dog.giveBirth(); // Output: The dog gives birth to puppies
}
PermalinkWhy build keyword used in flutter main
In Flutter, the build
method is used to describe the user interface of a widget. The build
method returns a tree of Elements
that describe what should be rendered on the screen.
The main
method in a Flutter application is the entry point of the application, just like in any other programming language. The main
method is responsible for setting up the application and initializing the Flutter framework.
When you create a Flutter widget, you need to override the build
method to describe what the widget should look like. The build
method is called by the Flutter framework whenever the widget needs to be redrawn on the screen.
So, the build
method is used in the context of a widget to describe its UI, while the main
method is used to initialize the application and start its execution. In the main
method, you typically create an instance of your root widget and pass it to the runApp
method, which starts the application and displays the widget tree on the screen.
PermalinkWhy does Builder use Flutter?
In Flutter, the Builder widget is used when you need to create a widget that has a context but you don't want to expose that context to the outside world.
For example, if you need to build a widget that requires a BuildContext, but you don't want to expose the context to the parent widget, you can wrap that widget in a Builder widget. The Builder widget will create a new BuildContext that is only accessible to the widget it contains, and that context can be used to build child widgets.
PermalinkMyWidget({Key? key}) : super(key: key); Describe this line code
This code creates a constructor for the MyWidget
class in Flutter. The constructor takes an optional Key
object as a parameter, which is used to uniquely identify the widget. The Key?
notation means that the key
parameter is nullable, i.e., it can be either a Key
object or null.
The super(key: key)
call in the constructor calls the parent constructor of the MyWidget
class and passes the key
parameter to it. In this case, the parent constructor is the constructor of the StatefulWidget
class, which takes a Key
object as a parameter. The super
keyword is used to call the parent constructor and pass the key
parameter to it.
Using a unique Key
object for each widget is important in Flutter because it helps the framework to efficiently rebuild the widget tree when necessary. The Key
object provides a stable identity for a widget across multiple build calls, allowing Flutter to match new widget elements with their previous elements and avoid unnecessary rebuilding.
PermalinkWhy super.key use in the dart class constructor
In Dart, the super keyword is used to call the constructor or methods of the parent class. When a subclass is created, it needs to call the constructor of its parent class to set up the inherited properties and methods. The super keyword is used to make that call.
In the case of using super.key in a class constructor, the key argument is a property that is inherited from the parent class. By calling super.key, the subclass is passing that property value to the constructor of the parent class.
This is commonly used when creating Flutter widgets, where a subclass is created to customize or add functionality to an existing widget. The super.key call ensures that the new widget instance has the same key value as its parent widget, which is necessary for proper widget rebuilding and state management.
So, super.key
is used in Dart class constructors to pass properties to the parent class constructor a
PermalinkStateful and stateless widget
In Flutter, widgets are the basic building blocks of the user interface. They define what the user sees and interacts with on the screen.
There are two types of widgets in Flutter: stateful and stateless.
A StatelessWidget
is a widget that does not depend on any external state to render its view. It is immutable, meaning that once it is built, it cannot be changed. A StatelessWidget
is useful when the widget does not need to change its appearance or behavior based on any user interaction or other external events. Examples of StatelessWidget are Text
, Icon
, Button
, and Image
.
PermalinkIs setState() method allows for the stateful and stateless widget
No, setState()
method is only available in Stateful Widgets in Flutter. Stateless widgets do not have a mutable state and therefore do not need to use setState(). Instead, they rebuild their widget tree whenever their build() method is called with new input parameters.
PermalinkIs initState() for both widget
No, initState()
method is only available in Stateful Widgets in Flutter. When a Stateful Widget is first created, Flutter creates a State object to associate with it. The initState() method is called on this State object, allowing you to initialize any state that you need for your widget.
Stateless
widgets do not have a mutable
state and therefore do not need an initState() method. Instead, all initialization must be done within the constructor and build() method of the Stateless Widget.
PermalinkThe difference between late, const and final with code
In Dart, three keywords are used for defining variables with a fixed value. They are:
const: This keyword is used to declare variables whose value is known at compile-time and remains constant throughout the life of the program. The value of a const variable must be a compile-time constant. It is evaluated at compile time rather than runtime.
const int x = 5;
const String name = "John";
final
This keyword is used to declare variables whose value is known at runtime and cannot be changed once initialized. A final variable can be initialized only once, and then its value is fixed for the rest of the program.
final int y = 10;
final double PI = 3.1416;
late
This keyword is used to declare variables whose value is not known at the time of declaration but will be known before the variable is first used. The variable must be initialized before it is used, or an error will occur. Example:
late int z;
void main() {
z = 15;
print(z);
}
The main difference between const and final is that const is evaluated at compile-time, while final is evaluated at runtime. The main difference between final and late is that final must be initialized when it is declared, whereas late can be initialized at a later time.
PermalinkBasic Dart
PermalinkMaterialApp Class
MaterialApp is a predefined class in a flutter. It is likely the main or core component of flutter. We can access all the other components and widgets provided by Flutter SDK. Text widget, Dropdownbutton widget, AppBar widget, Scaffold widget, ListView widget, StatelessWidget, StatefulWidget, IconButton widget, TextField widget, Padding widget, ThemeData widget, etc. are the widgets that can be accessed using MaterialApp class. Many more widgets are accessed using MaterialApp class.
PermalinkHome
It is used for the default route of the app means the widget defined in it is displayed when the application starts normally. Here we have defined the Scaffold widget inside the home property. Inside the Scaffold
, we define various properties like appBar, body, floatingActionButton, backgroundColor, etc. For example, in the appBar
property,y we have used the AppBar()
widget in which as a title we have passed ‘GeeksforGeeks’ which will be displayed at the top of the application in appear.