Multiple inheritance in Dart?

6,629 views
Skip to first unread message

Eduardo Ferrari Copat

unread,
Nov 7, 2012, 8:23:22 AM11/7/12
to mi...@dartlang.org
Is it possible to extend more than one class in Dart?

I did some research, and apparently it is not. Some builds ago, the interface statement was removed, it changed to "abstract class". I tried the following:

abstract class Foo()
{
   
// ....
}


abstract class Bar()
{
   
//....
}


class Other extends Foo, Bar //Syntax error!
{
   
//...
}

I think it was possible to set multiple interfaces before, we had Superinterface as we can check it in the Language Specification(Ctrl + F it), which we could set a list of interfaces as defined in this grammar production:

interfaces:
     
implements typeList
   
;



When we check the extend, it just implements one type:

superclass:
     
extends type
   
;


Shouldn't it be possible to extends several classes allowing Multiple inheritance?

Thanks.



chl

unread,
Nov 7, 2012, 8:31:47 AM11/7/12
to mi...@dartlang.org
Even with interfaces you will only have single inheritance. 

If you want multiple inheritance you should try playing with noSuchMethod. Example: calling a method of an instance variable when the method is not found on the current object.

Cheers!

Kai Sellgren

unread,
Nov 7, 2012, 8:48:33 AM11/7/12
to mi...@dartlang.org
As I understand, traits will arrive at some point in the future. They are meant for horizontal code reuse/inheritance. It's a good thing we can't extend multiple classes.

Ladislav Thon

unread,
Nov 7, 2012, 9:03:04 AM11/7/12
to mi...@dartlang.org
I think it was possible to set multiple interfaces before


You still can. From the very beginning, each class in Dart also defines an implicit interface. So you were always able to write

class Foo { ... }
class Bar extends Foo
{ ... }
class Baz
implements Foo { ... }

The only thing that was changed is that there is no interface keyword. But you can still implement an implicit interface of arbitrary class, including of course abstract classes.


Shouldn't it be possible to extends several classes allowing Multiple inheritance?


I would argue that the correct answer is no, it shouldn't. And mixins are coming in (near?) future, so that will cover a lot of multiple inheritance scenarios.

LT

Eduardo Ferrari Copat

unread,
Nov 7, 2012, 9:21:34 AM11/7/12
to mi...@dartlang.org
I see, thanks everyone!

Damien Cooke

unread,
May 16, 2019, 2:31:32 AM5/16/19
to Dart Misc
Kai,
Why do you think multiple class inheritance is a bad thing? I look at frameworks like Android and iOS (Java/Kotlin/Objective C/Swift)where multiple inheritance would simplify using the framework dramatically.  The whole idea of all UI Android action classes having to extend  Activity or Fragment are fine if we can also extend from our auxiliary classes. But unfortunately we can not, causing quite a bit of code duplication.  I think developers should be given all the tools and languages designers should not limit us in any way, especially on the premise we may not use the feature properly.  Every language can be used to write bad or good code.  So give us the tools to do our jobs using our creativity unlimited by language designers biases.

Kendrick Wilson

unread,
May 16, 2019, 4:59:27 AM5/16/19
to mi...@dartlang.org
Google diamond inheritance.....

Also, it is generally better to favor composition over inheritance for code reuse....

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/c04e343a-8265-4e1f-8a32-3abbc04bfbee%40dartlang.org.

Jonathan Rezende

unread,
May 16, 2019, 8:05:12 AM5/16/19
to mi...@dartlang.org
Aren't mixins enough for code reuse?



--
Jonathan Rezende
Reply all
Reply to author
Forward
Message has been deleted
0 new messages