Inheritance/Multiple
From Rosetta Code
You are encouraged to solve this task according to the task description, using any language you may know.
Multiple inheritance allows to specify that one 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.
Write two classes (or interfaces) Camera and MobilePhone, then write a class CameraPhone which is both a Camera and a MobilePhone.
There is no need to implement any functions for those classes.
Contents |
[edit] Ada
Ada 2005 has added interfaces, allowing a limited form of multiple inheritance.
package Multiple_Interfaces is
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;
[edit] Aikido
Aikido does not support multiple inheritance, but does allow multiple implementation of interfaces.
interface Camera {
}
interface Mobile_Phone {
}
class Camera_Phone implements Camera, Mobile_Phone {
}
[edit] C++
class Camera
{
// ...
};
class MobilePhone
{
// ...
};
class CameraPhone:
public Camera,
public MobilePhone
{
// ...
};
[edit] C#
In C# you may inherit from only one class, but you can inherit from multiple interfaces. Also, in C# it is standard practice to start all interface names with a capital 'I' so I have altered the name of the interface. In the example we inherit from a class and an interface.
interface ICamera {
// ...
}
class MobilePhone {
// ...
}
class CameraPhone: ICamera, MobilePhone {
// ...
}
[edit] Common Lisp
(defclass camera () ())
(defclass mobile-phone () ())
(defclass camera-phone (camera mobile-phone) ())
[edit] D
D doesn't allow multiple inheritance, but you can inherit after multiple interfaces.
interface Camera {
// member function prototypes and static methods
}
interface MobilePhone {
// member function prototypes and static methods
}
class CameraPhone: Camera, MobilePhone {
// member function implementations for Camera,
// MobilePhone, and CameraPhone
}
[edit] E
E does not have multiple inheritance as a built-in feature. In fact, E only has inheritance at all as a light syntactic sugar over delegation (message forwarding). However, using that facility it is possible to implement multiple inheritance.
This is a quick simple implementation of multiple inheritance. It simply searches (depth-first and inefficiently) the inheritance tree for a method; it does not do anything about diamond inheritance. These shortcomings could be fixed if more powerful multiple inheritance were needed.
def minherit(self, supers) {
def forwarder match [verb, args] {
escape __return {
if (verb == "__respondsTo") {
def [verb, arity] := args
for super ? (super.__respondsTo(verb, arity)) in supers {
return true
}
return false
} else if (verb == "__getAllegedType") {
# XXX not a complete implementation
return supers[0].__getAllegedType()
} else {
def arity := args.size()
for super ? (super.__respondsTo(verb, arity)) in supers {
return E.call(super, verb, args)
}
throw(`No parent of $self responds to $verb/$arity`)
}
}
}
return forwarder
}
The task example:
def makeCamera(self) {
return def camera extends minherit(self, []) {
to takesPictures() { return true }
}
}
def makeMobilePhone(self) {
return def mobilePhone extends minherit(self, []) {
to makesCalls() { return true }
to internalMemory() { return 64*1024 }
}
}
def makeCameraPhone(self) {
return def cameraPhone extends minherit(self, [
makeCamera(self),
makeMobilePhone(self),
]) {
to internalMemory() {
return super.internalMemory() + 32 * 1024**2
}
}
}
And testing that it works as intended:
? def p := makeCameraPhone(p)
> [p.takesPictures(), p.makesCalls(), p.internalMemory()]
# value: [true, true, 33619968]
[edit] Eiffel
class
CAMERA
end
class
MOBILE_PHONE
end
class
CAMERA_PHONE
inherit
CAMERA
MOBILE_PHONE
end
[edit] Haskell
class Camera a
class MobilePhone a
class (Camera a, MobilePhone a) => CameraPhone a
[edit] Icon and Unicon
[edit] Icon
Icon does not support classes or inheritance. An intermediate language called Idol was developed as proof of concept for extending Icon. This became one of the major addons contributing to Unicon.
[edit] Unicon
class Camera (instanceVars)
# methods...
# initializer...
end
class Phone (instanceVars)
# methods...
# initializer...
end
class CameraPhone : Camera, Phone (instanceVars)
# methods...
# initialiser...
end
[edit] Ioke
Camera = Origin mimic
MobilePhone = Origin mimic
CameraPhone = Camera mimic mimic!(MobilePhone)
[edit] J
coclass 'Camera'
create=: verb define
NB. creation-specifics for a camera go here
)
destroy=: codestroy
NB. additional camera methods go here
coclass 'MobilePhone'
create=: verb define
NB. creation-specifics for a mobile phone go here
)
destroy=: codestroy
NB. additional phone methods go here
coclass 'CameraPhone'
coinsert 'Camera MobilePhone'
create=: verb define
create_Camera_ f. y
create_MobilePhone_ f. y
NB. creation details specific to a camera phone go here
)
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.
[edit] Java
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.
public interface Camera{
//functions here with no definition...
//ex:
//public void takePicture();
}
public interface MobilePhone{
//functions here with no definition...
//ex:
//public void makeCall();
}
public class CameraPhone implements Camera, MobilePhone{
//functions here...
}
[edit] Logtalk
Logtalk supports multiple inheritance. 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).
:- object(camera,
...).
...
:- end_object.
:- object(mobile_phone,
...).
...
:- end_object.
:- object(camera_phone,
specializes(camera, mobile_phone),
...).
...
:- end_object.
[edit] OCaml
class camera =
object (self)
(*functions go here...*)
end
class mobile_phone =
object (self)
(*functions go here...*)
end
class camera_phone =
object (self)
inherit camera
inherit mobile_phone
(*functions go here...*)
end
[edit] Oz
class Camera end
class MobilePhone end
class CameraPhone from Camera MobilePhone end
[edit] Perl
package Camera;
#functions go here...
1;
package MobilePhone;
#functions go here...
1;
package CameraPhone;
use Camera;
use MobilePhone;
@ISA = qw( Camera MobilePhone );
#functions go here...
1;
or
package CameraPhone;
use base qw/Camera MobilePhone/;
#functions go here...
The same using the MooseX::Declare extention:
use MooseX::Declare;
class Camera {
# methods ...
}
class MobilePhone {
# methods ...
}
class CameraPhone extends(Camera, MobilePhone) {
# methods ...
}
[edit] PicoLisp
(class +Camera)
(class +MobilePhone)
(class +CameraPhone +Camera +MobilePhone)
[edit] Pop11
;;; load object support
lib objectclass;
define :class Camera;
;;; slots go here
enddefine;
define :class MobilePhone;
;;; slots go here
enddefine;
define :class CameraPhone is Camera, MobilePhone;
;;; extra slots go here
enddefine;
;;; methods go here
[edit] Python
class Camera:
pass #functions go here...
class MobilePhone:
pass #functions go here...
class CameraPhone(Camera, MobilePhone):
pass #functions go here...
[edit] Ruby
Ruby does not have multiple inheritance, but you can mix modules into classes:
module Camera
# define methods here
end
class MobilePhone
# define methods here
end
class CameraPhone < MobilePhone
include Camera
# define methods here
end
[edit] Slate
define: #Camera.
define: #MobilePhone.
define: #CameraPhone &parents: {Camera. MobilePhone}.
[edit] Tcl
Works with: Tcl version 8.6 or Library: TclOO
package require TclOO
oo::class create Camera
oo::class create MobilePhone
oo::class create CameraPhone {
superclass Camera MobilePhone
}

