Inheritance/Multiple: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 37: Line 37:
};
};
</lang>
</lang>

=={{header|Common Lisp}}==

<lang common-lisp>(defclass camera () ())
(defclass mobile-phone () ())
(defclass camera-phone (camera mobile-phone) ())</lang>


=={{header|J}}==
=={{header|J}}==

Revision as of 10:12, 10 March 2009

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. <lang 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; </lang>

C++

<lang cpp> class Camera {

 // ...

};

class MobilePhone {

 // ...

};

class CameraPhone:

 public Camera,
 public MobilePhone

{

 // ...

}; </lang>

Common Lisp

<lang common-lisp>(defclass camera () ()) (defclass mobile-phone () ()) (defclass camera-phone (camera mobile-phone) ())</lang>

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=: verb define
  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.

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. <lang java>public interface Camera{

  //functions here with no definition...
  //ex:
  //public void takePicture();

}</lang> <lang java>public interface MobilePhone{

  //functions here with no definition...
  //ex:
  //public void makeCall();

}</lang> <lang java>public class CameraPhone implements Camera, MobilePhone{

  //functions here...

}</lang>

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.

OCaml

<lang ocaml>class camera =

 object (self)
   (*functions go here...*)
 end</lang>

<lang ocaml>class mobile_phone =

 object (self)
   (*functions go here...*)
 end</lang>

<lang ocaml>class camera_phone =

 object (self)
   inherit camera
   inherit mobile_phone
   (*functions go here...*)
 end</lang>

Perl

<lang perl>package Camera;

  1. functions go here...

1;</lang>

<lang perl>package MobilePhone;

  1. functions go here...

1;</lang>

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

  1. functions go here...

1;</lang>

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

<lang python>class Camera:

 pass #functions go here...</lang>

<lang python>class MobilePhone:

 pass #functions go here...</lang>

<lang python>class CameraPhone(Camera, MobilePhone):

 pass #functions go here...</lang>