Multiple inheritance

From Rosetta Code

Jump to: navigation, search

Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.

Code examples should be formatted along the lines of one of the existing prototypes.

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] C++

 
class Camera
{
  // ...
};
 
class MobilePhone
{
  // ...
};
 
class CameraPhone:
  public Camera,
  public 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'

create=: verb define
asCamera=: '' conew 'Camera'
asPhone=:  '' conew 'MobilePhone'
)

destroy=: verb define
destroy__asCamera ''
destroy__asPhone ''
codestroy ''
)

NB. additional camera-phone methods go here

[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] 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] 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;

[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...
Personal tools