Inheritance/Multiple: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|J}}: Added Fix (with note) as corrected by Raul Miller.)
No edit summary
Line 4: Line 4:
Multiple inheritance allows to specify that one [[classes | 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.
Multiple inheritance allows to specify that one [[classes | 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) <code>Camera</code> and <code>MobilePhone</code>, then write a class <code>CameraPhone</code> which is both a <code>Camera</code> and a <code>MobilePhone</code>.
Write two classes (or interfaces) <tt>Camera</tt> and <tt>MobilePhone</tt>, then write a class <tt>CameraPhone</tt> which is both a <tt>Camera</tt> and a <tt>MobilePhone</tt>.


There is no need to implement any functions for those classes.
There is no need to implement any functions for those classes.
Line 10: Line 10:
=={{header|Ada}}==
=={{header|Ada}}==
Ada 2005 has added interfaces, allowing a limited form of multiple inheritance.
Ada 2005 has added interfaces, allowing a limited form of multiple inheritance.
<ada>
<lang ada>
package Multiple_Interfaces is
package Multiple_Interfaces is
type Camera is tagged null record;
type Camera is tagged null record;
Line 16: Line 16:
type Camera_Phone is new Camera and Mobile_Phone with null record;
type Camera_Phone is new Camera and Mobile_Phone with null record;
end Multiple_Interfaces;
end Multiple_Interfaces;
</ada>
</lang>


=={{header|C++}}==
=={{header|C++}}==
<cpp>
<lang cpp>
class Camera
class Camera
{
{
Line 36: Line 36:
// ...
// ...
};
};
</cpp>
</lang>


=={{header|J}}==
=={{header|J}}==
Line 77: Line 77:
=={{header|Java}}==
=={{header|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 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{
<lang java>public interface Camera{
//functions here with no definition...
//functions here with no definition...
//ex:
//ex:
//public void takePicture();
//public void takePicture();
}</java>
}</lang>
<java>public interface MobilePhone{
<lang java>public interface MobilePhone{
//functions here with no definition...
//functions here with no definition...
//ex:
//ex:
//public void makeCall();
//public void makeCall();
}</java>
}</lang>
<java>public class CameraPhone implements Camera, MobilePhone{
<lang java>public class CameraPhone implements Camera, MobilePhone{
//functions here...
//functions here...
}</java>
}</lang>


=={{header|Logtalk}}==
=={{header|Logtalk}}==
Line 116: Line 116:


=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>class camera =
<lang ocaml>class camera =
object (self)
object (self)
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class mobile_phone =
<lang ocaml>class mobile_phone =
object (self)
object (self)
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class camera_phone =
<lang ocaml>class camera_phone =
object (self)
object (self)
inherit camera
inherit camera
inherit mobile_phone
inherit mobile_phone
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<perl>package Camera;
<lang perl>package Camera;
#functions go here...
#functions go here...
1;</perl>
1;</lang>


<perl>package MobilePhone;
<lang perl>package MobilePhone;
#functions go here...
#functions go here...
1;</perl>
1;</lang>


<perl>package CameraPhone;
<lang perl>package CameraPhone;
use Camera;
use Camera;
use MobilePhone;
use MobilePhone;
@ISA = qw( Camera MobilePhone );
@ISA = qw( Camera MobilePhone );
#functions go here...
#functions go here...
1;</perl>
1;</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 168: Line 168:


=={{header|Python}}==
=={{header|Python}}==
<python>class Camera:
<lang python>class Camera:
pass #functions go here...</python>
pass #functions go here...</lang>


<python>class MobilePhone:
<lang python>class MobilePhone:
pass #functions go here...</python>
pass #functions go here...</lang>


<python>class CameraPhone(Camera, MobilePhone):
<lang python>class CameraPhone(Camera, MobilePhone):
pass #functions go here...</python>
pass #functions go here...</lang>

Revision as of 15:41, 3 February 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>

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>