EXTENDS
Inheritance allows us to create new classes that reuse, extend and/or modify pre-existing classes behavior. The pre-existing class is called superclass and the new class we're creating is called derived class. In Dart, we can inherit only one superclass, but inheritance is transitive. If the class Hatch
is derived from the class Car
and this class is derived from class Vehicle
, then Hatch will be derived from Vehicle. Use extends
to create derived class, and super
when you want to refer to the superclass.
Example :
Let us consider an example of implementation of the ‘extends’ keyword. There is no need to override the definition of the inherited class and can use the existing definition in the child class.
// This is the definition of the parent class ' teacher’
class teacher
{
// function member output( )
void output( )
{
print( " All classes have a teacher ! " ) ;
}
}
/* This is the definition of the base class ' student ' that inherits
* the base class ' teacher ' */
class student extends teacher
{
// function member output( )
void display( )
{
print( " Every student is taught by some teacher ! " ) ;
}
}
void main( )
{
/* creating an object of the derived class ' student '. This object
* can access the mmebers of both the classes ' student ' and ' teacher '
* because ' student ' class inherits ' teacher ' class. */
var s1 = new student( ) ;
// accessing member function output( ) of ' teacher ' class
s1.output( ) ;
// accessing member function display( ) of ' student ' class
s1.display( ) ;
}
Output :
All classes have a teacher!
Every student is taught by some teacher!
IMPLEMENTS
An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the user with the blueprint of the class that any class should follow if it interfaces with that class. If a class inherits another class, it should override ( re-define ) the functions present inside that interfaced class in its own way as per the subject. In Dart, there isn't a specific or direct way of creating the interfaces. To implement them, we use the ' implement ' keyword. By default, every class is an interface in itself containing all the interface members and the members of any other interfaces that it implements.
Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface. A class implements one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces.
Example 2:
// definition of the interface class ' Interface1'
class Interface1
{
// definition of the display1( ) function of the ' Interface1 ' class
void display1 ( )
{
print( " This is the function of the interface class ' Interface1 '. " ) ; }
}
// definition of the interface class ' Interface2 '
class Interface2
{
// definition of the display2( ) function of the ' Interface2 ' class
void display2( )
{
print( " This is the function of the interface class ' Interface2 '. " ) ; }
}
// definition of the interface class ' Interface3’
class Interface3
{
// definition of the display3( ) function of the ' Interface2 ' class
void display3( )
{
print( " This is the function of the interface class ' Interface3 '. " ) ;
}
}
// definition of the subclass implementing the above three interface classes
class subclass implements Interface1, Interface2, Interface3
{
void display1( )
{
print(" Overriding the display1( ) function in the subclass. " ) ;
}
void display2( )
{
print( " Overriding the display2( ) function in the subclass. " ) ;
}
void display3( )
{
print ( " Overriding the display3( ) function in the subclass. " ) ;
}
}
void main( )
{
// creating the object of the subclass
subclass s1 = new subclass( ) ;
// calling overridden methods of the interface classes in the subclass
s1.display1( ) ;
s1.display2( ) ;
s1.display3( ) ;
}
Output :
Overriding the display1( ) function in the subclass.
Overriding the display2( ) function in the subclass.
Overriding the display3( ) function in the subclass.
WITH
Mixins are a way to reuse class methods in multiple class hierarchies. Mixins can be understood as abstract classes that are used to reuse methods in different classes with the same functions / attributes. Mixins are a way of thinking and reusing the working the family of operations and state. It is similar to the reuse you get from extending a class, but not multiple inheritances. There is still only one superclass.
The ' with ' keyword is used to install Mixins. Mixin is a different type of structure, which can only be used with a keyword ' with '.
Mixin is a different type of structure, which can only be used with a keyword ' with '. In Dart, the class can play the role of mixin if the class does not have a constructor. It is also important to note that mixin does not impose a limit type and imposes restrictions on use in class methods.
Example 3:
// mixin with name ' teacher '
mixin teacher {
void func( ) {
print( ' This is the function of the mixin teacher ' ) ;
}
}
// mixin with name ' student '
mixin student {
void func2( ){
print( 125 ) ;
}
}
// mixin type used with keyword
class Principal with teacher, student{
@override
void func( ) {
print( ' We can override function inside this class if needed ' ) ;
}
}
void main( ) {
var princi = Principal( ) ;
princi.func( ) ;
princi.func2( ) ;
}
Output :
We can override function inside this class if needed
125