Inheritance/Multiple: Difference between revisions
No edit summary |
(Added J solution.) |
||
Line 35: | Line 35: | ||
}; |
}; |
||
</cpp> |
</cpp> |
||
=={{header|J}}== |
|||
coclass 'camera' |
|||
create=: verb define |
|||
NB. creation-specifics for a camera go here |
|||
) |
|||
destroy=: codestroy |
|||
NB. additional camera methods go here |
|||
coclass 'phone' |
|||
create=: verb define |
|||
NB. creation-specifics for a phone go here |
|||
) |
|||
destroy=: codestroy |
|||
NB. additional phone methods go here |
|||
coclass 'cameraphone' |
|||
create=: verb define |
|||
asCamera=: <nowiki>''</nowiki> conew 'camera' |
|||
asPhone=: <nowiki>''</nowiki> conew 'phone' |
|||
) |
|||
destroy=: verb define |
|||
destroy__asCamera <nowiki>''</nowiki> |
|||
destroy__asPhone <nowiki>''</nowiki> |
|||
codestroy <nowiki>''</nowiki> |
|||
) |
|||
NB. additional cameraphone methods go here |
|||
=={{header|Java}}== |
=={{header|Java}}== |
Revision as of 18:07, 19 May 2008
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.
Ada
Ada 2005 has added interfaces, allowing a limited form of multiple inheritance. <ada> 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; </ada>
C++
<cpp> class Camera {
// ...
};
class MobilePhone {
// ...
};
class CameraPhone:
public Camera, public MobilePhone
{
// ...
}; </cpp>
J
coclass 'camera' create=: verb define NB. creation-specifics for a camera go here ) destroy=: codestroy NB. additional camera methods go here
coclass 'phone' create=: verb define NB. creation-specifics for a phone go here ) destroy=: codestroy NB. additional phone methods go here
coclass 'cameraphone' create=: verb define asCamera=: '' conew 'camera' asPhone=: '' conew 'phone' ) destroy=: verb define destroy__asCamera '' destroy__asPhone '' codestroy '' ) NB. additional cameraphone methods go here
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. <java>public interface Camera{
//functions here with no definition... //ex: //public void takePicture();
}</java> <java>public interface MobilePhone{
//functions here with no definition... //ex: //public void makeCall();
}</java> <java>public class CameraPhone implements Camera, MobilePhone{
//functions here...
}</java>
OCaml
<ocaml>class camera =
object (self) (*functions go here...*) end</ocaml>
<ocaml>class mobile_phone =
object (self) (*functions go here...*) end</ocaml>
<ocaml>class camera_phone =
object (self) inherit camera inherit mobile_phone (*functions go here...*) end</ocaml>
Perl
<perl>package Camera;
- functions go here...
1;</perl>
<perl>package MobilePhone;
- functions go here...
1;</perl>
<perl>package CameraPhone; use Camera; use MobilePhone; @ISA = qw( Camera MobilePhone );
- functions go here...
1;</perl>
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
Python
<python>class Camera:
pass #functions go here...</python>
<python>class MobilePhone:
pass #functions go here...</python>
<python>class CameraPhone(Camera, MobilePhone):
pass #functions go here...</python>