Inheritance/Multiple

From Rosetta Code
Task
Inheritance/Multiple
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>

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;

  1. functions go here...

1;</perl>

<perl>package MobilePhone;

  1. functions go here...

1;</perl>

<perl>package CameraPhone; use Camera; use MobilePhone; @ISA = qw( Camera MobilePhone );

  1. 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>