Inheritance/Multiple: Difference between revisions
Content deleted Content added
PascalABC.NET |
|||
(29 intermediate revisions by 18 users not shown) | |||
Line 1:
{{Task|Basic language learning}}
[[Category:Object oriented]]
[[Category:Type System]]
Multiple inheritance allows to specify that one [[classes | class]] is a subclass of several other classes.
Some languages allow multiple [[inheritance]] for arbitrary classes, others restrict it to interfaces, some don't allow it at all.
;Task:
Write two classes (or interfaces) <tt>Camera</tt> and <tt>MobilePhone</tt>, then write a class <tt>CameraPhone</tt> which is both a <tt>Camera</tt> and a <tt>MobilePhone</tt>.
There is no need to implement any functions for those classes.
<br><br>
=={{header|Ada}}==
Ada 2005 has added interfaces, allowing a limited form of multiple inheritance.
<
type Camera is tagged null record;
type Mobile_Phone is limited Interface;
type Camera_Phone is new Camera and Mobile_Phone with null record;
end Multiple_Interfaces;</
{{omit from|Modula-2}}
=={{header|Aikido}}==
Aikido does not support multiple inheritance, but does allow multiple implementation of interfaces.
<
}
Line 29 ⟶ 32:
class Camera_Phone implements Camera, Mobile_Phone {
}</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
DIM Camera{TakePicture}
Line 44 ⟶ 47:
PROC_inherit(CameraPhone{}, Camera{})
PROC_inherit(CameraPhone{}, MobilePhone{})
PROC_class(CameraPhone{})</
=={{header|C
C simulates Multiple Inheritance via Structures.
<syntaxhighlight lang="c">
typedef struct{
double focalLength;
double resolution;
double memory;
}Camera;
typedef struct{
double balance;
double batteryLevel;
char** contacts;
}Phone;
typedef struct{
Camera cameraSample;
Phone phoneSample;
}CameraPhone;
</syntaxhighlight>
=={{header|C sharp|C#}}==
Line 71 ⟶ 77:
In the example we inherit from a class and an interface.
<
// ...
}
Line 81 ⟶ 87:
class CameraPhone: ICamera, MobilePhone {
// ...
}</
=={{header|C++}}==
<syntaxhighlight lang="cpp">class Camera
{
// ...
};
class MobilePhone
{
// ...
};
class CameraPhone:
public Camera,
public MobilePhone
{
// ...
};</syntaxhighlight>
=={{header|Clojure}}==
<
(defprotocol MobilePhone)
Line 90 ⟶ 114:
(deftype CameraPhone []
Camera
MobilePhone)</
=={{header|COBOL}}==
<
CLASS-ID. Camera.
*> ...
END CLASS Camera.
IDENTIFICATION DIVISION.
CLASS-ID. Mobile-Phone.
*> ...
END CLASS Mobile-Phone.
IDENTIFICATION DIVISION.
CLASS-ID. Camera-Phone
INHERITS FROM Camera, Mobile-Phone.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 109 ⟶ 137:
*> ...
END CLASS Camera-Phone.</
=={{header|Common Lisp}}==
<
(defclass mobile-phone () ())
(defclass camera-phone (camera mobile-phone) ())</
=={{header|D}}==
Line 121 ⟶ 149:
While D does not have multiple base class inheritance, you can inherit from multiple interfaces.
<
// member function prototypes and static methods
}
Line 132 ⟶ 160:
// member function implementations for Camera,
// MobilePhone, and CameraPhone
}</
D also supports the [[non-virtual interface]] pattern, where an interface may have non-virtual methods with defined implementations.
<
// A virtual function.
Image takePhoto();
Line 147 ⟶ 175:
}
}
}</
In addition, D's alias this feature allows one to create a type that, while it does not technically derive from two different classes, behaves as if it did.
<
string foo() {
return "I am an A.";
Line 188 ⟶ 216:
writeln(a.foo());
writeln(b.foo());
}</
You can currently only have a single alias this, but multiple alias this is planned. Nested alias this works today, but is somewhat finicky.
Line 194 ⟶ 222:
Lastly, D has template and string mixins. These can be used for static polymorphism, where a piece of code is written once and has a single definition, but is used in multiple places. It does not enable any sort of dynamic polymorphism that is not covered above.
<
void register() { /* implementation */ }
}
Line 212 ⟶ 240:
foo.register();
writeln(foo.myFunction());
}</
Using D's [[Compile-time calculation|CTFE]] and [[reflection]] capabilities, string mixins can copy the interface of other types, and thus be used for proxies and mocks.
Line 219 ⟶ 247:
Delphi doesn't support multiple inheritance, but it does have multiple interfaces.
<syntaxhighlight lang="delphi">type
ICamera = Interface
// ICamera methods...
Line 230 ⟶ 258:
TCameraPhone = class(TInterfacedObject, ICamera, IMobilePhone)
// ICamera and IMobilePhone methods...
end;</
=={{header|DWScript}}==
Line 247 ⟶ 275:
These shortcomings could be fixed if more powerful multiple inheritance were needed.
<
def forwarder match [verb, args] {
escape __return {
Line 269 ⟶ 297:
}
return forwarder
}</
The task example:
<
return def camera extends minherit(self, []) {
to takesPictures() { return true }
Line 295 ⟶ 323:
}
}
}</
And testing that it works as intended:
<syntaxhighlight lang="e">
? def p := makeCameraPhone(p)
> [p.takesPictures(), p.makesCalls(), p.internalMemory()]
# value: [true, true, 33619968]</
=={{header|Eiffel}}==
Having two class—one for CAMERA and the other for a MOBILE_PHONE ...
<syntaxhighlight lang="eiffel ">class
CAMERA
end</
<
MOBILE_PHONE
end</
=== Now Multiple Inherit ===
We can create a new CAMERA_PHONE, which inherits directly from both CAMERA and MOBILE_PHONE.
<syntaxhighlight lang="eiffel ">class
CAMERA_PHONE
inherit
CAMERA
MOBILE_PHONE
end</
NOTE: There is no reasonable limit to the number of classes we can inherit from in a single class. The compiler helps us to navigate issues like repeated inheritance and the "diamond of death" easily and quickly.
=={{header|Elena}}==
ELENA only permits inheritance from one parent class. However, mixins are supported
<syntaxhighlight lang
{
cameraMsg
= "camera"
}
class MobilePhone
{
mobileMsg
= "phone"
}
class CameraPhone
{
dispatch() => CameraFeature
}
public program()
{
var cp :=
console
console
}</syntaxhighlight>
Alternatively a group object may be created
<
class CameraFeature
{
cameraMsg
= "camera"
}
Line 356 ⟶ 388:
{
mobileMsg
= "phone"
}
{
new() =
}
public program()
{
var cp := CameraPhone
console
console
}</syntaxhighlight>
=={{header|F_Sharp|F#}}==
A class can only inherit from one other class, but it can implement any number of interfaces.
<syntaxhighlight lang="fsharp">type Picture = System.Drawing.Bitmap // (a type synonym)
// an interface type
type Camera =
abstract takePicture : unit -> Picture
// an interface that inherits multiple interfaces
type Camera2 =
inherits System.ComponentModel.INotifyPropertyChanged
inherits Camera
// a class with an abstract method with a default implementation
// (usually called a virtual method)
type MobilePhone() =
abstract makeCall : int[] -> unit
default x.makeCall(number) = () // empty impl
// a class that inherits from another class and implements an interface
type CameraPhone() =
inherit MobilePhone()
interface Camera with
member x.takePicture() = new Picture(10, 10)</syntaxhighlight>
=={{header|Factor}}==
<
TUPLE: mobile-phone ;
UNION: camera-phone camera mobile-phone ;</
=={{header|Fantom}}==
Line 385 ⟶ 443:
It is an error for method names to conflict.
<
class Camera
{
Line 416 ⟶ 474:
echo (cp.mobileMsg)
}
}</
=={{header|Forth}}==
Standard Forth does not supply high level data structures
or functions. But there is library code written by vendors
or users. For object programming with multiple inheritance
there is a user-supplied ANS compatible extension.
https://github.com/DouglasBHoffman/FMS2
Download the FMSMI package to run the
following example code.
<syntaxhighlight lang="forth">
\ define class camera with method say:
:class camera
:m say: ." camera " ;m
;class
\ define class phone with method say:
:class phone
:m say: ." phone " ;m
;class
\ define cameraPhone phone with method say:
\ class cameraPhone inherits from both class
\ camera and class phone
:class cameraPhone super{ camera phone }
:m say: self say: \ method conflicts in superclasses
\ are resolved by left-to-right order
\ so self say: will call the say: method
\ from class camera
super> phone say: \ super> phone is used to direct
\ this say: method to use the
\ method from class phone
;m
;class
cameraPhone cp \ instantiate a cameraPhone object named cp
cp say: \ send the say: message to cp
\ output:
camera phone
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' FB does not currently support multiple inheritance. Composition has to be used instead if one wants
Line 436 ⟶ 540:
cam As Camera ' using composition here
' other stuff
End Type</
=={{header|Go}}==
Go abandons traditional object oriented concepts of inheritance hierarchies, yet it does have features for composing both structs and interfaces.
<
package main
Line 496 ⟶ 574:
htc.sim = "XYZ"
fmt.Println(htc)
}</
{{out}} (Note sensor field still blank)
<pre>{{zoom } {XYZ 3.14}}</pre>
<
// Types implement interfaces simply by implementing functions.
// The type does not explicitly declare the interfaces it implements.
Line 545 ⟶ 623:
i.photo()
i.call()
}</
{{out}}
<pre>
Line 551 ⟶ 629:
omg!
</pre>
=={{header|Groovy}}==
Same inheritance rules as [[Inheritance/Multiple#Java|Java]].
=={{header|Haskell}}==
<
class MobilePhone a
class (Camera a, MobilePhone a) => CameraPhone a</
==Icon and {{header|Unicon}}==
Line 564 ⟶ 645:
This became one of the major addons contributing to Unicon.
<
# methods...
# initializer...
Line 577 ⟶ 658:
# methods...
# initialiser...
end</
=={{header|Io}}==
<
Camera click := method("Taking snapshot" println)
Line 591 ⟶ 672:
myPhone := CameraPhone clone
myPhone click // --> "Taking snapshot"
myPhone call // --> "Calling home"</
In Io each object has an internal list of prototype objects it inherits from. You can add to this list with <code>appendProto</code>.
=={{header|Ioke}}==
<
MobilePhone = Origin mimic
CameraPhone = Camera mimic mimic!(MobilePhone)</
=={{header|J}}==
<
create=: verb define
Line 630 ⟶ 712:
destroy=: codestroy
NB. additional camera-phone methods go here</
The adverb Fix (f.) is needed as shown so the superclass constructors
get executed in the object, not in the superclass.
Line 637 ⟶ 719:
Java does not allow multiple inheritance, but you can "implement" multiple interfaces. All methods in interfaces are abstract (they don't have an implementation).
When you implement an interface you need to implement the specified methods.
<
//functions here with no definition...
//ex:
//public void takePicture();
}</
<
//functions here with no definition...
//ex:
//public void makeCall();
}</
<
//functions here...
}</
{{omit|Julia}}
=={{header|Julia}}==
Julia supports inheritance via abstract types. In Julia, multiple dispatch allows objects of different types to have the same function interfaces. Julia also can support traits via parameters in type declarations or with macros. This makes multiple inheritance in Julia mostly unnecessary, except for the inconvenience of composing the data in a mixed type when declaring multiple similar types, for which there are macros.<br /> <br />For example, the functions <code> dialnumber(equipment, name) </code> and <code> video(equipment, filename) </code> could be used as generic interfaces to implement methods for a <code>Telephone</code>, a <code>Camera</code>, and a <code>SmartPhone</code>, and Julia would dispatch according to the type of the equipment.<syntaxhighlight lang="julia">
abstract type Phone end
struct DeskPhone <: Phone
book::Dict{String,String}
end
abstract type Camera end
struct kodak
roll::Vector{Array{Int32,2}}
end
struct CellPhone <: Phone
book::Dict{String,String}
roll::Vector{AbstractVector}
end
function dialnumber(phone::CellPhone)
println("beep beep")
end
function dialnumber(phone::Phone)
println("tat tat tat tat")
end
function snap(camera, img)
println("click")
push!(camera.roll, img)
end
dphone = DeskPhone(Dict(["information" => "411"]))
cphone = CellPhone(Dict(["emergency" => "911"]), [[]])
dialnumber(dphone)
dialnumber(cphone)
</syntaxhighlight>{{output}}<pre>
tat tat tat tat
beep beep
</pre>
=={{header|Kotlin}}==
Line 661 ⟶ 783:
to be abstract or to provide accessor implementations.
<
val numberOfLenses : Int
}
Line 688 ⟶ 810:
println(c2)
println(listOf(c2.javaClass.superclass) + c2.javaClass.interfaces)
}</
{{out}}
<pre>CameraPhone(numberOfLenses=1, battery_level=50)
Line 702 ⟶ 824:
and trays hand down the methods it has implemented provided that the type
fulfills the requirements for the trait. [http://lassoguide.com/language/traits.html http://lassoguide.com/language/traits.html]
<
require zoomfactor
Line 738 ⟶ 860:
#mydevice -> has_zoom
'<br />'
#mydevice -> is_smart</
-> false
true
=={{header|Latitude}}==
Latitude is a prototype-oriented language, and every object can have only one prototype. As such, multiple inheritance in the usual sense is impossible in Latitude. The behavior of multiple (implementation) inheritance can be approximated with mixins.
<syntaxhighlight lang="latitude">Camera ::= Mixin clone.
MobilePhone ::= Mixin clone.
CameraPhone ::= Object clone.
Camera inject: CameraPhone.
MobilePhone inject: CameraPhone.</syntaxhighlight>
In order to add functionality to either of the mixins, the <code>interface</code> slot of the mixin must be modified to include the name of the new method. Injecting a mixin makes a copy of the methods, as opposed to traditional (prototype) inheritance in which method calls are delegated.
=={{header|Lingo}}==
Lingo does not support multiple inheritance. But its slightly idiosyncratic inheritance implementation based on "ancestors" allows to assign/change inheritance relations at runtime. So a similar (but not identical) effect can be achieved by something like this:
<
property resolution
Line 755 ⟶ 890:
on snap (me)
put "SNAP!"
end</
<
property ringtone
Line 769 ⟶ 904:
put "RING!!!"
end repeat
end</
<
property ancestor
Line 785 ⟶ 920:
return me
end</
Usage:
<
cp.snap()
Line 802 ⟶ 937:
put cp.ringtone
-- "Bell"</
=={{header|Logtalk}}==
Line 808 ⟶ 943:
There is no "class" keyword in Logtalk;
an "object" keyword is used instead (Logtalk objects play the role of classes, meta-classes, instances, or prototypes depending on the relations with other objects).
<
...).
...
:- end_object.</
<
...).
...
:- end_object.</
<
specializes(camera, mobile_phone),
...).
...
:- end_object.</
=={{header|Lua}}==
Line 829 ⟶ 964:
by making it a closure.
<
return setmetatable(t,{__index = function(self, k)
--collisions are resolved in this implementation by simply taking the first one that comes along.
Line 841 ⟶ 976:
camera = {}
mobilephone = {}
cameraphone = setemetatables({},{camera,mobilephone})</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Class Camera {
cameratype$
Class:
module Camera (.cameratype$){
}
}
\\ INHERITANCE AT CODE LEVEL
Class MobilePhone {
Private:
model$
Class:
module MobilePhone (.model$) {
}
}
Class CameraPhone as Camera as MobilePhone {
Module CameraPhone ( .model$, .cameratype$) {
}
}
CP1 =CameraPhone("X-15", "OBSCURE")
Print
Print CP1 is type Camera = true
Print CP1 is type MobilePhone = true
\\ INHERITANCE AT OBJECT LEVEL
CP2 = MobilePhone("X-9") with Camera("WIDE")
\\ CP3 has no type
Group CP3 {
Module PrintAll {
If this is type Camera and this is type MobilePhone then
Print .model$, .cameratype$
Else
Print "Nothing to print"
End if
}
}
CP3.PrintAll ' Nothing to print
\\ using pointers and prepate inheritance at object level
CP->(CP1 with CP3)
CP=>PrintAll
CP->(CP2 with CP3)
CP=>PrintAll
}
CheckIt
</syntaxhighlight>
=={{header|Nemerle}}==
Like C#, Nemerle only allows pseudo-multiple inheritance through interfaces.
In Nemerle, the base class must be listed before any interfaces.
<
// ...
}
Line 882 ⟶ 1,040:
class CameraPhone: MobilePhone, ICamera {
// ...
}</
=={{header|NetRexx}}==
Line 891 ⟶ 1,049:
In this sample the class/interface names are augmented over those required in the task to prevent namespace pollution.
The sample also provides a complete working implementation to demonstrate the capability.
<
options replace format comments java crossref symbols binary
Line 936 ⟶ 1,094:
return shutter
method call() public
return ringTone</
{{out}}
<pre>
Line 943 ⟶ 1,101:
click...
ring...</pre>
=={{header|Nim}}==
nim does not support multiple inheritance (version<=1.4.6). It is just a demonstration of the procedure reloading nature of nim code.
<syntaxhighlight lang="nim">type
Camera = ref object of RootObj
MobilePhone = ref object of RootObj
CameraPhone = object
camera: Camera
phone: MobilePhone
proc `is`(cp: CameraPhone, t: typedesc): bool =
for field in cp.fields():
if field of t:
return true
var cp: CameraPhone
echo(cp is Camera)
echo(cp is MobilePhone)</syntaxhighlight>
{{out}}
<pre>
true
true</pre>
=={{header|Objective-C}}==
Line 953 ⟶ 1,131:
of multiple classes, you can use message forwarding to mimic the functionality of those classes without actually inheriting them, as described in [http://support.apple.com/kb/TA45894 this guide]:
<
}
@end
Line 1,005 ⟶ 1,183:
}
@end</
Caveat: the CameraPhone class will still technically not inherit from
Line 1,011 ⟶ 1,189:
=={{header|OCaml}}==
<
object (self)
(*functions go here...*)
end</
<
object (self)
(*functions go here...*)
end</
<
object (self)
inherit camera
inherit mobile_phone
(*functions go here...*)
end</
=={{header|Oforth}}==
Line 1,036 ⟶ 1,213:
If Camera and MobilePhone are designed as properties, we can write :
<
Property new: MobilePhone
Object Class new: CameraPhone
CameraPhone is: Camera
CameraPhone is: MobilePhone</
=={{header|ooRexx}}==
Line 1,047 ⟶ 1,224:
Mixins are more than just interfaces.
They can contain concrete method implementations and also create instance variables (scoped as private variables to the mixin methods).
<syntaxhighlight lang="oorexx">
-- inherited classes must be created as mixinclasses.
::class phone mixinclass object
Line 1,061 ⟶ 1,238:
-- or
::class cameraphone2 subclass camera inherit phone</
=={{header|OxygenBasic}}==
<
string cbuf
method TakePhoto()
Line 1,087 ⟶ 1,264:
cp.ViewPhoto
cp.MakeCall</
=={{header|Oz}}==
<
class MobilePhone end
class CameraPhone from Camera MobilePhone end</
=={{header|Pascal}}==
See [[Inheritance/Multiple#Delphi | Delphi]]
=={{header|PascalABC.NET}}==
In PascalABC.NET you may inherit from only one class, but you can inherit from multiple interfaces.
<syntaxhighlight lang="delphi">
type
IMy1 = interface
procedure My1;
end;
IMy2 = interface
procedure My2;
end;
MyClass = class(IMy1,IMy2)
public
procedure My1 := Writeln('My1');
procedure My2 := Writeln('My2');
end;
MyClassD = class(MyClass,IMy1,IMy2)
end;
begin
var my := new MyClassD;
my.My1; my.My2;
end.
</syntaxhighlight>
=={{header|Perl}}==
<
#functions go here...
1;</
<
#functions go here...
1;</
<
use Camera;
use MobilePhone;
@ISA = qw( Camera MobilePhone );
#functions go here...
1;</
or
<
use base qw/Camera MobilePhone/;
#functions go here...</
The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] extention:
<
class Camera {
Line 1,132 ⟶ 1,334:
class CameraPhone extends(Camera, MobilePhone) {
# methods ...
}</
=={{header|
{{libheader|Phix/Class}}
Needs 0.8.1+
===inheritance===
The programmer is expected to assume complete responsibility (away from the compiler) for checking/resolving any conflicts.
<syntaxhighlight lang="phix">
class Camra
string name = "nikkon"
end class
class Mobile
-- string name = "nokia" -- oops!
string mane = "nokia" -- ok!
end class
class CamraPhone extends Camra,Mobile
procedure show() ?{name,mane} end procedure
end class
CamraPhone cp = new()
cp.show()</syntaxhighlight>
{{out}}
<pre>
{"nikkon","nokia"}
</pre>
===composition===
The programmer is expected to assume complete responsibility for invoking new() appropriately.<br>
The example below shows four different approaches to invoking all the new() that are needed (one pair commented out).<br>
Note that invoking new() inside a class definition creates shared references to a single instance.<br>
The compiler demands to be explicitly told what the inner/inlined new() on cp2 are actually for.
<syntaxhighlight lang="phix">class Camera
public string name = "nikkon"
end class
class MobilePhone
public string name = "nokia" -- (clash no more)
end class
class CameraPhone
-- Camera c = new()
-- MobilePhone m = new()
public Camera c
public MobilePhone m
procedure show() ?{c.name,m.name} end procedure
end class
Camera c = new({"canon"})
MobilePhone m = new()
CameraPhone cp1 = new({c,m}),
cp2 = new({new("Camera"),new("MobilePhone")}),
cp3 = new() -- (internal/shared/NULL c,m)
cp3.c = new() -- (obviously c must be public)
cp3.m = new({"LG20"}) -- "" m "" ""
cp1.show()
cp2.show()
cp3.show() -- crashes without internal/above new()</syntaxhighlight>
{{out}}
<pre>
{"canon","nokia"}
{"nikkon","nokia"}
{"nikkon","LG20"}
</pre>
=={{header|PicoLisp}}==
<
(class +MobilePhone)
(class +CameraPhone +MobilePhone +Camera)
</syntaxhighlight>
=={{header|Pop11}}==
<
lib objectclass;
Line 1,172 ⟶ 1,415:
enddefine;
;;; methods go here</
=={{header|PowerShell}}==
{{works with|PowerShell|5}}
<syntaxhighlight lang="powershell">
class Camera {}
class MobilePhone {}
class CameraPhone : Camera, MobilePhone {}
</syntaxhighlight>
=={{header|PureBasic}}==
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
<
EndClass
Line 1,192 ⟶ 1,434:
Class CameraMobile Extends Camera Extends Mobil
EndClass</
=={{header|Python}}==
<
pass #functions go here...</
<
pass #functions go here...</
<
pass #functions go here...</
=={{header|Racket}}==
Line 1,209 ⟶ 1,451:
Mixins can be used to achieve some of the benefits of multiple inheritance.
<
(define camera<%> (interface ()))
Line 1,218 ⟶ 1,460:
(super-new)
;; implement methods here
))</
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2012.06}}
<syntaxhighlight lang="raku" line>class Camera {}
class MobilePhone {}
class CameraPhone is Camera is MobilePhone {}
say CameraPhone.^mro; # undefined type object
say CameraPhone.new.^mro; # instantiated object</syntaxhighlight>
{{out}}
<pre>CameraPhone() Camera() MobilePhone() Any() Mu()
CameraPhone() Camera() MobilePhone() Any() Mu()</pre>
The <tt>.^mro</tt> is not an ordinary method call,
but a call to the object's metaobject
that returns the method resolution order for this type.
=={{header|Ring}}==
<
# Project : Inheritance/Multiple
Line 1,256 ⟶ 1,516:
# Add MobilePhone Attributes
AddParentClassAttributes(self,:MobilePhone)
</syntaxhighlight>
Output:
<pre>
Line 1,267 ⟶ 1,527:
=={{header|Ruby}}==
Ruby does not have multiple inheritance, but you can mix modules into classes:
<
# define methods here
end
Line 1,276 ⟶ 1,536:
include Camera
# define methods here
end</
=={{header|Rust}}==
<syntaxhighlight lang="rust">trait Camera {}
trait MobilePhone {}
trait CameraPhone: Camera + MobilePhone {}</syntaxhighlight>
=={{header|Scala}}==
<
trait MobilePhone
class CameraPhone extends Camera with MobilePhone</
=={{header|Self}}==
Self is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This is an example of the relevant excerpts from a Self transporter fileout. Normally the object tree would be built and navigated within the graphical Self environment.
<
<
<
=={{header|Sidef}}==
<
class MobilePhone {};
class CameraPhone << Camera, MobilePhone {};</
=={{header|Slate}}==
<
define: #MobilePhone.
define: #CameraPhone &parents: {Camera. MobilePhone}.</
=={{header|Swift}}==
Like Objective-C, Swift does not allow multiple inheritance. However, you can conform to multiple protocols.
<
}
Line 1,311 ⟶ 1,577:
class CameraPhone: Camera, Phone {
}</
=={{header|Tcl}}==
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<
oo::class create Camera
Line 1,321 ⟶ 1,587:
oo::class create CameraPhone {
superclass Camera MobilePhone
}</
=={{header|Wren}}==
Wren does not support either multiple inheritance or interfaces.
However, multiple inheritance can be simulated by inheriting from a single class and then embedding objects of other classes and wrapping their methods.
<syntaxhighlight lang="wren">class Camera {
construct new() {}
snap() { System.print("taking a photo") }
}
class Phone {
construct new() {}
call() { System.print("calling home") }
}
class CameraPhone is Camera {
construct new(phone) { _phone = phone } // uses composition for the Phone part
// inherits Camera's snap() method
// Phone's call() method can be wrapped
call() { _phone.call() }
}
var p = Phone.new()
var cp = CameraPhone.new(p)
cp.snap()
cp.call()</syntaxhighlight>
{{out}}
<pre>
taking a photo
calling home
</pre>
=={{header|zkl}}==
<
class CameraPhone(Camera,MobilePhone){}
CameraPhone.linearizeParents</
{{out}}Show the class search order
<pre>L(Class(CameraPhone),Class(Camera),Class(MobilePhone))</pre>
{{omit from|6502 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|8080 Assembly}}
{{omit from|8086 Assembly}}
{{omit from|ARM Assembly}}
{{omit from|AWK}}
{{omit from|Axe}}
{{omit from|Batch File|Not an OO language.}}
{{omit from|JavaScript|https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Property_Inheritance_Revisited/No_Multiple_Inheritance}}
{{omit from|Metafont}}
Line 1,341 ⟶ 1,643:
{{omit from|ML/I}}
{{omit from|PARI/GP}}
{{omit from|Retro}}
{{omit from|TI-83 BASIC|Does not have user-defined data structures or objects.}}
{{omit from|TI-89 BASIC|Does not have user-defined data structures or objects.}}
{{omit from|
|