Inheritance/Multiple: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
Puppydrum64 (talk | contribs) mNo edit summary |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 16:
=={{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 32:
class Camera_Phone implements Camera, Mobile_Phone {
}</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
DIM Camera{TakePicture}
Line 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;
Line 68:
Phone phoneSample;
}CameraPhone;
</syntaxhighlight>
=={{header|C sharp|C#}}==
Line 77:
In the example we inherit from a class and an interface.
<
// ...
}
Line 87:
class CameraPhone: ICamera, MobilePhone {
// ...
}</
=={{header|C++}}==
<
{
// ...
Line 105:
{
// ...
};</
=={{header|Clojure}}==
<
(defprotocol MobilePhone)
Line 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 Camera, Mobile-Phone. ▼
CLASS-ID. Camera-Phone
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
Line 133 ⟶ 137:
*> ...
END CLASS Camera-Phone.</
=={{header|Common Lisp}}==
<
(defclass mobile-phone () ())
(defclass camera-phone (camera mobile-phone) ())</
=={{header|D}}==
Line 145 ⟶ 149:
While D does not have multiple base class inheritance, you can inherit from multiple interfaces.
<
// member function prototypes and static methods
}
Line 156 ⟶ 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 171 ⟶ 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 212 ⟶ 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 218 ⟶ 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 236 ⟶ 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 243 ⟶ 247:
Delphi doesn't support multiple inheritance, but it does have multiple interfaces.
<syntaxhighlight lang="delphi">type
ICamera = Interface
// ICamera methods...
Line 254 ⟶ 258:
TCameraPhone = class(TInterfacedObject, ICamera, IMobilePhone)
// ICamera and IMobilePhone methods...
end;</
=={{header|DWScript}}==
Line 271 ⟶ 275:
These shortcomings could be fixed if more powerful multiple inheritance were needed.
<
def forwarder match [verb, args] {
escape __return {
Line 293 ⟶ 297:
}
return forwarder
}</
The task example:
<
return def camera extends minherit(self, []) {
to takesPictures() { return true }
Line 319 ⟶ 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 ...
<
CAMERA
end</
<
MOBILE_PHONE
end</
=== Now Multiple Inherit ===
We can create a new CAMERA_PHONE, which inherits directly from both CAMERA and MOBILE_PHONE.
<
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
<
{
cameraMsg
Line 371 ⟶ 375:
console.writeLine(cp.cameraMsg);
console.writeLine(cp.mobileMsg)
}</
Alternatively a group object may be created
<
class CameraFeature
Line 398 ⟶ 402:
console.writeLine(cp.cameraMsg);
console.writeLine(cp.mobileMsg)
}</
=={{header|F_Sharp|F#}}==
A class can only inherit from one other class, but it can implement any number of interfaces.
<
// an interface type
Line 424 ⟶ 428:
inherit MobilePhone()
interface Camera with
member x.takePicture() = new Picture(10, 10)</
=={{header|Factor}}==
<
TUPLE: mobile-phone ;
UNION: camera-phone camera mobile-phone ;</
=={{header|Fantom}}==
Line 439 ⟶ 443:
It is an error for method names to conflict.
<
class Camera
{
Line 470 ⟶ 474:
echo (cp.mobileMsg)
}
}</
Line 484 ⟶ 488:
following example code.
<
\ define class camera with method say:
Line 516 ⟶ 520:
\ output:
camera phone
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' FB does not currently support multiple inheritance. Composition has to be used instead if one wants
Line 536 ⟶ 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 570 ⟶ 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 619 ⟶ 623:
i.photo()
i.call()
}</
{{out}}
<pre>
Line 630 ⟶ 634:
=={{header|Haskell}}==
<
class MobilePhone a
class (Camera a, MobilePhone a) => CameraPhone a</
==Icon and {{header|Unicon}}==
Line 641 ⟶ 645:
This became one of the major addons contributing to Unicon.
<
# methods...
# initializer...
Line 654 ⟶ 658:
# methods...
# initialiser...
end</
=={{header|Io}}==
<
Camera click := method("Taking snapshot" println)
Line 668 ⟶ 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 708 ⟶ 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 715 ⟶ 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}}
Line 733 ⟶ 737:
=={{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.<
abstract type Phone end
Line 769 ⟶ 773:
dialnumber(dphone)
dialnumber(cphone)
</
tat tat tat tat
beep beep
Line 779 ⟶ 783:
to be abstract or to provide accessor implementations.
<
val numberOfLenses : Int
}
Line 806 ⟶ 810:
println(c2)
println(listOf(c2.javaClass.superclass) + c2.javaClass.interfaces)
}</
{{out}}
<pre>CameraPhone(numberOfLenses=1, battery_level=50)
Line 820 ⟶ 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 856 ⟶ 860:
#mydevice -> has_zoom
'<br />'
#mydevice -> is_smart</
-> false
Line 865 ⟶ 869:
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.
<
MobilePhone ::= Mixin clone.
CameraPhone ::= Object clone.
Camera inject: CameraPhone.
MobilePhone inject: CameraPhone.</
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.
Line 876 ⟶ 880:
=={{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 886 ⟶ 890:
on snap (me)
put "SNAP!"
end</
<
property ringtone
Line 900 ⟶ 904:
put "RING!!!"
end repeat
end</
<
property ancestor
Line 916 ⟶ 920:
return me
end</
Usage:
<
cp.snap()
Line 933 ⟶ 937:
put cp.ringtone
-- "Bell"</
=={{header|Logtalk}}==
Line 939 ⟶ 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 960 ⟶ 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 972 ⟶ 976:
camera = {}
mobilephone = {}
cameraphone = setemetatables({},{camera,mobilephone})</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Class Camera {
Line 1,021 ⟶ 1,025:
}
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 1,036 ⟶ 1,040:
class CameraPhone: MobilePhone, ICamera {
// ...
}</
=={{header|NetRexx}}==
Line 1,045 ⟶ 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 1,090 ⟶ 1,094:
return shutter
method call() public
return ringTone</
{{out}}
<pre>
Line 1,100 ⟶ 1,104:
=={{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.
<
Camera = ref object of RootObj
MobilePhone = ref object of RootObj
Line 1,112 ⟶ 1,116:
var cp: CameraPhone
echo(cp is Camera)
echo(cp is MobilePhone)</
{{out}}
<pre>
Line 1,127 ⟶ 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,179 ⟶ 1,183:
}
@end</
Caveat: the CameraPhone class will still technically not inherit from
Line 1,185 ⟶ 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,209 ⟶ 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,220 ⟶ 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,234 ⟶ 1,238:
-- or
::class cameraphone2 subclass camera inherit phone</
=={{header|OxygenBasic}}==
<
string cbuf
method TakePhoto()
Line 1,260 ⟶ 1,264:
cp.ViewPhoto
cp.MakeCall</
=={{header|Oz}}==
<
class MobilePhone end
class CameraPhone from Camera MobilePhone end</
=={{header|Pascal}}==
Line 1,273 ⟶ 1,277:
=={{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,305 ⟶ 1,309:
class CameraPhone extends(Camera, MobilePhone) {
# methods ...
}</
=={{header|Phix}}==
Line 1,312 ⟶ 1,316:
===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"
Line 1,324 ⟶ 1,328:
end class
CamraPhone cp = new()
cp.show()</
{{out}}
<pre>
Line 1,334 ⟶ 1,338:
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.
<
public string name = "nikkon"
end class
Line 1,356 ⟶ 1,360:
cp1.show()
cp2.show()
cp3.show() -- crashes without internal/above new()</
{{out}}
<pre>
Line 1,365 ⟶ 1,369:
=={{header|PicoLisp}}==
<
(class +MobilePhone)
(class +CameraPhone +MobilePhone +Camera)
</syntaxhighlight>
=={{header|Pop11}}==
<
lib objectclass;
Line 1,386 ⟶ 1,390:
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,405 ⟶ 1,409:
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,422 ⟶ 1,426:
Mixins can be used to achieve some of the benefits of multiple inheritance.
<
(define camera<%> (interface ()))
Line 1,431 ⟶ 1,435:
(super-new)
;; implement methods here
))</
=={{header|Raku}}==
Line 1,437 ⟶ 1,441:
{{works with|Rakudo|2012.06}}
<syntaxhighlight lang="raku"
class MobilePhone {}
class CameraPhone is Camera is MobilePhone {}
say CameraPhone.^mro; # undefined type object
say CameraPhone.new.^mro; # instantiated object</
{{out}}
<pre>CameraPhone() Camera() MobilePhone() Any() Mu()
Line 1,452 ⟶ 1,456:
=={{header|Ring}}==
<
# Project : Inheritance/Multiple
Line 1,487 ⟶ 1,491:
# Add MobilePhone Attributes
AddParentClassAttributes(self,:MobilePhone)
</syntaxhighlight>
Output:
<pre>
Line 1,498 ⟶ 1,502:
=={{header|Ruby}}==
Ruby does not have multiple inheritance, but you can mix modules into classes:
<
# define methods here
end
Line 1,507 ⟶ 1,511:
include Camera
# define methods here
end</
=={{header|Rust}}==
<
trait MobilePhone {}
trait CameraPhone: Camera + MobilePhone {}</
=={{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,548 ⟶ 1,552:
class CameraPhone: Camera, Phone {
}</
=={{header|Tcl}}==
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<
oo::class create Camera
Line 1,558 ⟶ 1,562:
oo::class create CameraPhone {
superclass Camera MobilePhone
}</
=={{header|Wren}}==
Line 1,564 ⟶ 1,568:
However, multiple inheritance can be simulated by inheriting from a single class and then embedding objects of other classes and wrapping their methods.
<
construct new() {}
snap() { System.print("taking a photo") }
Line 1,584 ⟶ 1,588:
var cp = CameraPhone.new(p)
cp.snap()
cp.call()</
{{out}}
Line 1,593 ⟶ 1,597:
=={{header|zkl}}==
<
class CameraPhone(Camera,MobilePhone){}
CameraPhone.linearizeParents</
{{out}}Show the class search order
<pre>L(Class(CameraPhone),Class(Camera),Class(MobilePhone))</pre>
|