Inheritance/Single: Difference between revisions
No edit summary |
PascalABC.NET |
||
(227 intermediate revisions by more than 100 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Basic language learning}} |
|||
{{task|Basic language learning}}[[Category:Object oriented]]Show a tree of [[classes]] which inherit from each other. The top of the tree should be a class called Animal. The second level should have Dog and Cat. Under Dog should be Lab and Collie. None of the classes need to have any functions, the only thing they need to do is inherit from the specified superclasses (overriding functions should be shown in [[Polymorphism]]). The tree should look like this: |
|||
[[Category:Object oriented]] |
|||
<pre> Animal |
|||
[[Category:Type System]] |
|||
/\ |
|||
[[Category:Encyclopedia]] |
|||
/ \ |
|||
/ \ |
|||
::: ''This task is about derived types; for implementation inheritance, see [[Polymorphism]]. |
|||
Dog Cat |
|||
/\ |
|||
/ \ |
|||
Inheritance is an operation of [[type algebra]] that creates a new type from one or several parent types. |
|||
/ \ |
|||
Lab Collie</pre> |
|||
The obtained type is called '''derived type'''. |
|||
It inherits some of the properties of its parent types. |
|||
Usually inherited properties are: |
|||
:::* methods |
|||
:::* components |
|||
:::* parts of the representation |
|||
The [[classes | class]] of the new type is a '''subclass''' of the classes rooted in the parent types. |
|||
When all (in certain sense) properties of the parents are preserved by the derived type, it is said to be a [[wp:Liskov_substitution_principle|Liskov subtype]]. |
|||
When properties are preserved then the derived type is ''substitutable'' for its parents in all contexts. Usually full substitutability is achievable only in some contexts. |
|||
Inheritance is |
|||
:::* '''single''', when only one parent is allowed |
|||
:::* '''[[multiple inheritance | multiple]]''', otherwise |
|||
Some single inheritance languages usually allow multiple inheritance for certain [[abstract type]]s, interfaces in particular. |
|||
Inheritance can be considered as a relation parent-child. |
|||
Parent types are sometimes called '''supertype''', the derived ones are '''subtype'''. This relation is [[wp:Transitive_relation|transitive]] and [[wp:Reflexive_relation|reflexive]]. |
|||
Types bound by the relation form a [[wp:Directed_acyclic_graph directed acyclic graph]] (ignoring reflexivity). |
|||
With single inheritance it becomes a [[wp:Tree_(graph_theory)|tree]]. |
|||
;Task: |
|||
Show a tree of types which inherit from each other. |
|||
::: At the top of the tree should be a class called '''Animal'''. |
|||
::: The second level should have '''Dog''' and '''Cat'''. |
|||
::: Under '''Dog''' should be '''Lab''' and '''Collie'''. |
|||
::: None of the classes need to have any functions, the only thing they need to do is inherit from the specified superclasses <br> (overriding functions should be shown in [[Polymorphism]]). |
|||
The tree should look like this: |
|||
<pre> |
|||
Animal |
|||
/\ |
|||
/ \ |
|||
/ \ |
|||
Dog Cat |
|||
/\ |
|||
/ \ |
|||
/ \ |
|||
Lab Collie |
|||
</pre> |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">T Animal |
|||
{ |
|||
} |
|||
T Dog(Animal) |
|||
{ |
|||
} |
|||
T Cat(Animal) |
|||
{ |
|||
} |
|||
T Lab(Dog) |
|||
{ |
|||
} |
|||
T Collie(Dog) |
|||
{ |
|||
}</syntaxhighlight> |
|||
=={{header|ActionScript}}== |
|||
<syntaxhighlight lang="actionscript"> |
|||
public class Animal { |
|||
// ... |
|||
} |
|||
public class Cat extends Animal { |
|||
// ... |
|||
} |
|||
public class Dog extends Animal { |
|||
// ... |
|||
} |
|||
public class Lab extends Dog { |
|||
// ... |
|||
} |
|||
public class Collie extends Dog { |
|||
// ... |
|||
}</syntaxhighlight> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang="ada">package Inheritance is |
|||
<ada> |
|||
package Inheritance is |
|||
type Animal is tagged private; |
type Animal is tagged private; |
||
type Dog is new Animal with private; |
type Dog is new Animal with private; |
||
Line 24: | Line 114: | ||
type Lab is new Dog with null record; |
type Lab is new Dog with null record; |
||
type Collie is new Dog with null record; |
type Collie is new Dog with null record; |
||
end Inheritance; |
end Inheritance;</syntaxhighlight> |
||
</ada> |
|||
=={{header|Aikido}}== |
|||
<syntaxhighlight lang="aikido "> |
|||
class Animal{ |
|||
//functions go here... |
|||
} |
|||
class Dog extends Animal { |
|||
//functions go here... |
|||
} |
|||
class Cat extends Animal { |
|||
//functions go here... |
|||
} |
|||
class Lab extends Dog { |
|||
//functions go here... |
|||
} |
|||
class Collie extends Dog { |
|||
//functions go here... |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|AmigaE}}== |
|||
<syntaxhighlight lang="amigae"> |
|||
OBJECT animal |
|||
ENDOBJECT |
|||
OBJECT dog OF animal |
|||
ENDOBJECT |
|||
OBJECT cat OF animal |
|||
ENDOBJECT |
|||
OBJECT lab OF dog |
|||
ENDOBJECT |
|||
OBJECT collie OF dog |
|||
ENDOBJECT |
|||
</syntaxhighlight> |
|||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">script Animal |
|||
end script |
|||
script Dog |
|||
property parent : Animal |
|||
end script |
|||
script Cat |
|||
property parent : Animal |
|||
end script |
|||
script Lab |
|||
property parent : Dog |
|||
end script |
|||
script Collie |
|||
property parent : Dog |
|||
end script</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
|||
{{works with|AutoHotkey_L}} |
|||
AutoHotkey_L is prototype-based. However, for convenience, class-syntax may be used to create a base object. |
|||
<syntaxhighlight lang="autohotkey">dog := new Collie |
|||
MsgBox, % "A " dog.__Class " is a " dog.base.base.__Class " and is part of the " dog.kingdom " kingdom." |
|||
class Animal { |
|||
static kingdom := "Animalia" ; Class variable |
|||
} |
|||
class Dog extends Animal { |
|||
} |
|||
class Cat extends Animal { |
|||
} |
|||
class Lab extends Dog { |
|||
} |
|||
class Collie extends Dog { |
|||
}</syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
|||
{{works with|BBC BASIC for Windows}} |
|||
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"CLASSLIB" |
|||
DIM Animal{method} |
|||
PROC_class(Animal{}) |
|||
DIM Cat{method} |
|||
PROC_inherit(Cat{}, Animal{}) |
|||
PROC_class(Cat{}) |
|||
DIM Dog{method} |
|||
PROC_inherit(Dog{}, Animal{}) |
|||
PROC_class(Dog{}) |
|||
DIM Labrador{method} |
|||
PROC_inherit(Labrador{}, Dog{}) |
|||
PROC_class(Labrador{}) |
|||
DIM Collie{method} |
|||
PROC_inherit(Collie{}, Dog{}) |
|||
PROC_class(Collie{})</syntaxhighlight> |
|||
=={{header|C}}== |
|||
* See [[Inheritance/C]] |
|||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp">class Animal |
|||
{ |
|||
/* ... */ |
|||
// ... |
|||
} |
|||
class Dog : Animal |
|||
{ |
|||
/* ... */ |
|||
// ... |
|||
} |
|||
class Lab : Dog |
|||
{ |
|||
/* ... */ |
|||
// ... |
|||
} |
|||
class Collie : Dog |
|||
{ |
|||
/* ... */ |
|||
// ... |
|||
} |
|||
class Cat : Animal |
|||
{ |
|||
/* ... */ |
|||
// ... |
|||
}</syntaxhighlight> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang="cpp">class Animal |
|||
<cpp> |
|||
class Animal |
|||
{ |
{ |
||
// ... |
// ... |
||
Line 51: | Line 274: | ||
{ |
{ |
||
// ... |
// ... |
||
};</syntaxhighlight> |
|||
}; |
|||
</cpp> |
|||
=={{header|ChucK}}== |
|||
<syntaxhighlight lang="chuck"> |
|||
public class Drums{ |
|||
//functions go here... |
|||
} |
|||
public class LatinKit extends Drums{ |
|||
//functions go here... |
|||
} |
|||
public class ElectronicKit extends Drums{ |
|||
//functions go here... |
|||
} |
|||
public class Congas extends LatinKit{ |
|||
//functions go here... |
|||
} |
|||
public class TechnoDrums extends ElectronicKit{ |
|||
//functions go here... |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Clojure}}== |
|||
This is not very useful in clojure |
|||
<syntaxhighlight lang="clojure">(gen-class :name Animal) |
|||
(gen-class :name Dog :extends Animal) |
|||
(gen-class :name Cat :extends Animal) |
|||
(gen-class :name Lab :extends Dog) |
|||
(gen-class :name Collie :extends Dog)</syntaxhighlight> |
|||
More useful: |
|||
<syntaxhighlight lang="clojure">(derive ::dog ::animal) |
|||
(derive ::cat ::animal) |
|||
(derive ::lab ::dog) |
|||
(derive ::collie ::dog)</syntaxhighlight> |
|||
use: |
|||
<syntaxhighlight lang="clojure">user> (isa? ::dog ::animal) |
|||
true |
|||
user> (isa? ::dog ::cat) |
|||
false |
|||
user> (isa? ::collie ::animal) |
|||
true</syntaxhighlight> |
|||
=={{header|COBOL}}== |
|||
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
|||
CLASS-ID. Animal. |
|||
*> ... |
|||
END CLASS Animal. |
|||
IDENTIFICATION DIVISION. |
|||
CLASS-ID. Dog |
|||
INHERITS FROM Animal. |
|||
ENVIRONMENT DIVISION. |
|||
CONFIGURATION SECTION. |
|||
REPOSITORY. |
|||
CLASS Animal. |
|||
*> ... |
|||
END CLASS Dog. |
|||
IDENTIFICATION DIVISION. |
|||
CLASS-ID. Cat |
|||
INHERITS FROM Animal. |
|||
ENVIRONMENT DIVISION. |
|||
CONFIGURATION SECTION. |
|||
REPOSITORY. |
|||
CLASS Animal. |
|||
*> ... |
|||
END CLASS Cat. |
|||
IDENTIFICATION DIVISION. |
|||
CLASS-ID. Lab |
|||
INHERITS FROM Dog. |
|||
ENVIRONMENT DIVISION. |
|||
CONFIGURATION SECTION. |
|||
REPOSITORY. |
|||
CLASS Dog. |
|||
*> ... |
|||
END CLASS Lab. |
|||
IDENTIFICATION DIVISION. |
|||
CLASS-ID. Collie |
|||
INHERITS FROM Dog. |
|||
ENVIRONMENT DIVISION. |
|||
CONFIGURATION SECTION. |
|||
REPOSITORY. |
|||
CLASS Dog. |
|||
*> ... |
|||
END CLASS Collie.</syntaxhighlight> |
|||
=={{header|Coco}}== |
|||
<syntaxhighlight lang="coco">class Animal |
|||
class Cat extends Animal |
|||
class Dog extends Animal |
|||
class Lab extends Dog |
|||
class Collie extends Dog</syntaxhighlight> |
|||
On the subject of inheritance, it is worth noting that Coco's <code>super</code> works differently from CoffeeScript's. In particular, the constructor of a subclass should generally say <code>super ...</code>, not just <code>super</code>. Here is a translation of the example from the CoffeeScript documentation: |
|||
<syntaxhighlight lang="coco">class Animal |
|||
(@name) -> |
|||
move: (meters) -> |
|||
alert @name + " moved #{meters}m." |
|||
class Snake extends Animal |
|||
-> super ... |
|||
move: -> |
|||
alert 'Slithering...' |
|||
super 5 |
|||
class Horse extends Animal |
|||
-> super ... |
|||
move: -> |
|||
alert 'Galloping...' |
|||
super 45 |
|||
sam = new Snake 'Sammy the Python' |
|||
tom = new Horse 'Tommy the Palomino' |
|||
sam.move! |
|||
tom.move!</syntaxhighlight> |
|||
=={{header|Comal}}== |
|||
{{works with|UniComal}} |
|||
{{works with|AmiComal}} |
|||
<syntaxhighlight lang="comal"> STRUC Animal |
|||
DIM Species$ OF 20 |
|||
ENDSTRUC Animal |
|||
STRUC Dog |
|||
INHERIT Animal |
|||
DIM Race$ OF 20 |
|||
FUNC New CONSTRUCTOR |
|||
Species$="Dog" |
|||
ENDFUNC New |
|||
ENDSTRUC Dog |
|||
STRUC Cat |
|||
INHERIT Animal |
|||
DIM Race$ OF 20 |
|||
FUNC New CONSTRUCTOR |
|||
Species$="Cat" |
|||
ENDFUNC New |
|||
ENDSTRUC Cat |
|||
STRUC Lab |
|||
INHERIT Dog |
|||
FUNC New CONSTRUCTOR |
|||
Race$:="Lab" |
|||
ENDFUNC New |
|||
ENDSTRUC Lab |
|||
STRUC Collie |
|||
INHERIT Dog |
|||
FUNC New CONSTRUCTOR |
|||
Race$:="Collie" |
|||
ENDFUNC New |
|||
ENDSTRUC Collie</syntaxhighlight> |
|||
=={{header|Common Lisp}}== |
|||
Using CLOS classes, we have the following: |
|||
<syntaxhighlight lang="lisp">(defclass animal () ()) |
|||
(defclass dog (animal) ()) |
|||
(defclass lab (dog) ()) |
|||
(defclass collie (dog) ()) |
|||
(defclass cat (animal) ())</syntaxhighlight> |
|||
Alternatively, since there is no multiple inheritance in the task requirement, structures could also be used: |
|||
<syntaxhighlight lang="lisp">(defstruct animal) |
|||
(defstruct (dog (:include animal))) |
|||
(defstruct (lab (:include dog))) |
|||
(defstruct (collie (:include dog))) |
|||
(defstruct (cat (:include animal)))</syntaxhighlight> |
|||
(Structures are less flexible than CLOS objects but often somewhat more efficiently implemented, due to those restrictions.) |
|||
Inheritance is not required for object-oriented programming in Lisp. It is used for code reuse, because it allows common utilities and protocol conventions to be factored out into base class methods. However, a class doesn't have to inherit from a base class just so that some existing methods can work with instances of that class. |
|||
Furthermore, all of the "basic types" also have a class, so methods can be readily specialized to lists, integers, strings, symbols, et cetera. This is done without having to modify any class definitions. |
|||
<syntaxhighlight lang="lisp"> |
|||
;;; ASN.1 serialization logic specialized for animal class |
|||
(defmethod serialize-to-asn-1 ((a animal)) |
|||
#| ... |# |
|||
) |
|||
;;; casually introduce the method over strings too; no relation to animal |
|||
(defmethod serialize-to-asn-1 ((s string)) |
|||
#| ... #| |
|||
)</syntaxhighlight> |
|||
These classes do not have to inherit from some interface or base class which provides a prototype for the serialize-to-asn-1 method. Such a requirement has more to do with static typing than object oriented programming. Usually in languages which require such inheritance, there are also statically typed references. A class must conform to some "ASNEncodable" class so that its instances can be passed to functions which expect references to an ASN1Encodable type, which is verified at compile time. |
|||
=={{header|Component Pascal}}== |
|||
<syntaxhighlight lang="oberon2"> |
|||
TYPE |
|||
Animal = ABSTRACT RECORD (* *) END; |
|||
Cat = RECORD (Animal) (* *) END; (* final record (cannot be extended) - by default *) |
|||
Dog = EXTENSIBLE RECORD (Animal) (* *) END; (* extensible record *) |
|||
Lab = RECORD (Dog) (* *) END; |
|||
Collie = RECORD (Dog) (* *) END; |
|||
</syntaxhighlight> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<d>class Animal |
<syntaxhighlight lang="d">class Animal { |
||
// ... |
|||
{ |
|||
} |
|||
class Dog: Animal { |
|||
// ... |
|||
} |
|||
class Lab: Dog { |
|||
// ... |
|||
} |
|||
class Collie: Dog { |
|||
// ... |
|||
} |
|||
class Cat: Animal { |
|||
// ... |
|||
} |
|||
void main() {}</syntaxhighlight> |
|||
=={{header|Delphi}}== |
|||
<syntaxhighlight lang="delphi">type |
|||
Animal = class(TObject) |
|||
private |
|||
// private functions/variables |
|||
public |
|||
// public functions/variables |
|||
end; |
|||
Dog = class(Animal); |
|||
Cat = class(Animal); |
|||
Collie = class(Dog); |
|||
Lab = class(Dog);</syntaxhighlight> |
|||
=={{header|DWScript}}== |
|||
<syntaxhighlight lang="delphi">type |
|||
Animal = class(TObject) |
|||
private |
|||
// private functions/variables |
|||
public |
|||
// public functions/variables |
|||
end; |
|||
type Dog = class(Animal) end; |
|||
type Cat = class(Animal) end; |
|||
type Collie = class(Dog) end; |
|||
type Lab = class(Dog) end;</syntaxhighlight> |
|||
=={{header|E}}== |
|||
Outside of interactions with the host platform's objects, E does not generally deal in complex type hierarchies; the focus is more on "what guarantees does this object provide", and composition rather than inheritance. However, it is possible to set up a type hierarchy scheme with just a bit of code. |
|||
In E, a ''guard'' accepts, or coerces, certain objects and rejects others; its [[wp:Range (mathematics)|range]] constitutes a type. An ''auditor'' examines the implementation of an object and marks it approved; a ''stamp'' is an auditor which does no actual checking. Here, we create a guard/stamp pair; the guard accepts every stamped object. The stamp also asks for each supertype's stamp on the objects it audits. |
|||
<syntaxhighlight lang="e">def makeType(label, superstamps) { |
|||
def stamp { |
|||
to audit(audition) { |
|||
for s in superstamps { audition.ask(s) } |
|||
return true |
|||
} |
|||
} |
|||
def guard { |
|||
to coerce(specimen, ejector) { |
|||
if (__auditedBy(stamp, specimen)) { |
|||
return specimen |
|||
} else { |
|||
throw.eject(ejector, `$specimen is not a $label`) |
|||
} |
|||
} |
|||
} |
|||
return [guard, stamp] |
|||
}</syntaxhighlight> |
|||
Setting up the task's specified tree: |
|||
<syntaxhighlight lang="e">def [Animal, AnimalStamp] := makeType("Animal", []) |
|||
def [Cat, CatStamp] := makeType("Cat", [AnimalStamp]) |
|||
def [Dog, DogStamp] := makeType("Dog", [AnimalStamp]) |
|||
def [Lab, LabStamp] := makeType("Lab", [DogStamp]) |
|||
def [Collie, CollieStamp] := makeType("Collie", [DogStamp])</syntaxhighlight> |
|||
Some example objects: |
|||
<syntaxhighlight lang="e">def fido implements LabStamp {} |
|||
def tom implements CatStamp {} |
|||
def brick {} # not an animal</syntaxhighlight> |
|||
Testing against the types: |
|||
<syntaxhighlight lang="e">? fido :Animal |
|||
# value: <fido> |
|||
? fido :Cat |
|||
# problem: <fido> is not a Cat |
|||
? fido :Lab |
|||
# value: <fido> |
|||
? tom :Animal |
|||
# value: <tom> |
|||
? tom :Cat |
|||
# value: <tom> |
|||
? brick :Animal |
|||
# problem: <brick> is not a Animal</syntaxhighlight> |
|||
=={{header|Eiffel}}== |
|||
<syntaxhighlight lang="eiffel "> |
|||
class |
|||
ANIMAL |
|||
end |
|||
class |
|||
DOG |
|||
inherit |
|||
ANIMAL |
|||
end |
|||
class |
|||
CAT |
|||
inherit |
|||
ANIMAL |
|||
end |
|||
class |
|||
LAB |
|||
inherit |
|||
DOG |
|||
end |
|||
class |
|||
COLLIE |
|||
inherit |
|||
DOG |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|Elena}}== |
|||
ELENA 4.x : |
|||
<syntaxhighlight lang="elena">class Animal |
|||
{ |
|||
// ... |
|||
} |
|||
class Dog : Animal |
|||
{ |
|||
// ... |
|||
} |
|||
class Lab : Dog |
|||
{ |
|||
// ... |
|||
} |
|||
class Collie : Dog |
|||
{ |
|||
// ... |
// ... |
||
} |
} |
||
class Cat : Animal |
|||
{ |
|||
// ... |
|||
}</syntaxhighlight> |
|||
=={{header|EMal}}== |
|||
class Dog: Animal |
|||
<syntaxhighlight lang="emal"> |
|||
in Org:RosettaCode |
|||
type Animal |
|||
model do end |
|||
type Dog extends Animal |
|||
model do end |
|||
type Cat extends Animal |
|||
model do end |
|||
type Lab extends Dog |
|||
model do end |
|||
type Collie extends Dog |
|||
model do end |
|||
type Main |
|||
var fuffy = Collie() |
|||
for each generic kind in generic[Animal, Dog, Cat, Lab, Collie] |
|||
writeLine("Fuffy " + when(Generic.check(kind, fuffy), "is", "is not") + " a " + Generic.name(kind)) |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Fuffy is a Org:RosettaCode:Animal |
|||
Fuffy is a Org:RosettaCode:Dog |
|||
Fuffy is not a Org:RosettaCode:Cat |
|||
Fuffy is not a Org:RosettaCode:Lab |
|||
Fuffy is a Org:RosettaCode:Collie |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
|||
The <code>()</code> behind the class names indicates a public default constructor; you need some type of public constructor to derive from a class. |
|||
<syntaxhighlight lang="fsharp">type Animal() = |
|||
class // explicit syntax needed for empty class |
|||
end |
|||
type Dog() = |
|||
inherit Animal() |
|||
type Lab() = |
|||
inherit Dog() |
|||
type Collie() = |
|||
inherit Dog() |
|||
type Cat() = |
|||
inherit Animal()</syntaxhighlight> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang="factor">TUPLE: animal ; |
|||
TUPLE: dog < animal ; |
|||
TUPLE: cat < animal ; |
|||
TUPLE: lab < dog ; |
|||
TUPLE: collie < dog ;</syntaxhighlight> |
|||
=={{header|Fancy}}== |
|||
<syntaxhighlight lang="fancy">class Animal { |
|||
# ... |
|||
} |
|||
class Dog : Animal { |
|||
# ... |
|||
} |
|||
class Cat : Animal { |
|||
# ... |
|||
} |
|||
class Lab : Dog { |
|||
# ... |
|||
} |
|||
class Collie : Dog { |
|||
# ... |
|||
}</syntaxhighlight> |
|||
=={{header|Fantom}}== |
|||
<syntaxhighlight lang="fantom">class Animal |
|||
{ |
{ |
||
// ... |
|||
} |
} |
||
class |
class Dog : Animal |
||
{ |
{ |
||
// ... |
|||
} |
} |
||
class |
class Cat : Animal |
||
{ |
{ |
||
// ... |
|||
} |
} |
||
class |
class Lab : Dog |
||
{ |
{ |
||
} |
|||
// ... |
|||
}</d> |
|||
class Collie : Dog |
|||
{ |
|||
}</syntaxhighlight> |
|||
=={{header|Forth}}== |
|||
{{works with|4tH|3.61.5}} |
|||
There are numerous, mutually incompatible object oriented frameworks for Forth. This one works with the FOOS preprocessor extension of [[4tH]]. |
|||
<syntaxhighlight lang="forth">include 4pp/lib/foos.4pp |
|||
:: Animal class end-class {} ; |
|||
:: Dog extends Animal end-extends {} ; |
|||
:: Cat extends Animal end-extends {} ; |
|||
:: Lab extends Dog end-extends {} ; |
|||
:: Collie extends Dog end-extends {} ;</syntaxhighlight> |
|||
Works with any ANS Forth |
|||
Needs the FMS2 library code located here: |
|||
https://github.com/DouglasBHoffman/FMS2 |
|||
<syntaxhighlight lang="forth">include FMS2LL.f |
|||
:class Animal ;class |
|||
:class Dog <super Animal ;class |
|||
:class Cat <super Animal ;class |
|||
:class Lab <super Dog ;class |
|||
:class Collie <super Dog ;class</syntaxhighlight> |
|||
=={{header|Fortran}}== |
|||
OO has been part of the Fortran standard since 2003 but the compilers are still playing catchup. This example builds with the Intel 11.1.069 compiler (free for personal use on linux). |
|||
<syntaxhighlight lang="fortran">module anim |
|||
type animal |
|||
end type animal |
|||
type, extends(animal) :: dog |
|||
end type dog |
|||
type, extends(animal) :: cat |
|||
end type cat |
|||
type, extends(dog) :: lab |
|||
end type lab |
|||
type, extends(dog) :: collie |
|||
end type collie |
|||
end module anim</syntaxhighlight> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Type Animal Extends Object ' to enable virtual methods etc. if needed |
|||
' ... |
|||
End Type |
|||
Type Dog Extends Animal |
|||
' ... |
|||
End Type |
|||
Type Cat Extends Animal |
|||
' ... |
|||
End Type |
|||
Type Lab Extends Dog |
|||
' ... |
|||
End Type |
|||
Type Collie Extends Dog |
|||
' ... |
|||
End Type</syntaxhighlight> |
|||
=={{header|Go}}== |
|||
Go eschews most trappings of inheritance, yet it's anonymous field feature allows building one struct type upon another and accessing fields of "embedded" types without extra synax. |
|||
<syntaxhighlight lang="go">package main |
|||
type animal struct { |
|||
alive bool |
|||
} |
|||
type dog struct { |
|||
animal |
|||
obedienceTrained bool |
|||
} |
|||
type cat struct { |
|||
animal |
|||
litterBoxTrained bool |
|||
} |
|||
type lab struct { |
|||
dog |
|||
color string |
|||
} |
|||
type collie struct { |
|||
dog |
|||
catchesFrisbee bool |
|||
} |
|||
func main() { |
|||
var pet lab |
|||
pet.alive = true |
|||
pet.obedienceTrained = false |
|||
pet.color = "yellow" |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Groovy}}== |
|||
<syntaxhighlight lang="groovy"> |
|||
class Animal{ |
|||
//contents go here... |
|||
} |
|||
class Dog extends Animal{ |
|||
//contents go here... |
|||
} |
|||
class Cat extends Animal{ |
|||
//contents go here... |
|||
} |
|||
class Lab extends Dog{ |
|||
//contents go here... |
|||
} |
|||
class Collie extends Dog{ |
|||
//contents go here... |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Haskell}}== |
|||
A type can't inherit properties from other types, but it can belong to any number of type classes, which may themselves be subclasses of other type classes. |
|||
<syntaxhighlight lang="haskell">class Animal a |
|||
class Animal a => Cat a |
|||
class Animal a => Dog a |
|||
class Dog a => Lab a |
|||
class Dog a => Collie a</syntaxhighlight> |
|||
=={{header|Haxe}}== |
|||
<syntaxhighlight lang="haxe"> |
|||
class Animal { |
|||
// ... |
|||
} |
|||
class Cat extends Animal { |
|||
// ... |
|||
} |
|||
class Dog extends Animal { |
|||
// ... |
|||
} |
|||
class Lab extends Dog { |
|||
// ... |
|||
} |
|||
class Collie extends Dog { |
|||
// ... |
|||
} |
|||
</syntaxhighlight> |
|||
== Icon and {{header|Unicon}} == |
|||
This example only works in Unicon. |
|||
<syntaxhighlight lang="unicon"> |
|||
class Animal () |
|||
end |
|||
class Dog : Animal () |
|||
end |
|||
class Cat : Animal () |
|||
end |
|||
class Lab : Dog () |
|||
end |
|||
class Collie : Dog () |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|Inform 7}}== |
|||
<syntaxhighlight lang="inform7">An animal is a kind of thing. |
|||
A cat is a kind of animal. |
|||
A dog is a kind of animal. |
|||
A collie is a kind of dog. |
|||
A lab is a kind of dog.</syntaxhighlight> |
|||
"Animal" is actually a predefined kind in Inform 7, so its definition here is redundant (but legal). |
|||
=={{header|Io}}== |
=={{header|Io}}== |
||
<syntaxhighlight lang="io">Animal := Object clone |
|||
Cat := Animal clone |
|||
Dog := Animal clone |
|||
Collie := Dog clone |
|||
Lab := Dog clone</syntaxhighlight> |
|||
=={{header|J}}== |
|||
Here is how this would normally be done: |
|||
<syntaxhighlight lang="j"> |
|||
coclass 'Animal' |
|||
coclass 'Dog' |
|||
coinsert 'Animal' |
|||
coclass 'Cat' |
|||
coinsert 'Animal' |
|||
coclass 'Lab' |
|||
coinsert 'Dog' |
|||
coclass 'Collie' |
|||
coinsert 'Dog' |
|||
</syntaxhighlight> |
|||
<code>coclass</code> specifies that following definitions will be within the named class, and <code>coinsert</code> specifies that the current class will inherit from the named classes (or object -- in J the only difference between a class and an object is its name and how you can create them -- this motivates the "co" prefix on operations which manipulate '''c'''lasses and '''o'''bjects). |
|||
See http://www.jsoftware.com/help/jforc/modular_code.htm |
|||
That said, some operations in J -- including <code>coinsert</code> -- will create classes if they did not already exist. So the above may be simplified to: |
|||
<syntaxhighlight lang="j">coinsert_Dog_ 'Animal' |
|||
coinsert_Cat_ 'Animal' |
|||
coinsert_Lab_ 'Dog' |
|||
coinsert_Collie_ 'Dog'</syntaxhighlight> |
|||
That said, note that classes and objects are not "types" in J. Instead, they are components of names. In general, when we deal with objects and classes we deal with references to the underlying representation, and in J the references are names, so a collection of classes and objects, in J, would be a collection of names which refer to classes and objects. In other words, the "type" (to the degree that there is a type) would be best thought of as "name" (or, more mechanically: boxed list of characters). |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang="java"> |
|||
<java>public class Animal{ |
|||
public class Animal{ |
|||
//functions go here... |
//functions go here... |
||
} |
|||
}</java> |
|||
public class Dog extends Animal{ |
|||
//functions go here... |
//functions go here... |
||
} |
|||
}</java> |
|||
public class Cat extends Animal{ |
|||
//functions go here... |
//functions go here... |
||
} |
|||
}</java> |
|||
public class Lab extends Dog{ |
|||
//functions go here... |
//functions go here... |
||
} |
|||
}</java> |
|||
public class Collie extends Dog{ |
|||
//functions go here... |
//functions go here... |
||
} |
|||
}</java> |
|||
</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
|||
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. |
|||
<syntaxhighlight lang="javascript"> |
|||
function Animal() { |
|||
// ... |
|||
} |
|||
function Dog() { |
|||
// ... |
|||
} |
|||
Dog.prototype = new Animal(); |
|||
function Cat() { |
|||
// ... |
|||
} |
|||
Cat.prototype = new Animal(); |
|||
function Collie() { |
|||
// ... |
|||
} |
|||
Collie.prototype = new Dog(); |
|||
function Lab() { |
|||
// ... |
|||
} |
|||
Lab.prototype = new Dog(); |
|||
Animal.prototype.speak = function() {print("an animal makes a sound")}; |
|||
var lab = new Lab(); |
|||
lab.speak(); // shows "an animal makes a sound" |
|||
</syntaxhighlight> |
|||
=={{header|Julia}}== |
|||
Julia is not really an object-oriented programming language. It supports polymorphism and inheriting functionality but not structure. Thus inheritance hierarchies must be made with abstract types. Abstract types can not be instantiated and do not contain any fields. So below Dog is abstract while Collie is a concrete type which may contain fields. |
|||
<syntaxhighlight lang="julia"> |
|||
abstract type Animal end |
|||
abstract type Dog <: Animal end |
|||
abstract type Cat <: Animal end |
|||
struct Lab <: Dog end |
|||
struct Collie <: Dog end |
|||
</syntaxhighlight> |
|||
=={{header|Kite}}== |
|||
<syntaxhighlight lang="kite">class Animal [ |
|||
#Method goes here |
|||
]; |
|||
class Dog from Animal [ |
|||
#Method goes here |
|||
]; |
|||
class Lab from Dog [ |
|||
#Method goes here |
|||
]; |
|||
class collie from Dog [ |
|||
#Method goes here |
|||
]; |
|||
</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang="scala">// version 1.0.6 |
|||
open class Animal { |
|||
override fun toString() = "animal" |
|||
} |
|||
open class Dog : Animal() { |
|||
override fun toString() = "dog" |
|||
} |
|||
class Cat : Animal() { |
|||
override fun toString() = "cat" |
|||
} |
|||
class Labrador : Dog() { |
|||
override fun toString() = "labrador" |
|||
} |
|||
class Collie : Dog() { |
|||
override fun toString() = "collie" |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val felix: Animal = Cat() |
|||
val rover: Animal = Dog() |
|||
val bella: Dog = Labrador() |
|||
val casey: Dog = Collie() |
|||
println("Felix is a $felix") |
|||
println("Rover is a $rover") |
|||
println("Bella is a $bella") |
|||
println("Casey is a $casey") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Felix is a cat |
|||
Rover is a dog |
|||
Bella is a labrador |
|||
Casey is a collie |
|||
</pre> |
|||
=={{header|Lasso}}== |
|||
<syntaxhighlight lang="lasso">define animal => type { |
|||
data public gender::string |
|||
} |
|||
define dog => type { |
|||
parent animal |
|||
} |
|||
define cat => type { |
|||
parent animal |
|||
} |
|||
define collie => type { |
|||
parent dog |
|||
} |
|||
define lab => type { |
|||
parent dog |
|||
} |
|||
local(myanimal = lab) |
|||
#myanimal -> gender = 'Male' |
|||
#myanimal -> gender</syntaxhighlight> |
|||
-> Male |
|||
=={{header|Latitude}}== |
|||
Latitude is a prototype-oriented language, so defining a subclass is equivalent to constructing an instance. |
|||
<syntaxhighlight lang="latitude"> |
|||
Animal ::= Object clone tap { |
|||
;; Methods go here... |
|||
}. |
|||
Dog ::= Animal clone tap { |
|||
;; Methods go here... |
|||
}. |
|||
Cat ::= Animal clone tap { |
|||
;; Methods go here... |
|||
}. |
|||
Lab ::= Dog clone tap { |
|||
;; Methods go here... |
|||
}. |
|||
Collie ::= Dog clone tap { |
|||
;; Methods go here... |
|||
}.</syntaxhighlight> |
|||
We <code>clone</code> the parent and then <code>tap</code> the new instance to add functionality to it. Note that we use <code>::=</code> here rather than the usual <code>:=</code>, as the former implicitly defines an appropriate <code>toString</code> method representative of the new "class". |
|||
=={{header|Lingo}}== |
|||
In Lingo Classes are represented by "parent scripts". Instead of using new() as in the code below, child classes can also use rawNew() when creating an instance of their parent classes. rawNew() creates an instance of a class without calling its initialization function 'new' (constructor). |
|||
<syntaxhighlight lang="lingo"> |
|||
-- parent script "Animal" |
|||
-- ... |
|||
-- parent script "Dog" |
|||
property ancestor |
|||
on new (me) |
|||
me.ancestor = script("Animal").new() |
|||
return me |
|||
end |
|||
-- parent script "Cat" |
|||
property ancestor |
|||
on new (me) |
|||
me.ancestor = script("Animal").new() |
|||
return me |
|||
end |
|||
-- parent script "Lab" |
|||
property ancestor |
|||
on new (me) |
|||
me.ancestor = script("Dog").new() |
|||
return me |
|||
end |
|||
-- parent script "Collie" |
|||
property ancestor |
|||
on new (me) |
|||
me.ancestor = script("Dog").new() |
|||
return me |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|Lisaac}}== |
|||
<syntaxhighlight lang="lisaac"> |
|||
Section Header |
|||
+ name := ANIMAL; |
|||
// ... |
|||
Section Header |
|||
+ name := CAT; |
|||
Section Inherit |
|||
- parent : ANIMAL := ANIMAL; |
|||
// ... |
|||
Section Header |
|||
+ name := DOG; |
|||
Section Inherit |
|||
- parent : ANIMAL := ANIMAL; |
|||
// ... |
|||
Section Header |
|||
+ name := LAB; |
|||
Section Inherit |
|||
- parent : DOG := DOG; |
|||
// ... |
|||
Section Header |
|||
+ name := COLLIE; |
|||
Section Inherit |
|||
- parent : DOG := DOG; |
|||
// ... |
|||
</syntaxhighlight> |
|||
=={{header|Logtalk}}== |
|||
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). |
|||
<syntaxhighlight lang="logtalk"> |
|||
:- object(thing, |
|||
instantiates(thing)). |
|||
:- end_object. |
|||
:- object(animal, |
|||
specializes(thing)). |
|||
... |
|||
:- end_object. |
|||
:- object(dog, |
|||
specializes(animal)). |
|||
... |
|||
:- end_object. |
|||
:- object(cat, |
|||
specializes(animal)). |
|||
... |
|||
:- end_object. |
|||
:- object(lab, |
|||
specializes(dog)). |
|||
... |
|||
:- end_object. |
|||
:- object(collie, |
|||
specializes(dog)). |
|||
... |
|||
:- end_object.</syntaxhighlight> |
|||
=={{header|Lua}}== |
|||
Lua has no in-built formal OOP mechanism, though there are many possible ways of implementing work-alikes. |
|||
<syntaxhighlight lang="lua">Class = { |
|||
classname = "Class aka Object aka Root-Of-Tree", |
|||
new = function(s,t) |
|||
s.__index = s |
|||
local instance = setmetatable(t or {}, s) |
|||
instance.parent = s |
|||
return instance |
|||
end |
|||
} |
|||
Animal = Class:new{classname="Animal", speak=function(s) return s.voice or "("..s.classname.." has no voice)" end } |
|||
Cat = Animal:new{classname="Cat", voice="meow"} |
|||
Dog = Animal:new{classname="Dog", voice="woof"} |
|||
Lab = Dog:new{classname="Lab", voice="bark"} |
|||
Collie = Dog:new{classname="Collie"} -- subclass without a unique voice |
|||
print("Animal:speak(): " .. Animal:speak()) |
|||
print("Cat:speak(): " .. Cat:speak()) |
|||
print("Dog:speak(): " .. Dog:speak()) |
|||
print("Lab:speak(): " .. Lab:speak()) |
|||
print("Collie:speak(): " .. Collie:speak()) |
|||
max = Collie:new{voice="Hi, I am Max the talking Collie!"} -- instance with a unique voice |
|||
print("max:speak(): " .. max:speak()) |
|||
print("max himself is (instance): " .. max.classname) |
|||
print("max's parent is (class): " .. max.parent.classname) |
|||
print("max's parent's parent is (class): " .. max.parent.parent.classname) |
|||
print("max's parent's parent's parent is (class): " .. max.parent.parent.parent.classname) |
|||
print("max's parent's parent's parent's parent is (class): " .. max.parent.parent.parent.parent.classname) |
|||
print("max's parent's parent's parent's parent's parent is (nil reference): " .. tostring(max.parent.parent.parent.parent.parent))</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Animal:speak(): (Animal has no voice) |
|||
Cat:speak(): meow |
|||
Dog:speak(): woof |
|||
Lab:speak(): bark |
|||
Collie:speak(): woof |
|||
max:speak(): Hi, I am Max the talking Collie! |
|||
max himself is (instance): Collie |
|||
max's parent is (class): Collie |
|||
max's parent's parent is (class): Dog |
|||
max's parent's parent's parent is (class): Animal |
|||
max's parent's parent's parent's parent is (class): Class aka Object aka Root-Of-Tree |
|||
max's parent's parent's parent's parent's parent is (nil reference): nil</pre> |
|||
=={{header|M2000 Interpreter}}== |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Module CheckIt { |
|||
Class Animal { |
|||
} |
|||
Class Dog as Animal { |
|||
} |
|||
Class Cat as Animal{ |
|||
} |
|||
Class Labrador As Dog { |
|||
} |
|||
Class Collie As Dog{ |
|||
} |
|||
\\ a is a pointer to a group made from class Labrador |
|||
a->Labrador() |
|||
Print a is type Animal = True |
|||
Print a is type Dog = True |
|||
Print a is type Labrador = True |
|||
} |
|||
CheckIt |
|||
</syntaxhighlight> |
|||
=={{header|Neko}}== |
|||
<syntaxhighlight lang="neko">var Animal = $new(null); |
|||
var Dog = $new(null); |
|||
$objsetproto(Dog, Animal); |
|||
var Cat = $new(null); |
|||
$objsetproto(Cat, Animal); |
|||
var Lab = $new(null); |
|||
$objsetproto(Lab, Dog); |
|||
var Collie = $new(null); |
|||
$objsetproto(Collie, Dog);</syntaxhighlight> |
|||
=={{header|Nemerle}}== |
|||
<syntaxhighlight lang="nemerle">class Animal { |
|||
// ... |
|||
} |
|||
class Dog: Animal { |
|||
// ... |
|||
} |
|||
class Lab: Dog { |
|||
// ... |
|||
} |
|||
class Collie: Dog { |
|||
// ... |
|||
} |
|||
class Cat: Animal { |
|||
// ... |
|||
}</syntaxhighlight> |
|||
=={{header|NetRexx}}== |
|||
Class names cosmetically augmented slightly to prevent namespace pollution. |
|||
For brevity, all classes are defined within the same source file. Normally classes exist as separate source units. |
|||
<syntaxhighlight lang="netrexx">/* NetRexx */ |
|||
options replace format comments java crossref symbols binary |
|||
class RInheritSingle public |
|||
method main(args = String[]) public static |
|||
animals = [ - |
|||
RInheritSingle_Animal(), - |
|||
RInheritSingle_Cat(), - |
|||
RInheritSingle_Dog(), - |
|||
RInheritSingle_Lab(), - |
|||
RInheritSingle_Collie() - |
|||
] |
|||
say 'Object ID'.left(12) 'Class type'.left(24) 'Superclass type' |
|||
say '.'.left(12, '.') '.'.left(24, '.') '.'.left(24, '.') |
|||
loop animal over animals |
|||
parse animal.whatAmI() oid ct st |
|||
say oid.left(12) ct.left(24) st |
|||
end animal |
|||
return |
|||
class RInheritSingle_Animal private |
|||
properties indirect |
|||
whatThatIs = String |
|||
whatThisIs = String |
|||
method RInheritSingle_Animal() public |
|||
-- Animal specific set-up |
|||
setWhatThatIs(this.getClass().getSuperclass().getSimpleName()) |
|||
setWhatThisIs(this.getClass().getSimpleName()) |
|||
return |
|||
method hashToString() public |
|||
return '@'(Rexx this.hashCode()).d2x().right(8, 0) |
|||
method whatAmI() public |
|||
iAmText = hashToString() getWhatThisIs() getWhatThatIs() |
|||
return iAmText |
|||
class RInheritSingle_Cat private extends RInheritSingle_Animal |
|||
method RInheritSingle_Cat() public |
|||
-- Do Cat specific set-up |
|||
return |
|||
class RInheritSingle_Dog private extends RInheritSingle_Animal |
|||
method RInheritSingle_Dog() public |
|||
-- Do Dog specific set-up |
|||
return |
|||
class RInheritSingle_Lab private extends RInheritSingle_Dog |
|||
method RInheritSingle_Lab() public |
|||
-- Do Lab specific set-up |
|||
return |
|||
class RInheritSingle_Collie private extends RInheritSingle_Dog |
|||
method RInheritSingle_Collie() public |
|||
-- Do Collie specific set-up |
|||
return |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Object ID Class type Superclass type |
|||
............ ........................ ........................ |
|||
@3F81D405 RInheritSingle_Animal Object |
|||
@51430296 RInheritSingle_Cat RInheritSingle_Animal |
|||
@065EEF88 RInheritSingle_Dog RInheritSingle_Animal |
|||
@42BFCCFC RInheritSingle_Lab RInheritSingle_Dog |
|||
@3E2AD6A0 RInheritSingle_Collie RInheritSingle_Dog |
|||
</pre> |
|||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">type |
|||
Animal = object of RootObj |
|||
Dog = object of Animal |
|||
Cat = object of Animal |
|||
Lab = object of Dog |
|||
Collie = object of Dog</syntaxhighlight> |
|||
=={{header|Oberon}}== |
|||
Tested with [https://miasap.se/obnc OBNC]. |
|||
<syntaxhighlight lang="oberon">MODULE Animals; |
|||
TYPE |
|||
Animal = RECORD END; |
|||
Dog = RECORD (Animal) END; |
|||
Cat = RECORD (Animal) END; |
|||
Lab = RECORD (Dog) END; |
|||
Collie = RECORD (Dog) END; |
|||
END Animals. |
|||
</syntaxhighlight> |
|||
=={{header|Oberon-2}}== |
|||
Works with oo2c Version 2 |
|||
<syntaxhighlight lang="oberon2"> |
|||
MODULE Animals; |
|||
TYPE |
|||
Animal = POINTER TO AnimalDesc; |
|||
AnimalDesc = RECORD END; |
|||
Cat = POINTER TO CatDesc; |
|||
CatDesc = RECORD (AnimalDesc) END; |
|||
Dog = POINTER TO DogDesc; |
|||
DogDesc = RECORD (AnimalDesc) END; |
|||
Lab = POINTER TO LabDesc; |
|||
LabDesc = RECORD (DogDesc) END; |
|||
Collie = POINTER TO CollieDesc; |
|||
CollieDesc = RECORD (DogDesc) END; |
|||
END Animals. |
|||
</syntaxhighlight> |
|||
=={{header|Objeck}}== |
|||
<syntaxhighlight lang="objeck">class Animal |
|||
{ #~ ... ~# } |
|||
class Dog from Animal |
|||
{ #~ ... ~# } |
|||
class Lab from Dog |
|||
{ #~ ... ~# } |
|||
class Collie from Dog |
|||
{ #~ ... ~# } |
|||
class Cat from Animal |
|||
{ #~ ... ~# }</syntaxhighlight> |
|||
=={{header|Objective-C}}== |
|||
<syntaxhighlight lang="objc">@interface Animal : NSObject |
|||
{ |
|||
// ... |
|||
} |
|||
// ... |
|||
@end |
|||
@interface Dog : Animal |
|||
{ |
|||
// ... |
|||
} |
|||
// ... |
|||
@end |
|||
@interface Lab : Dog |
|||
{ |
|||
// ... |
|||
} |
|||
// ... |
|||
@end |
|||
@interface Collie : Dog |
|||
{ |
|||
// ... |
|||
} |
|||
// ... |
|||
@end |
|||
@interface Cat : Animal |
|||
{ |
|||
// ... |
|||
} |
|||
// ... |
|||
@end</syntaxhighlight> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang="ocaml"> |
|||
<ocaml>class animal = |
|||
class animal = |
|||
object (self) |
object (self) |
||
(*functions go here...*) |
(*functions go here...*) |
||
end |
end |
||
class dog = |
|||
object (self) |
object (self) |
||
inherit animal |
inherit animal |
||
(*functions go here...*) |
(*functions go here...*) |
||
end |
end |
||
class cat = |
|||
object (self) |
object (self) |
||
inherit animal |
inherit animal |
||
(*functions go here...*) |
(*functions go here...*) |
||
end |
end |
||
class lab = |
|||
object (self) |
object (self) |
||
inherit dog |
inherit dog |
||
(*functions go here...*) |
(*functions go here...*) |
||
end |
end |
||
class collie = |
|||
object (self) |
object (self) |
||
inherit dog |
inherit dog |
||
(*functions go here...*) |
(*functions go here...*) |
||
end |
end |
||
</syntaxhighlight> |
|||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="odin">package main |
|||
Animal :: struct { |
|||
alive: bool |
|||
} |
|||
Dog :: struct { |
|||
using animal: Animal, |
|||
obedience_trained: bool |
|||
} |
|||
Cat :: struct { |
|||
using animal: Animal, |
|||
litterbox_trained: bool |
|||
} |
|||
Lab :: struct { |
|||
using dog: Dog, |
|||
color: string |
|||
} |
|||
Collie :: struct { |
|||
using dog: Dog, |
|||
catches_frisbee: bool |
|||
} |
|||
main :: proc() { |
|||
pet : Lab |
|||
pet.alive = true |
|||
pet.obedience_trained = true |
|||
pet.color = "yellow" |
|||
}</syntaxhighlight> |
|||
=={{header|Oforth}}== |
|||
<syntaxhighlight lang="oforth">Object Class new: Animal |
|||
Animal Class new: Cat |
|||
Animal Class new: Dog |
|||
Dog Class new: Lab |
|||
Dog Class new: Collie</syntaxhighlight> |
|||
=={{header|ooRexx}}== |
|||
<syntaxhighlight lang="oorexx"> |
|||
-- subclass of object by default |
|||
::class animal |
|||
::class cat subclass animal |
|||
::class dog subclass animal |
|||
::class lab subclass dog |
|||
::class collie subclass dog |
|||
</syntaxhighlight> |
|||
=={{header|OxygenBasic}}== |
|||
<syntaxhighlight lang="oxygenbasic"> |
|||
class animal |
|||
method show() as string |
|||
return "Animal " |
|||
end method |
|||
end Class |
|||
class dog |
|||
from Animal |
|||
method show() as string |
|||
return animal.show()+"dog " |
|||
end method |
|||
end Class |
|||
class cat |
|||
from animal |
|||
method show() as string |
|||
return animal.show()+"cat " |
|||
end method |
|||
end Class |
|||
class Lab |
|||
from dog |
|||
method show() as string |
|||
return dog.show()+"Lab " |
|||
end method |
|||
end Class |
|||
class Collie |
|||
from dog |
|||
method show() as string |
|||
return dog.show()+"Collie " |
|||
end method |
|||
end Class |
|||
Collie c |
|||
print c.show 'result: Animal Dog Collie |
|||
</syntaxhighlight> |
|||
=={{header|Oz}}== |
|||
<syntaxhighlight lang="oz">class Animal |
|||
%% ... |
|||
end |
|||
class Dog from Animal |
|||
%% ... |
|||
end |
|||
class Lab from Dog |
|||
%% ... |
|||
end |
|||
class Collie from Dog |
|||
%% ... |
|||
end |
|||
class Cat from Animal |
|||
%% ... |
|||
end</syntaxhighlight> |
|||
=={{header|Pascal}}== |
|||
See [[Inheritance/Single#Delphi | Delphi]] |
|||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
type |
|||
Animal = class end; |
|||
Dog = class(Animal) end; |
|||
Cat = class(Animal) end; |
|||
Lab = class(Dog) end; |
|||
Collie = class(Dog) end; |
|||
</syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>package Animal; |
|||
<syntaxhighlight lang="perl"> |
|||
package Animal; |
|||
#functions go here... |
#functions go here... |
||
1; |
1; |
||
package Dog; |
|||
use Animal; |
use Animal; |
||
@ISA = qw( Animal ); |
@ISA = qw( Animal ); |
||
#functions go here... |
#functions go here... |
||
1; |
1; |
||
package Cat; |
|||
use Animal; |
use Animal; |
||
@ISA = qw( Animal ); |
@ISA = qw( Animal ); |
||
#functions go here... |
#functions go here... |
||
1; |
1; |
||
package Lab; |
|||
use Dog; |
use Dog; |
||
@ISA = qw( Dog ); |
@ISA = qw( Dog ); |
||
#functions go here... |
#functions go here... |
||
1; |
1; |
||
package Collie; |
|||
use Dog; |
use Dog; |
||
@ISA = qw( Dog ); |
@ISA = qw( Dog ); |
||
#functions go here... |
#functions go here... |
||
1; |
1; |
||
# The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module: |
|||
use MooseX::Declare; |
|||
class Animal { |
|||
# methods go here... |
|||
} |
|||
class Dog extends Animal { |
|||
# methods go here... |
|||
} |
|||
class Cat extends Animal { |
|||
# methods go here... |
|||
} |
|||
class Lab extends Dog { |
|||
# methods go here... |
|||
} |
|||
class Collie extends Dog { |
|||
# methods go here... |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Phix}}== |
|||
{{libheader|Phix/Class}} |
|||
Add (private|public) fields and methods as needed. Make Animal and Dog abstract (ie use "abstract class") to prevent instantiation. |
|||
<!--(notonline)--> |
|||
<syntaxhighlight lang="phix"> |
|||
without js -- (class) |
|||
class Animal |
|||
private string species |
|||
end class |
|||
class Dog extends Animal |
|||
public procedure bark() |
|||
puts(1,"woof\n") |
|||
end procedure |
|||
end class |
|||
class Lab extends Dog end class |
|||
class Collie extends Dog end class |
|||
class Cat extends Animal end class |
|||
</syntaxhighlight> |
|||
=={{header|PHP}}== |
|||
<syntaxhighlight lang="php">class Animal { |
|||
// functions go here... |
|||
} |
|||
class Dog extends Animal { |
|||
// functions go here... |
|||
} |
|||
class Cat extends Animal { |
|||
// functions go here... |
|||
} |
|||
class Lab extends Dog { |
|||
// functions go here... |
|||
} |
|||
class Collie extends Dog { |
|||
// functions go here... |
|||
}</syntaxhighlight> |
|||
=={{header|PicoLisp}}== |
|||
<syntaxhighlight lang="picolisp">(class +Animal) |
|||
(class +Dog +Animal) |
|||
(class +Cat +Animal) |
|||
(class +Lab +Dog) |
|||
(class +Collie +Dog)</syntaxhighlight> |
|||
<syntaxhighlight lang="picolisp">: (dep '+Animal) |
|||
+Animal |
|||
+Cat |
|||
+Dog |
|||
+Collie |
|||
+Lab</syntaxhighlight> |
|||
=={{header|PowerShell}}== |
|||
{{works with|PowerShell|5}} |
|||
<syntaxhighlight lang="powershell"> |
|||
class Animal {} |
|||
class Dog : Animal {} |
|||
class Cat: Animal {} |
|||
class Lab : Dog {} |
|||
class Collie : Dog {} |
|||
</syntaxhighlight> |
|||
=={{header|PureBasic}}== |
|||
Although PureBasic is mostly used for procedural coding it has both the ability to interact with object oriented libraries and code and also the capacity to write it if needed. |
|||
===Native version=== |
|||
<syntaxhighlight lang="purebasic">Interface Animal |
|||
Eat() |
|||
Sleep() |
|||
EndInterface |
|||
Interface Cat Extends Animal |
|||
ChaseMouse() |
|||
EndInterface |
|||
Interface Dog Extends Animal |
|||
Bark() |
|||
WagTail() |
|||
EndInterface |
|||
Interface Lab Extends Dog |
|||
Swim() |
|||
EndInterface |
|||
Interface Collie Extends Dog |
|||
HeardSheep() |
|||
EndInterface</syntaxhighlight> |
|||
===Simple OOP Version=== |
|||
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP]. |
|||
<syntaxhighlight lang="purebasic">Class Animal |
|||
EndClass |
|||
Class Dog Extends Animal |
|||
Public Method Bark() |
|||
EndMethod |
|||
EndClass |
|||
Class Cat Extends Animal |
|||
Public Method Sleep() |
|||
EndMethod |
|||
EndClass |
|||
Class Lab Extends Dog |
|||
Public Method Swim() |
|||
EndMethod |
|||
EndClass |
|||
Class Collie Extends Dog |
|||
Public Method Fetch() |
|||
EndMethod |
|||
EndClass |
|||
;- test the code |
|||
*Lassie.Collie = NewObject.Collie |
|||
*Lassie\Bark() |
|||
*Lassie\Fetch()</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Unrevised style classes: |
|||
<python>class Animal: |
|||
<syntaxhighlight lang="python">class Animal: |
|||
pass #functions go here...</python> |
|||
pass #functions go here... |
|||
class Dog(Animal): |
|||
pass #functions go here... |
pass #functions go here... |
||
class Cat(Animal): |
|||
pass #functions go here... |
pass #functions go here... |
||
class Lab(Dog): |
|||
pass #functions go here... |
pass #functions go here... |
||
class Collie(Dog): |
|||
pass #functions go here...</ |
pass #functions go here...</syntaxhighlight> |
||
New style classes: |
|||
=={{header|Smalltalk}}== |
|||
<syntaxhighlight lang="python">import time |
|||
This is an example of the object serialization format used by many varieties of Smalltalk. Normally the class tree would be defined and navigated via a class browser within a graphical Smalltalk environment. |
|||
Object subclass: #Animal |
|||
class Animal(object): |
|||
instanceVariableNames: ' ' "* space separated list of names *" |
|||
def __init__(self, birth=None, alive=True): |
|||
classVariableNames: ' ' |
|||
self.birth = birth if birth else time.time() |
|||
poolDictionaries: ' ' |
|||
self.alive = alive |
|||
category: ' ' ! |
|||
def age(self): |
|||
return time.time() - self.birth |
|||
def kill(self): |
|||
self.alive = False |
|||
class Dog(Animal): |
|||
def __init__(self, bones_collected=0, **kwargs): |
|||
self.bone_collected = bones_collected |
|||
super(Dog, self).__init__(**kwargs) |
|||
class Cat(Animal): |
|||
max_lives = 9 |
|||
def __init__(self, lives=max_lives, **kwargs): |
|||
self.lives = lives |
|||
super(Cat, self).__init__(**kwargs) |
|||
def kill(self): |
|||
if self.lives>0: |
|||
self.lives -= 1 |
|||
if self.lives == 0: |
|||
super(Cat, self).kill() |
|||
else: |
|||
raise ValueError |
|||
return self |
|||
class Labrador(Dog): |
|||
def __init__(self, guide_dog=False, **kwargs): |
|||
self.guide_dog=False |
|||
super(Labrador, self).__init__(**kwargs) |
|||
class Collie(Dog): |
|||
def __init__(self, sheep_dog=False, **kwargs): |
|||
self.sheep_dog=False |
|||
super(Collie, self).__init__(**kwargs) |
|||
lassie = Collie() |
|||
felix = Cat() |
|||
felix.kill().kill().kill() |
|||
mr_winkle = Dog() |
|||
buddy = Labrador() |
|||
buddy.kill() |
|||
print(f"Felix has {felix.lives} lives, Buddy is {'not' if buddy.alive else 'not'} alive!") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Felix has 6 lives, Buddy is not alive! |
|||
</pre> |
|||
=={{header|R}}== |
|||
===S3=== |
|||
Inheritance is implemented by setting the object's class attribute with a character vector. |
|||
<syntaxhighlight lang="r">aCollie <- "woof" |
|||
class(aCollie) <- c("Collie", "Dog", "Animal")</syntaxhighlight> |
|||
===S4=== |
|||
Inheritance is implemented by using the 'contains' argument in setClass |
|||
<syntaxhighlight lang="r">setClass("Animal", representation(), prototype()) |
|||
setClass("Dog", representation(), prototype(), contains="Animal") |
|||
setClass("Cat", representation(), prototype(), contains="Animal") |
|||
setClass("Collie", representation(), prototype(), contains="Dog") |
|||
setClass("Lab", representation(), prototype(), contains="Dog")</syntaxhighlight> |
|||
=={{header|Racket}}== |
|||
<syntaxhighlight lang="racket"> |
|||
#lang racket |
|||
(define animal% (class object% (super-new))) |
|||
(define dog% (class animal% (super-new))) |
|||
(define cat% (class animal% (super-new))) |
|||
(define lab% (class dog% (super-new))) |
|||
(define collie% (class dog% (super-new))) |
|||
;; unit tests |
|||
(require rackunit) |
|||
(check-true (is-a? (new dog%) animal%)) |
|||
(check-false (is-a? (new collie%) cat%)) |
|||
</syntaxhighlight> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2015-09-16}} |
|||
<syntaxhighlight lang="raku" line>class Animal {} |
|||
class Dog is Animal {} |
|||
class Cat is Animal {} |
|||
class Lab is Dog {} |
|||
class Collie is Dog {} |
|||
say Collie.^parents; # undefined type object |
|||
say Collie.new.^parents; # instantiated object</syntaxhighlight> |
|||
{{out}} |
|||
<pre>((Dog) (Animal)) |
|||
((Dog) (Animal))</pre> |
|||
The <tt>.^parents</tt> notation indicates a method call to the object's metaobject rather than to the object itself. |
|||
=={{header|REBOL}}== |
|||
<syntaxhighlight lang="rebol">REBOL [ |
|||
Title: "Inheritance" |
|||
URL: http://rosettacode.org/wiki/Inheritance |
|||
] |
|||
; REBOL provides subclassing through its prototype mechanism: |
|||
Animal: make object! [ |
|||
legs: 4 |
|||
] |
|||
Dog: make Animal [ |
|||
says: "Woof!" |
|||
] |
|||
Cat: make Animal [ |
|||
says: "Meow..." |
|||
] |
|||
Lab: make Dog [] |
|||
Collie: make Dog [] |
|||
; Demonstrate inherited properties: |
|||
print ["Cat has" Cat/legs "legs."] |
|||
print ["Lab says:" Lab/says]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Cat has 4 legs. |
|||
Lab says: Woof!</pre> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
Class Animal |
|||
Class Dog from Animal |
|||
Class Cat from Animal |
|||
Class Lab from Dog |
|||
Class Collie from Dog |
|||
</syntaxhighlight> |
|||
=={{header|Ruby}}== |
|||
<code>inherited</code> is a method defined on an instance of a <code>Class</code> object. It is invoked when a new subclass of the current class is defined (i.e. at the <code>end</code> statement of a <code>class</code> definition). |
|||
<syntaxhighlight lang="ruby">class Animal |
|||
#functions go here... |
|||
def self.inherited(subclass) |
|||
puts "new subclass of #{self}: #{subclass}" |
|||
end |
|||
end |
|||
class Dog < Animal |
|||
#functions go here... |
|||
end |
|||
class Cat < Animal |
|||
#functions go here... |
|||
end |
|||
class Lab < Dog |
|||
#functions go here... |
|||
end |
|||
class Collie < Dog |
|||
#functions go here... |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>new subclass of Animal: Dog |
|||
new subclass of Dog: Lab |
|||
new subclass of Dog: Collie |
|||
new subclass of Animal: Cat</pre> |
|||
=={{header|Rust}}== |
|||
A type can't inherit properties from other types, but it can implmement any number of traits, which may themselves be subtraits of other traits. |
|||
<syntaxhighlight lang="rust">trait Animal {} |
|||
trait Cat: Animal {} |
|||
trait Dog: Animal {} |
|||
trait Lab: Dog {} |
|||
trait Collie: Dog {}</syntaxhighlight> |
|||
=={{header|Scala}}== |
|||
Scala has both classes and traits. Classes can only be singly inherited, but both |
|||
can inherit a trait multiple times. |
|||
This inheritance can be declared at the point |
|||
of instantiation as well, precluding the need to declare a trait or class for the |
|||
sole purpose of combining traits. |
|||
For the simple inheritance chain of this task, |
|||
any (or all) of the <code>class</code> keywords below can be replaced with <code>trait</code> |
|||
<syntaxhighlight lang="scala">class Animal |
|||
class Dog extends Animal |
|||
class Cat extends Animal |
|||
class Lab extends Dog |
|||
class Collie extends Dog</syntaxhighlight> |
|||
=={{header|Seed7}}== |
|||
[http://seed7.sourceforge.net/manual/objects.htm Seed7 object orientation] is based on interface types and implementation types. |
|||
The example below defines a hierarchy of implementation types. |
|||
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
|||
const type: Animal is new struct |
|||
# ... |
|||
end struct; |
|||
const type: Dog is sub Animal struct |
|||
"* declare methods here, separated with '!' *" |
|||
# ... |
|||
"* !Animal methodsFor: 'a category'! *" |
|||
end struct; |
|||
"* methodName *" |
|||
"* method body! ! |
|||
const type: Lab is sub Dog struct |
|||
!Animal subclass: #Dog |
|||
# ... |
|||
end struct; |
|||
const type: Collie is sub Dog struct |
|||
!Animal subclass: #Cat |
|||
# ... |
|||
end struct; |
|||
const type: Cat is sub Animal struct |
|||
!Dog subclass: #Lab |
|||
# ... |
|||
end struct;</syntaxhighlight> |
|||
=={{header|Self}}== |
|||
Self is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This is an example of the relevant excerpts from a Self transporter fileout. Normally the object tree would be built and navigated within the graphical Self environment. |
|||
<syntaxhighlight lang="self"> |
|||
animal = () |
|||
dog = (| parent* = animal |) |
|||
cat = (| parent* = animal |) |
|||
lab = (| parent* = dog |) |
|||
collie = (| parent* = dog |) |
|||
</syntaxhighlight> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">class Animal {}; |
|||
class Dog << Animal {}; |
|||
class Cat << Animal {}; |
|||
class Lab << Dog {}; |
|||
class Collie << Dog {};</syntaxhighlight> |
|||
=={{header|Simula}}== |
|||
<syntaxhighlight lang="simula">begin |
|||
class Animal; |
|||
! instance variables; |
|||
begin |
|||
! methods; |
|||
end; |
|||
Animal class Dog; |
|||
begin |
|||
end; |
|||
Animal class Cat; |
|||
begin |
|||
end; |
|||
Dog class Lab; |
|||
begin |
|||
end; |
|||
Dog class Collie; |
|||
begin |
|||
end; |
|||
end</syntaxhighlight> |
|||
=={{header|Slate}}== |
|||
<syntaxhighlight lang="slate">define: #Animal &parents: {Cloneable}. |
|||
!Dog subclass: #Collie |
|||
define: #Dog &parents: {Animal}. |
|||
define: #Cat &parents: {Animal}. |
|||
define: #Lab &parents: {Dog}. |
|||
define: #Collie &parents: {Dog}.</syntaxhighlight> |
|||
=={{header|Smalltalk}}== |
|||
This is an example of the object serialization format used by many varieties of Smalltalk. Normally the class tree would be defined and navigated via a class browser within a graphical Smalltalk environment. |
|||
<syntaxhighlight lang="smalltalk">Object subclass: #Animal |
|||
instanceVariableNames: ' ' "* space separated list of names *" |
|||
classVariableNames: ' ' |
|||
poolDictionaries: ' ' |
|||
category: ' ' ! |
|||
"* declare methods here, separated with '!' *" |
|||
"* !Animal methodsFor: 'a category'! *" |
|||
"* methodName *" |
|||
"* method body! !" |
|||
!Animal subclass: #Dog |
|||
"* etc. *" ! |
"* etc. *" ! |
||
!Animal subclass: #Cat |
|||
"* etc. *" ! |
|||
!Dog subclass: #Lab |
|||
"* etc. *" ! |
|||
!Dog subclass: #Collie |
|||
"* etc. *" !</syntaxhighlight> |
|||
=={{header|Swift}}== |
|||
<syntaxhighlight lang="swift">class Animal { |
|||
// ... |
|||
} |
|||
class Dog : Animal { |
|||
// ... |
|||
} |
|||
class Lab : Dog { |
|||
// ... |
|||
} |
|||
class Collie : Dog { |
|||
// ... |
|||
} |
|||
class Cat : Animal { |
|||
// ... |
|||
}</syntaxhighlight> |
|||
=={{header|Tcl}}== |
|||
{{works with|Tcl|8.6}} or {{libheader|TclOO}} |
|||
<syntaxhighlight lang="tcl">package require TclOO |
|||
oo::class create Animal { |
|||
# ... |
|||
} |
|||
oo::class create Dog { |
|||
superclass Animal |
|||
# ... |
|||
} |
|||
oo::class create Cat { |
|||
superclass Animal |
|||
# ... |
|||
} |
|||
oo::class create Collie { |
|||
superclass Dog |
|||
# ... |
|||
} |
|||
oo::class create Lab { |
|||
superclass Dog |
|||
# ... |
|||
}</syntaxhighlight> |
|||
=={{header|TXR}}== |
|||
====Inheritance among symbolic exception tags==== |
|||
<syntaxhighlight lang="txr">@(defex cat animal) |
|||
@(defex lab dog animal) |
|||
@(defex collie dog)</syntaxhighlight> |
|||
The second line is a shorthand which defines a lab to be a kind of dog, and at the same time a dog to be a kind of animal. |
|||
If we throw an exception of type <code>lab</code>, it can be caught in a catch for a <code>dog</code> or for an <code>animal</code>. Continuing with the query: |
|||
<syntaxhighlight lang="txr">@(try) |
|||
@ (throw lab "x") |
|||
@(catch animal (arg)) |
|||
@(end)</syntaxhighlight> |
|||
{{out}} Test: |
|||
<pre>$ txr dog-cat.txr |
|||
arg="x"</pre> |
|||
====OOP Inheritance in TXR Lisp==== |
|||
<syntaxhighlight lang="txrlisp">(defstruct animal nil |
|||
name |
|||
(:method get-name (me) |
|||
(if me.name me.name (error `get-name: animal @me has no name`))) |
|||
(:method speak (me stream) |
|||
(error "abstract animal cannot speak"))) |
|||
(defstruct dog animal |
|||
(:method speak (me : (stream *stdout*)) |
|||
(put-line `@{me.(get-name)}: bark!` stream))) |
|||
(defstruct cat animal |
|||
(:method speak (me : (stream *stdout*)) |
|||
(put-line `@{me.(get-name)}: meow!` stream))) |
|||
(defstruct lab dog) |
|||
(defstruct collie dog) |
|||
(let ((pet1 (new collie name "Lassie")) |
|||
(pet2 (new cat name "Max"))) |
|||
pet1.(speak) |
|||
pet2.(speak))</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Lassie: bark! |
|||
Max: meow!</pre> |
|||
=={{header|Visual Basic .NET}}== |
|||
<syntaxhighlight lang="vbnet">Class Animal |
|||
' ... |
|||
End Class |
|||
Class Dog |
|||
Inherits Animal |
|||
' ... |
|||
End Class |
|||
Class Lab |
|||
Inherits Dog |
|||
' ... |
|||
End Class |
|||
Class Collie |
|||
Inherits Dog |
|||
' ... |
|||
End Class |
|||
Class Cat |
|||
Inherits Animal |
|||
' ... |
|||
End Class</syntaxhighlight> |
|||
=={{header|Vorpal}}== |
|||
<syntaxhighlight lang="vorpal">pet = new() |
|||
cat = new(pet) |
|||
dog = new(pet) |
|||
fido = new(dog) |
|||
felix = new(cat)</syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">class Animal { |
|||
// methods |
|||
} |
|||
class Dog is Animal { |
|||
// methods |
|||
} |
|||
class Cat is Animal { |
|||
// mwethods |
|||
} |
|||
class Labrador is Dog { |
|||
// methods |
|||
} |
|||
class Collie is Dog { |
|||
// methods |
|||
}</syntaxhighlight> |
|||
=={{header|XLISP}}== |
|||
<syntaxhighlight lang="lisp">(define-class animal) |
|||
(define-class dog |
|||
(super-class animal)) |
|||
(define-class cat |
|||
(super-class animal)) |
|||
(define-class collie |
|||
(super-class dog)) |
|||
(define-class lab |
|||
(super-class dog))</syntaxhighlight> |
|||
A REPL session: |
|||
<syntaxhighlight lang="lisp">[1] (cat 'superclass) |
|||
#<Class:ANIMAL #x57094c8> |
|||
[2] (collie 'superclass) |
|||
#<Class:DOG #x57094c8> |
|||
[3] (animal 'superclass) |
|||
#<Class:OBJECT #x57094c8> |
|||
[4] (dog 'show) |
|||
Object is #<Class:DOG #x57094c8>, Class is #<Class:CLASS #x57094c8> |
|||
Instance variables: |
|||
NAME = DOG |
|||
MESSAGES = () |
|||
IVARS = () |
|||
CVARS = #<Environment #x5879788> |
|||
SUPERCLASS = #<Class:ANIMAL #x57094c8> |
|||
IVARCNT = 0 |
|||
IVARTOTAL = 0 |
|||
#<Class:DOG #x57094c8></syntaxhighlight> |
|||
=={{header|zkl}}== |
|||
<syntaxhighlight lang="zkl">class Animal{} |
|||
class Dog(Animal){} class Cat(Animal){} |
|||
class Lab(Dog){} class Collie(Dog){} |
|||
Collie.linearizeParents</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
L(Class(Collie),Class(Dog),Class(Animal)) |
|||
</pre> |
|||
{{omit from|6502 Assembly}} |
|||
{{omit from|68000 Assembly}} |
|||
{{omit from|8080 Assembly}} |
|||
{{omit from|8086 Assembly}} |
|||
{{Omit From|ALGOL 68|It isn't immediately obvious that ALGOL 68 is object oriented.}} |
|||
{{omit from|ARM Assembly}} |
|||
{{Omit From|AWK}} |
|||
{{omit from|Axe}} |
|||
{{omit from|Batch File|Not an OO language.}} |
|||
{{omit from|M4}} |
|||
{{omit from|Mathematica}} |
|||
{{omit from|Maxima}} |
|||
{{Omit From|Metafont}} |
|||
{{omit from|MIPS Assembly}} |
|||
{{omit from|ML/I}} |
|||
{{omit from|Modula-2}} |
|||
{{omit from|PARI/GP}} |
|||
{{Omit From|TI-83 BASIC}} |
|||
{{Omit From|TI-89 BASIC}} |
|||
{{omit from|Z80 Assembly}} |
Latest revision as of 00:51, 24 June 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- This task is about derived types; for implementation inheritance, see Polymorphism.
Inheritance is an operation of type algebra that creates a new type from one or several parent types.
The obtained type is called derived type.
It inherits some of the properties of its parent types.
Usually inherited properties are:
- methods
- components
- parts of the representation
The class of the new type is a subclass of the classes rooted in the parent types.
When all (in certain sense) properties of the parents are preserved by the derived type, it is said to be a Liskov subtype.
When properties are preserved then the derived type is substitutable for its parents in all contexts. Usually full substitutability is achievable only in some contexts.
Inheritance is
- single, when only one parent is allowed
- multiple, otherwise
Some single inheritance languages usually allow multiple inheritance for certain abstract types, interfaces in particular.
Inheritance can be considered as a relation parent-child.
Parent types are sometimes called supertype, the derived ones are subtype. This relation is transitive and reflexive.
Types bound by the relation form a wp:Directed_acyclic_graph directed acyclic graph (ignoring reflexivity).
With single inheritance it becomes a tree.
- Task
Show a tree of types which inherit from each other.
- At the top of the tree should be a class called Animal.
- The second level should have Dog and Cat.
- Under Dog should be Lab and Collie.
- None of the classes need to have any functions, the only thing they need to do is inherit from the specified superclasses
(overriding functions should be shown in Polymorphism).
The tree should look like this:
Animal /\ / \ / \ Dog Cat /\ / \ / \ Lab Collie
11l
T Animal
{
}
T Dog(Animal)
{
}
T Cat(Animal)
{
}
T Lab(Dog)
{
}
T Collie(Dog)
{
}
ActionScript
public class Animal {
// ...
}
public class Cat extends Animal {
// ...
}
public class Dog extends Animal {
// ...
}
public class Lab extends Dog {
// ...
}
public class Collie extends Dog {
// ...
}
Ada
package Inheritance is
type Animal is tagged private;
type Dog is new Animal with private;
type Cat is new Animal with private;
type Lab is new Dog with private;
type Collie is new Dog with private;
private
type Animal is tagged null record;
type Dog is new Animal with null record;
type Cat is new Animal with null record;
type Lab is new Dog with null record;
type Collie is new Dog with null record;
end Inheritance;
Aikido
class Animal{
//functions go here...
}
class Dog extends Animal {
//functions go here...
}
class Cat extends Animal {
//functions go here...
}
class Lab extends Dog {
//functions go here...
}
class Collie extends Dog {
//functions go here...
}
AmigaE
OBJECT animal
ENDOBJECT
OBJECT dog OF animal
ENDOBJECT
OBJECT cat OF animal
ENDOBJECT
OBJECT lab OF dog
ENDOBJECT
OBJECT collie OF dog
ENDOBJECT
AppleScript
script Animal
end script
script Dog
property parent : Animal
end script
script Cat
property parent : Animal
end script
script Lab
property parent : Dog
end script
script Collie
property parent : Dog
end script
AutoHotkey
AutoHotkey_L is prototype-based. However, for convenience, class-syntax may be used to create a base object.
dog := new Collie
MsgBox, % "A " dog.__Class " is a " dog.base.base.__Class " and is part of the " dog.kingdom " kingdom."
class Animal {
static kingdom := "Animalia" ; Class variable
}
class Dog extends Animal {
}
class Cat extends Animal {
}
class Lab extends Dog {
}
class Collie extends Dog {
}
BBC BASIC
INSTALL @lib$+"CLASSLIB"
DIM Animal{method}
PROC_class(Animal{})
DIM Cat{method}
PROC_inherit(Cat{}, Animal{})
PROC_class(Cat{})
DIM Dog{method}
PROC_inherit(Dog{}, Animal{})
PROC_class(Dog{})
DIM Labrador{method}
PROC_inherit(Labrador{}, Dog{})
PROC_class(Labrador{})
DIM Collie{method}
PROC_inherit(Collie{}, Dog{})
PROC_class(Collie{})
C
- See Inheritance/C
C#
class Animal
{
/* ... */
// ...
}
class Dog : Animal
{
/* ... */
// ...
}
class Lab : Dog
{
/* ... */
// ...
}
class Collie : Dog
{
/* ... */
// ...
}
class Cat : Animal
{
/* ... */
// ...
}
C++
class Animal
{
// ...
};
class Dog: public Animal
{
// ...
};
class Lab: public Dog
{
// ...
};
class Collie: public Dog
{
// ...
};
class Cat: public Animal
{
// ...
};
ChucK
public class Drums{
//functions go here...
}
public class LatinKit extends Drums{
//functions go here...
}
public class ElectronicKit extends Drums{
//functions go here...
}
public class Congas extends LatinKit{
//functions go here...
}
public class TechnoDrums extends ElectronicKit{
//functions go here...
}
Clojure
This is not very useful in clojure
(gen-class :name Animal)
(gen-class :name Dog :extends Animal)
(gen-class :name Cat :extends Animal)
(gen-class :name Lab :extends Dog)
(gen-class :name Collie :extends Dog)
More useful:
(derive ::dog ::animal)
(derive ::cat ::animal)
(derive ::lab ::dog)
(derive ::collie ::dog)
use:
user> (isa? ::dog ::animal)
true
user> (isa? ::dog ::cat)
false
user> (isa? ::collie ::animal)
true
COBOL
IDENTIFICATION DIVISION.
CLASS-ID. Animal.
*> ...
END CLASS Animal.
IDENTIFICATION DIVISION.
CLASS-ID. Dog
INHERITS FROM Animal.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Animal.
*> ...
END CLASS Dog.
IDENTIFICATION DIVISION.
CLASS-ID. Cat
INHERITS FROM Animal.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Animal.
*> ...
END CLASS Cat.
IDENTIFICATION DIVISION.
CLASS-ID. Lab
INHERITS FROM Dog.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Dog.
*> ...
END CLASS Lab.
IDENTIFICATION DIVISION.
CLASS-ID. Collie
INHERITS FROM Dog.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Dog.
*> ...
END CLASS Collie.
Coco
class Animal
class Cat extends Animal
class Dog extends Animal
class Lab extends Dog
class Collie extends Dog
On the subject of inheritance, it is worth noting that Coco's super
works differently from CoffeeScript's. In particular, the constructor of a subclass should generally say super ...
, not just super
. Here is a translation of the example from the CoffeeScript documentation:
class Animal
(@name) ->
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
-> super ...
move: ->
alert 'Slithering...'
super 5
class Horse extends Animal
-> super ...
move: ->
alert 'Galloping...'
super 45
sam = new Snake 'Sammy the Python'
tom = new Horse 'Tommy the Palomino'
sam.move!
tom.move!
Comal
STRUC Animal
DIM Species$ OF 20
ENDSTRUC Animal
STRUC Dog
INHERIT Animal
DIM Race$ OF 20
FUNC New CONSTRUCTOR
Species$="Dog"
ENDFUNC New
ENDSTRUC Dog
STRUC Cat
INHERIT Animal
DIM Race$ OF 20
FUNC New CONSTRUCTOR
Species$="Cat"
ENDFUNC New
ENDSTRUC Cat
STRUC Lab
INHERIT Dog
FUNC New CONSTRUCTOR
Race$:="Lab"
ENDFUNC New
ENDSTRUC Lab
STRUC Collie
INHERIT Dog
FUNC New CONSTRUCTOR
Race$:="Collie"
ENDFUNC New
ENDSTRUC Collie
Common Lisp
Using CLOS classes, we have the following:
(defclass animal () ())
(defclass dog (animal) ())
(defclass lab (dog) ())
(defclass collie (dog) ())
(defclass cat (animal) ())
Alternatively, since there is no multiple inheritance in the task requirement, structures could also be used:
(defstruct animal)
(defstruct (dog (:include animal)))
(defstruct (lab (:include dog)))
(defstruct (collie (:include dog)))
(defstruct (cat (:include animal)))
(Structures are less flexible than CLOS objects but often somewhat more efficiently implemented, due to those restrictions.)
Inheritance is not required for object-oriented programming in Lisp. It is used for code reuse, because it allows common utilities and protocol conventions to be factored out into base class methods. However, a class doesn't have to inherit from a base class just so that some existing methods can work with instances of that class.
Furthermore, all of the "basic types" also have a class, so methods can be readily specialized to lists, integers, strings, symbols, et cetera. This is done without having to modify any class definitions.
;;; ASN.1 serialization logic specialized for animal class
(defmethod serialize-to-asn-1 ((a animal))
#| ... |#
)
;;; casually introduce the method over strings too; no relation to animal
(defmethod serialize-to-asn-1 ((s string))
#| ... #|
)
These classes do not have to inherit from some interface or base class which provides a prototype for the serialize-to-asn-1 method. Such a requirement has more to do with static typing than object oriented programming. Usually in languages which require such inheritance, there are also statically typed references. A class must conform to some "ASNEncodable" class so that its instances can be passed to functions which expect references to an ASN1Encodable type, which is verified at compile time.
Component Pascal
TYPE
Animal = ABSTRACT RECORD (* *) END;
Cat = RECORD (Animal) (* *) END; (* final record (cannot be extended) - by default *)
Dog = EXTENSIBLE RECORD (Animal) (* *) END; (* extensible record *)
Lab = RECORD (Dog) (* *) END;
Collie = RECORD (Dog) (* *) END;
D
class Animal {
// ...
}
class Dog: Animal {
// ...
}
class Lab: Dog {
// ...
}
class Collie: Dog {
// ...
}
class Cat: Animal {
// ...
}
void main() {}
Delphi
type
Animal = class(TObject)
private
// private functions/variables
public
// public functions/variables
end;
Dog = class(Animal);
Cat = class(Animal);
Collie = class(Dog);
Lab = class(Dog);
DWScript
type
Animal = class(TObject)
private
// private functions/variables
public
// public functions/variables
end;
type Dog = class(Animal) end;
type Cat = class(Animal) end;
type Collie = class(Dog) end;
type Lab = class(Dog) end;
E
Outside of interactions with the host platform's objects, E does not generally deal in complex type hierarchies; the focus is more on "what guarantees does this object provide", and composition rather than inheritance. However, it is possible to set up a type hierarchy scheme with just a bit of code.
In E, a guard accepts, or coerces, certain objects and rejects others; its range constitutes a type. An auditor examines the implementation of an object and marks it approved; a stamp is an auditor which does no actual checking. Here, we create a guard/stamp pair; the guard accepts every stamped object. The stamp also asks for each supertype's stamp on the objects it audits.
def makeType(label, superstamps) {
def stamp {
to audit(audition) {
for s in superstamps { audition.ask(s) }
return true
}
}
def guard {
to coerce(specimen, ejector) {
if (__auditedBy(stamp, specimen)) {
return specimen
} else {
throw.eject(ejector, `$specimen is not a $label`)
}
}
}
return [guard, stamp]
}
Setting up the task's specified tree:
def [Animal, AnimalStamp] := makeType("Animal", [])
def [Cat, CatStamp] := makeType("Cat", [AnimalStamp])
def [Dog, DogStamp] := makeType("Dog", [AnimalStamp])
def [Lab, LabStamp] := makeType("Lab", [DogStamp])
def [Collie, CollieStamp] := makeType("Collie", [DogStamp])
Some example objects:
def fido implements LabStamp {}
def tom implements CatStamp {}
def brick {} # not an animal
Testing against the types:
? fido :Animal
# value: <fido>
? fido :Cat
# problem: <fido> is not a Cat
? fido :Lab
# value: <fido>
? tom :Animal
# value: <tom>
? tom :Cat
# value: <tom>
? brick :Animal
# problem: <brick> is not a Animal
Eiffel
class
ANIMAL
end
class
DOG
inherit
ANIMAL
end
class
CAT
inherit
ANIMAL
end
class
LAB
inherit
DOG
end
class
COLLIE
inherit
DOG
end
Elena
ELENA 4.x :
class Animal
{
// ...
}
class Dog : Animal
{
// ...
}
class Lab : Dog
{
// ...
}
class Collie : Dog
{
// ...
}
class Cat : Animal
{
// ...
}
EMal
in Org:RosettaCode
type Animal
model do end
type Dog extends Animal
model do end
type Cat extends Animal
model do end
type Lab extends Dog
model do end
type Collie extends Dog
model do end
type Main
var fuffy = Collie()
for each generic kind in generic[Animal, Dog, Cat, Lab, Collie]
writeLine("Fuffy " + when(Generic.check(kind, fuffy), "is", "is not") + " a " + Generic.name(kind))
end
- Output:
Fuffy is a Org:RosettaCode:Animal Fuffy is a Org:RosettaCode:Dog Fuffy is not a Org:RosettaCode:Cat Fuffy is not a Org:RosettaCode:Lab Fuffy is a Org:RosettaCode:Collie
F#
The ()
behind the class names indicates a public default constructor; you need some type of public constructor to derive from a class.
type Animal() =
class // explicit syntax needed for empty class
end
type Dog() =
inherit Animal()
type Lab() =
inherit Dog()
type Collie() =
inherit Dog()
type Cat() =
inherit Animal()
Factor
TUPLE: animal ;
TUPLE: dog < animal ;
TUPLE: cat < animal ;
TUPLE: lab < dog ;
TUPLE: collie < dog ;
Fancy
class Animal {
# ...
}
class Dog : Animal {
# ...
}
class Cat : Animal {
# ...
}
class Lab : Dog {
# ...
}
class Collie : Dog {
# ...
}
Fantom
class Animal
{
}
class Dog : Animal
{
}
class Cat : Animal
{
}
class Lab : Dog
{
}
class Collie : Dog
{
}
Forth
There are numerous, mutually incompatible object oriented frameworks for Forth. This one works with the FOOS preprocessor extension of 4tH.
include 4pp/lib/foos.4pp
:: Animal class end-class {} ;
:: Dog extends Animal end-extends {} ;
:: Cat extends Animal end-extends {} ;
:: Lab extends Dog end-extends {} ;
:: Collie extends Dog end-extends {} ;
Works with any ANS Forth
Needs the FMS2 library code located here: https://github.com/DouglasBHoffman/FMS2
include FMS2LL.f
:class Animal ;class
:class Dog <super Animal ;class
:class Cat <super Animal ;class
:class Lab <super Dog ;class
:class Collie <super Dog ;class
Fortran
OO has been part of the Fortran standard since 2003 but the compilers are still playing catchup. This example builds with the Intel 11.1.069 compiler (free for personal use on linux).
module anim
type animal
end type animal
type, extends(animal) :: dog
end type dog
type, extends(animal) :: cat
end type cat
type, extends(dog) :: lab
end type lab
type, extends(dog) :: collie
end type collie
end module anim
FreeBASIC
' FB 1.05.0 Win64
Type Animal Extends Object ' to enable virtual methods etc. if needed
' ...
End Type
Type Dog Extends Animal
' ...
End Type
Type Cat Extends Animal
' ...
End Type
Type Lab Extends Dog
' ...
End Type
Type Collie Extends Dog
' ...
End Type
Go
Go eschews most trappings of inheritance, yet it's anonymous field feature allows building one struct type upon another and accessing fields of "embedded" types without extra synax.
package main
type animal struct {
alive bool
}
type dog struct {
animal
obedienceTrained bool
}
type cat struct {
animal
litterBoxTrained bool
}
type lab struct {
dog
color string
}
type collie struct {
dog
catchesFrisbee bool
}
func main() {
var pet lab
pet.alive = true
pet.obedienceTrained = false
pet.color = "yellow"
}
Groovy
class Animal{
//contents go here...
}
class Dog extends Animal{
//contents go here...
}
class Cat extends Animal{
//contents go here...
}
class Lab extends Dog{
//contents go here...
}
class Collie extends Dog{
//contents go here...
}
Haskell
A type can't inherit properties from other types, but it can belong to any number of type classes, which may themselves be subclasses of other type classes.
class Animal a
class Animal a => Cat a
class Animal a => Dog a
class Dog a => Lab a
class Dog a => Collie a
Haxe
class Animal {
// ...
}
class Cat extends Animal {
// ...
}
class Dog extends Animal {
// ...
}
class Lab extends Dog {
// ...
}
class Collie extends Dog {
// ...
}
Icon and Unicon
This example only works in Unicon.
class Animal ()
end
class Dog : Animal ()
end
class Cat : Animal ()
end
class Lab : Dog ()
end
class Collie : Dog ()
end
Inform 7
An animal is a kind of thing.
A cat is a kind of animal.
A dog is a kind of animal.
A collie is a kind of dog.
A lab is a kind of dog.
"Animal" is actually a predefined kind in Inform 7, so its definition here is redundant (but legal).
Io
Animal := Object clone
Cat := Animal clone
Dog := Animal clone
Collie := Dog clone
Lab := Dog clone
J
Here is how this would normally be done:
coclass 'Animal'
coclass 'Dog'
coinsert 'Animal'
coclass 'Cat'
coinsert 'Animal'
coclass 'Lab'
coinsert 'Dog'
coclass 'Collie'
coinsert 'Dog'
coclass
specifies that following definitions will be within the named class, and coinsert
specifies that the current class will inherit from the named classes (or object -- in J the only difference between a class and an object is its name and how you can create them -- this motivates the "co" prefix on operations which manipulate classes and objects).
See http://www.jsoftware.com/help/jforc/modular_code.htm
That said, some operations in J -- including coinsert
-- will create classes if they did not already exist. So the above may be simplified to:
coinsert_Dog_ 'Animal'
coinsert_Cat_ 'Animal'
coinsert_Lab_ 'Dog'
coinsert_Collie_ 'Dog'
That said, note that classes and objects are not "types" in J. Instead, they are components of names. In general, when we deal with objects and classes we deal with references to the underlying representation, and in J the references are names, so a collection of classes and objects, in J, would be a collection of names which refer to classes and objects. In other words, the "type" (to the degree that there is a type) would be best thought of as "name" (or, more mechanically: boxed list of characters).
Java
public class Animal{
//functions go here...
}
public class Dog extends Animal{
//functions go here...
}
public class Cat extends Animal{
//functions go here...
}
public class Lab extends Dog{
//functions go here...
}
public class Collie extends Dog{
//functions go here...
}
JavaScript
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance.
function Animal() {
// ...
}
function Dog() {
// ...
}
Dog.prototype = new Animal();
function Cat() {
// ...
}
Cat.prototype = new Animal();
function Collie() {
// ...
}
Collie.prototype = new Dog();
function Lab() {
// ...
}
Lab.prototype = new Dog();
Animal.prototype.speak = function() {print("an animal makes a sound")};
var lab = new Lab();
lab.speak(); // shows "an animal makes a sound"
Julia
Julia is not really an object-oriented programming language. It supports polymorphism and inheriting functionality but not structure. Thus inheritance hierarchies must be made with abstract types. Abstract types can not be instantiated and do not contain any fields. So below Dog is abstract while Collie is a concrete type which may contain fields.
abstract type Animal end
abstract type Dog <: Animal end
abstract type Cat <: Animal end
struct Lab <: Dog end
struct Collie <: Dog end
Kite
class Animal [
#Method goes here
];
class Dog from Animal [
#Method goes here
];
class Lab from Dog [
#Method goes here
];
class collie from Dog [
#Method goes here
];
Kotlin
// version 1.0.6
open class Animal {
override fun toString() = "animal"
}
open class Dog : Animal() {
override fun toString() = "dog"
}
class Cat : Animal() {
override fun toString() = "cat"
}
class Labrador : Dog() {
override fun toString() = "labrador"
}
class Collie : Dog() {
override fun toString() = "collie"
}
fun main(args: Array<String>) {
val felix: Animal = Cat()
val rover: Animal = Dog()
val bella: Dog = Labrador()
val casey: Dog = Collie()
println("Felix is a $felix")
println("Rover is a $rover")
println("Bella is a $bella")
println("Casey is a $casey")
}
- Output:
Felix is a cat Rover is a dog Bella is a labrador Casey is a collie
Lasso
define animal => type {
data public gender::string
}
define dog => type {
parent animal
}
define cat => type {
parent animal
}
define collie => type {
parent dog
}
define lab => type {
parent dog
}
local(myanimal = lab)
#myanimal -> gender = 'Male'
#myanimal -> gender
-> Male
Latitude
Latitude is a prototype-oriented language, so defining a subclass is equivalent to constructing an instance.
Animal ::= Object clone tap {
;; Methods go here...
}.
Dog ::= Animal clone tap {
;; Methods go here...
}.
Cat ::= Animal clone tap {
;; Methods go here...
}.
Lab ::= Dog clone tap {
;; Methods go here...
}.
Collie ::= Dog clone tap {
;; Methods go here...
}.
We clone
the parent and then tap
the new instance to add functionality to it. Note that we use ::=
here rather than the usual :=
, as the former implicitly defines an appropriate toString
method representative of the new "class".
Lingo
In Lingo Classes are represented by "parent scripts". Instead of using new() as in the code below, child classes can also use rawNew() when creating an instance of their parent classes. rawNew() creates an instance of a class without calling its initialization function 'new' (constructor).
-- parent script "Animal"
-- ...
-- parent script "Dog"
property ancestor
on new (me)
me.ancestor = script("Animal").new()
return me
end
-- parent script "Cat"
property ancestor
on new (me)
me.ancestor = script("Animal").new()
return me
end
-- parent script "Lab"
property ancestor
on new (me)
me.ancestor = script("Dog").new()
return me
end
-- parent script "Collie"
property ancestor
on new (me)
me.ancestor = script("Dog").new()
return me
end
Lisaac
Section Header
+ name := ANIMAL;
// ...
Section Header
+ name := CAT;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...
Section Header
+ name := DOG;
Section Inherit
- parent : ANIMAL := ANIMAL;
// ...
Section Header
+ name := LAB;
Section Inherit
- parent : DOG := DOG;
// ...
Section Header
+ name := COLLIE;
Section Inherit
- parent : DOG := DOG;
// ...
Logtalk
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(thing,
instantiates(thing)).
:- end_object.
:- object(animal,
specializes(thing)).
...
:- end_object.
:- object(dog,
specializes(animal)).
...
:- end_object.
:- object(cat,
specializes(animal)).
...
:- end_object.
:- object(lab,
specializes(dog)).
...
:- end_object.
:- object(collie,
specializes(dog)).
...
:- end_object.
Lua
Lua has no in-built formal OOP mechanism, though there are many possible ways of implementing work-alikes.
Class = {
classname = "Class aka Object aka Root-Of-Tree",
new = function(s,t)
s.__index = s
local instance = setmetatable(t or {}, s)
instance.parent = s
return instance
end
}
Animal = Class:new{classname="Animal", speak=function(s) return s.voice or "("..s.classname.." has no voice)" end }
Cat = Animal:new{classname="Cat", voice="meow"}
Dog = Animal:new{classname="Dog", voice="woof"}
Lab = Dog:new{classname="Lab", voice="bark"}
Collie = Dog:new{classname="Collie"} -- subclass without a unique voice
print("Animal:speak(): " .. Animal:speak())
print("Cat:speak(): " .. Cat:speak())
print("Dog:speak(): " .. Dog:speak())
print("Lab:speak(): " .. Lab:speak())
print("Collie:speak(): " .. Collie:speak())
max = Collie:new{voice="Hi, I am Max the talking Collie!"} -- instance with a unique voice
print("max:speak(): " .. max:speak())
print("max himself is (instance): " .. max.classname)
print("max's parent is (class): " .. max.parent.classname)
print("max's parent's parent is (class): " .. max.parent.parent.classname)
print("max's parent's parent's parent is (class): " .. max.parent.parent.parent.classname)
print("max's parent's parent's parent's parent is (class): " .. max.parent.parent.parent.parent.classname)
print("max's parent's parent's parent's parent's parent is (nil reference): " .. tostring(max.parent.parent.parent.parent.parent))
- Output:
Animal:speak(): (Animal has no voice) Cat:speak(): meow Dog:speak(): woof Lab:speak(): bark Collie:speak(): woof max:speak(): Hi, I am Max the talking Collie! max himself is (instance): Collie max's parent is (class): Collie max's parent's parent is (class): Dog max's parent's parent's parent is (class): Animal max's parent's parent's parent's parent is (class): Class aka Object aka Root-Of-Tree max's parent's parent's parent's parent's parent is (nil reference): nil
M2000 Interpreter
Module CheckIt {
Class Animal {
}
Class Dog as Animal {
}
Class Cat as Animal{
}
Class Labrador As Dog {
}
Class Collie As Dog{
}
\\ a is a pointer to a group made from class Labrador
a->Labrador()
Print a is type Animal = True
Print a is type Dog = True
Print a is type Labrador = True
}
CheckIt
Neko
var Animal = $new(null);
var Dog = $new(null);
$objsetproto(Dog, Animal);
var Cat = $new(null);
$objsetproto(Cat, Animal);
var Lab = $new(null);
$objsetproto(Lab, Dog);
var Collie = $new(null);
$objsetproto(Collie, Dog);
Nemerle
class Animal {
// ...
}
class Dog: Animal {
// ...
}
class Lab: Dog {
// ...
}
class Collie: Dog {
// ...
}
class Cat: Animal {
// ...
}
NetRexx
Class names cosmetically augmented slightly to prevent namespace pollution.
For brevity, all classes are defined within the same source file. Normally classes exist as separate source units.
/* NetRexx */
options replace format comments java crossref symbols binary
class RInheritSingle public
method main(args = String[]) public static
animals = [ -
RInheritSingle_Animal(), -
RInheritSingle_Cat(), -
RInheritSingle_Dog(), -
RInheritSingle_Lab(), -
RInheritSingle_Collie() -
]
say 'Object ID'.left(12) 'Class type'.left(24) 'Superclass type'
say '.'.left(12, '.') '.'.left(24, '.') '.'.left(24, '.')
loop animal over animals
parse animal.whatAmI() oid ct st
say oid.left(12) ct.left(24) st
end animal
return
class RInheritSingle_Animal private
properties indirect
whatThatIs = String
whatThisIs = String
method RInheritSingle_Animal() public
-- Animal specific set-up
setWhatThatIs(this.getClass().getSuperclass().getSimpleName())
setWhatThisIs(this.getClass().getSimpleName())
return
method hashToString() public
return '@'(Rexx this.hashCode()).d2x().right(8, 0)
method whatAmI() public
iAmText = hashToString() getWhatThisIs() getWhatThatIs()
return iAmText
class RInheritSingle_Cat private extends RInheritSingle_Animal
method RInheritSingle_Cat() public
-- Do Cat specific set-up
return
class RInheritSingle_Dog private extends RInheritSingle_Animal
method RInheritSingle_Dog() public
-- Do Dog specific set-up
return
class RInheritSingle_Lab private extends RInheritSingle_Dog
method RInheritSingle_Lab() public
-- Do Lab specific set-up
return
class RInheritSingle_Collie private extends RInheritSingle_Dog
method RInheritSingle_Collie() public
-- Do Collie specific set-up
return
- Output:
Object ID Class type Superclass type ............ ........................ ........................ @3F81D405 RInheritSingle_Animal Object @51430296 RInheritSingle_Cat RInheritSingle_Animal @065EEF88 RInheritSingle_Dog RInheritSingle_Animal @42BFCCFC RInheritSingle_Lab RInheritSingle_Dog @3E2AD6A0 RInheritSingle_Collie RInheritSingle_Dog
Nim
type
Animal = object of RootObj
Dog = object of Animal
Cat = object of Animal
Lab = object of Dog
Collie = object of Dog
Oberon
Tested with OBNC.
MODULE Animals;
TYPE
Animal = RECORD END;
Dog = RECORD (Animal) END;
Cat = RECORD (Animal) END;
Lab = RECORD (Dog) END;
Collie = RECORD (Dog) END;
END Animals.
Oberon-2
Works with oo2c Version 2
MODULE Animals;
TYPE
Animal = POINTER TO AnimalDesc;
AnimalDesc = RECORD END;
Cat = POINTER TO CatDesc;
CatDesc = RECORD (AnimalDesc) END;
Dog = POINTER TO DogDesc;
DogDesc = RECORD (AnimalDesc) END;
Lab = POINTER TO LabDesc;
LabDesc = RECORD (DogDesc) END;
Collie = POINTER TO CollieDesc;
CollieDesc = RECORD (DogDesc) END;
END Animals.
Objeck
class Animal
{ #~ ... ~# }
class Dog from Animal
{ #~ ... ~# }
class Lab from Dog
{ #~ ... ~# }
class Collie from Dog
{ #~ ... ~# }
class Cat from Animal
{ #~ ... ~# }
Objective-C
@interface Animal : NSObject
{
// ...
}
// ...
@end
@interface Dog : Animal
{
// ...
}
// ...
@end
@interface Lab : Dog
{
// ...
}
// ...
@end
@interface Collie : Dog
{
// ...
}
// ...
@end
@interface Cat : Animal
{
// ...
}
// ...
@end
OCaml
class animal =
object (self)
(*functions go here...*)
end
class dog =
object (self)
inherit animal
(*functions go here...*)
end
class cat =
object (self)
inherit animal
(*functions go here...*)
end
class lab =
object (self)
inherit dog
(*functions go here...*)
end
class collie =
object (self)
inherit dog
(*functions go here...*)
end
Odin
package main
Animal :: struct {
alive: bool
}
Dog :: struct {
using animal: Animal,
obedience_trained: bool
}
Cat :: struct {
using animal: Animal,
litterbox_trained: bool
}
Lab :: struct {
using dog: Dog,
color: string
}
Collie :: struct {
using dog: Dog,
catches_frisbee: bool
}
main :: proc() {
pet : Lab
pet.alive = true
pet.obedience_trained = true
pet.color = "yellow"
}
Oforth
Object Class new: Animal
Animal Class new: Cat
Animal Class new: Dog
Dog Class new: Lab
Dog Class new: Collie
ooRexx
-- subclass of object by default
::class animal
::class cat subclass animal
::class dog subclass animal
::class lab subclass dog
::class collie subclass dog
OxygenBasic
class animal
method show() as string
return "Animal "
end method
end Class
class dog
from Animal
method show() as string
return animal.show()+"dog "
end method
end Class
class cat
from animal
method show() as string
return animal.show()+"cat "
end method
end Class
class Lab
from dog
method show() as string
return dog.show()+"Lab "
end method
end Class
class Collie
from dog
method show() as string
return dog.show()+"Collie "
end method
end Class
Collie c
print c.show 'result: Animal Dog Collie
Oz
class Animal
%% ...
end
class Dog from Animal
%% ...
end
class Lab from Dog
%% ...
end
class Collie from Dog
%% ...
end
class Cat from Animal
%% ...
end
Pascal
See Delphi
PascalABC.NET
type
Animal = class end;
Dog = class(Animal) end;
Cat = class(Animal) end;
Lab = class(Dog) end;
Collie = class(Dog) end;
Perl
package Animal;
#functions go here...
1;
package Dog;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
package Cat;
use Animal;
@ISA = qw( Animal );
#functions go here...
1;
package Lab;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
package Collie;
use Dog;
@ISA = qw( Dog );
#functions go here...
1;
# The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module:
use MooseX::Declare;
class Animal {
# methods go here...
}
class Dog extends Animal {
# methods go here...
}
class Cat extends Animal {
# methods go here...
}
class Lab extends Dog {
# methods go here...
}
class Collie extends Dog {
# methods go here...
}
Phix
Add (private|public) fields and methods as needed. Make Animal and Dog abstract (ie use "abstract class") to prevent instantiation.
without js -- (class)
class Animal
private string species
end class
class Dog extends Animal
public procedure bark()
puts(1,"woof\n")
end procedure
end class
class Lab extends Dog end class
class Collie extends Dog end class
class Cat extends Animal end class
PHP
class Animal {
// functions go here...
}
class Dog extends Animal {
// functions go here...
}
class Cat extends Animal {
// functions go here...
}
class Lab extends Dog {
// functions go here...
}
class Collie extends Dog {
// functions go here...
}
PicoLisp
(class +Animal)
(class +Dog +Animal)
(class +Cat +Animal)
(class +Lab +Dog)
(class +Collie +Dog)
: (dep '+Animal)
+Animal
+Cat
+Dog
+Collie
+Lab
PowerShell
class Animal {}
class Dog : Animal {}
class Cat: Animal {}
class Lab : Dog {}
class Collie : Dog {}
PureBasic
Although PureBasic is mostly used for procedural coding it has both the ability to interact with object oriented libraries and code and also the capacity to write it if needed.
Native version
Interface Animal
Eat()
Sleep()
EndInterface
Interface Cat Extends Animal
ChaseMouse()
EndInterface
Interface Dog Extends Animal
Bark()
WagTail()
EndInterface
Interface Lab Extends Dog
Swim()
EndInterface
Interface Collie Extends Dog
HeardSheep()
EndInterface
Simple OOP Version
Using the open-source precompiler SimpleOOP.
Class Animal
EndClass
Class Dog Extends Animal
Public Method Bark()
EndMethod
EndClass
Class Cat Extends Animal
Public Method Sleep()
EndMethod
EndClass
Class Lab Extends Dog
Public Method Swim()
EndMethod
EndClass
Class Collie Extends Dog
Public Method Fetch()
EndMethod
EndClass
;- test the code
*Lassie.Collie = NewObject.Collie
*Lassie\Bark()
*Lassie\Fetch()
Python
Unrevised style classes:
class Animal:
pass #functions go here...
class Dog(Animal):
pass #functions go here...
class Cat(Animal):
pass #functions go here...
class Lab(Dog):
pass #functions go here...
class Collie(Dog):
pass #functions go here...
New style classes:
import time
class Animal(object):
def __init__(self, birth=None, alive=True):
self.birth = birth if birth else time.time()
self.alive = alive
def age(self):
return time.time() - self.birth
def kill(self):
self.alive = False
class Dog(Animal):
def __init__(self, bones_collected=0, **kwargs):
self.bone_collected = bones_collected
super(Dog, self).__init__(**kwargs)
class Cat(Animal):
max_lives = 9
def __init__(self, lives=max_lives, **kwargs):
self.lives = lives
super(Cat, self).__init__(**kwargs)
def kill(self):
if self.lives>0:
self.lives -= 1
if self.lives == 0:
super(Cat, self).kill()
else:
raise ValueError
return self
class Labrador(Dog):
def __init__(self, guide_dog=False, **kwargs):
self.guide_dog=False
super(Labrador, self).__init__(**kwargs)
class Collie(Dog):
def __init__(self, sheep_dog=False, **kwargs):
self.sheep_dog=False
super(Collie, self).__init__(**kwargs)
lassie = Collie()
felix = Cat()
felix.kill().kill().kill()
mr_winkle = Dog()
buddy = Labrador()
buddy.kill()
print(f"Felix has {felix.lives} lives, Buddy is {'not' if buddy.alive else 'not'} alive!")
- Output:
Felix has 6 lives, Buddy is not alive!
R
S3
Inheritance is implemented by setting the object's class attribute with a character vector.
aCollie <- "woof"
class(aCollie) <- c("Collie", "Dog", "Animal")
S4
Inheritance is implemented by using the 'contains' argument in setClass
setClass("Animal", representation(), prototype())
setClass("Dog", representation(), prototype(), contains="Animal")
setClass("Cat", representation(), prototype(), contains="Animal")
setClass("Collie", representation(), prototype(), contains="Dog")
setClass("Lab", representation(), prototype(), contains="Dog")
Racket
#lang racket
(define animal% (class object% (super-new)))
(define dog% (class animal% (super-new)))
(define cat% (class animal% (super-new)))
(define lab% (class dog% (super-new)))
(define collie% (class dog% (super-new)))
;; unit tests
(require rackunit)
(check-true (is-a? (new dog%) animal%))
(check-false (is-a? (new collie%) cat%))
Raku
(formerly Perl 6)
class Animal {}
class Dog is Animal {}
class Cat is Animal {}
class Lab is Dog {}
class Collie is Dog {}
say Collie.^parents; # undefined type object
say Collie.new.^parents; # instantiated object
- Output:
((Dog) (Animal)) ((Dog) (Animal))
The .^parents notation indicates a method call to the object's metaobject rather than to the object itself.
REBOL
REBOL [
Title: "Inheritance"
URL: http://rosettacode.org/wiki/Inheritance
]
; REBOL provides subclassing through its prototype mechanism:
Animal: make object! [
legs: 4
]
Dog: make Animal [
says: "Woof!"
]
Cat: make Animal [
says: "Meow..."
]
Lab: make Dog []
Collie: make Dog []
; Demonstrate inherited properties:
print ["Cat has" Cat/legs "legs."]
print ["Lab says:" Lab/says]
- Output:
Cat has 4 legs. Lab says: Woof!
Ring
Class Animal
Class Dog from Animal
Class Cat from Animal
Class Lab from Dog
Class Collie from Dog
Ruby
inherited
is a method defined on an instance of a Class
object. It is invoked when a new subclass of the current class is defined (i.e. at the end
statement of a class
definition).
class Animal
#functions go here...
def self.inherited(subclass)
puts "new subclass of #{self}: #{subclass}"
end
end
class Dog < Animal
#functions go here...
end
class Cat < Animal
#functions go here...
end
class Lab < Dog
#functions go here...
end
class Collie < Dog
#functions go here...
end
- Output:
new subclass of Animal: Dog new subclass of Dog: Lab new subclass of Dog: Collie new subclass of Animal: Cat
Rust
A type can't inherit properties from other types, but it can implmement any number of traits, which may themselves be subtraits of other traits.
trait Animal {}
trait Cat: Animal {}
trait Dog: Animal {}
trait Lab: Dog {}
trait Collie: Dog {}
Scala
Scala has both classes and traits. Classes can only be singly inherited, but both
can inherit a trait multiple times.
This inheritance can be declared at the point
of instantiation as well, precluding the need to declare a trait or class for the
sole purpose of combining traits.
For the simple inheritance chain of this task,
any (or all) of the class
keywords below can be replaced with trait
class Animal
class Dog extends Animal
class Cat extends Animal
class Lab extends Dog
class Collie extends Dog
Seed7
Seed7 object orientation is based on interface types and implementation types. The example below defines a hierarchy of implementation types.
$ include "seed7_05.s7i";
const type: Animal is new struct
# ...
end struct;
const type: Dog is sub Animal struct
# ...
end struct;
const type: Lab is sub Dog struct
# ...
end struct;
const type: Collie is sub Dog struct
# ...
end struct;
const type: Cat is sub Animal struct
# ...
end struct;
Self
Self is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This is an example of the relevant excerpts from a Self transporter fileout. Normally the object tree would be built and navigated within the graphical Self environment.
animal = ()
dog = (| parent* = animal |)
cat = (| parent* = animal |)
lab = (| parent* = dog |)
collie = (| parent* = dog |)
Sidef
class Animal {};
class Dog << Animal {};
class Cat << Animal {};
class Lab << Dog {};
class Collie << Dog {};
Simula
begin
class Animal;
! instance variables;
begin
! methods;
end;
Animal class Dog;
begin
end;
Animal class Cat;
begin
end;
Dog class Lab;
begin
end;
Dog class Collie;
begin
end;
end
Slate
define: #Animal &parents: {Cloneable}.
define: #Dog &parents: {Animal}.
define: #Cat &parents: {Animal}.
define: #Lab &parents: {Dog}.
define: #Collie &parents: {Dog}.
Smalltalk
This is an example of the object serialization format used by many varieties of Smalltalk. Normally the class tree would be defined and navigated via a class browser within a graphical Smalltalk environment.
Object subclass: #Animal
instanceVariableNames: ' ' "* space separated list of names *"
classVariableNames: ' '
poolDictionaries: ' '
category: ' ' !
"* declare methods here, separated with '!' *"
"* !Animal methodsFor: 'a category'! *"
"* methodName *"
"* method body! !"
!Animal subclass: #Dog
"* etc. *" !
!Animal subclass: #Cat
"* etc. *" !
!Dog subclass: #Lab
"* etc. *" !
!Dog subclass: #Collie
"* etc. *" !
Swift
class Animal {
// ...
}
class Dog : Animal {
// ...
}
class Lab : Dog {
// ...
}
class Collie : Dog {
// ...
}
class Cat : Animal {
// ...
}
Tcl
or
package require TclOO
oo::class create Animal {
# ...
}
oo::class create Dog {
superclass Animal
# ...
}
oo::class create Cat {
superclass Animal
# ...
}
oo::class create Collie {
superclass Dog
# ...
}
oo::class create Lab {
superclass Dog
# ...
}
TXR
Inheritance among symbolic exception tags
@(defex cat animal)
@(defex lab dog animal)
@(defex collie dog)
The second line is a shorthand which defines a lab to be a kind of dog, and at the same time a dog to be a kind of animal.
If we throw an exception of type lab
, it can be caught in a catch for a dog
or for an animal
. Continuing with the query:
@(try)
@ (throw lab "x")
@(catch animal (arg))
@(end)
- Output:
Test
$ txr dog-cat.txr arg="x"
OOP Inheritance in TXR Lisp
(defstruct animal nil
name
(:method get-name (me)
(if me.name me.name (error `get-name: animal @me has no name`)))
(:method speak (me stream)
(error "abstract animal cannot speak")))
(defstruct dog animal
(:method speak (me : (stream *stdout*))
(put-line `@{me.(get-name)}: bark!` stream)))
(defstruct cat animal
(:method speak (me : (stream *stdout*))
(put-line `@{me.(get-name)}: meow!` stream)))
(defstruct lab dog)
(defstruct collie dog)
(let ((pet1 (new collie name "Lassie"))
(pet2 (new cat name "Max")))
pet1.(speak)
pet2.(speak))
- Output:
Lassie: bark! Max: meow!
Visual Basic .NET
Class Animal
' ...
End Class
Class Dog
Inherits Animal
' ...
End Class
Class Lab
Inherits Dog
' ...
End Class
Class Collie
Inherits Dog
' ...
End Class
Class Cat
Inherits Animal
' ...
End Class
Vorpal
pet = new()
cat = new(pet)
dog = new(pet)
fido = new(dog)
felix = new(cat)
Wren
class Animal {
// methods
}
class Dog is Animal {
// methods
}
class Cat is Animal {
// mwethods
}
class Labrador is Dog {
// methods
}
class Collie is Dog {
// methods
}
XLISP
(define-class animal)
(define-class dog
(super-class animal))
(define-class cat
(super-class animal))
(define-class collie
(super-class dog))
(define-class lab
(super-class dog))
A REPL session:
[1] (cat 'superclass)
#<Class:ANIMAL #x57094c8>
[2] (collie 'superclass)
#<Class:DOG #x57094c8>
[3] (animal 'superclass)
#<Class:OBJECT #x57094c8>
[4] (dog 'show)
Object is #<Class:DOG #x57094c8>, Class is #<Class:CLASS #x57094c8>
Instance variables:
NAME = DOG
MESSAGES = ()
IVARS = ()
CVARS = #<Environment #x5879788>
SUPERCLASS = #<Class:ANIMAL #x57094c8>
IVARCNT = 0
IVARTOTAL = 0
#<Class:DOG #x57094c8>
zkl
class Animal{}
class Dog(Animal){} class Cat(Animal){}
class Lab(Dog){} class Collie(Dog){}
Collie.linearizeParents
- Output:
L(Class(Collie),Class(Dog),Class(Animal))
- Programming Tasks
- Basic language learning
- Object oriented
- Type System
- Encyclopedia
- 11l
- ActionScript
- Ada
- Aikido
- AmigaE
- AppleScript
- AutoHotkey
- BBC BASIC
- C
- C sharp
- C++
- ChucK
- Clojure
- COBOL
- Coco
- Comal
- Common Lisp
- Component Pascal
- D
- Delphi
- DWScript
- E
- Eiffel
- Elena
- EMal
- F Sharp
- Factor
- Fancy
- Fantom
- Forth
- Fortran
- FreeBASIC
- Go
- Groovy
- Haskell
- Haxe
- Unicon
- Inform 7
- Io
- J
- Java
- JavaScript
- Julia
- Kite
- Kotlin
- Lasso
- Latitude
- Lingo
- Lisaac
- Logtalk
- Lua
- M2000 Interpreter
- Neko
- Nemerle
- NetRexx
- Nim
- Oberon
- Oberon-2
- Objeck
- Objective-C
- OCaml
- Odin
- Oforth
- OoRexx
- OxygenBasic
- Oz
- Pascal
- PascalABC.NET
- Perl
- Phix
- Phix/Class
- PHP
- PicoLisp
- PowerShell
- PureBasic
- Python
- R
- Racket
- Raku
- REBOL
- Ring
- Ruby
- Rust
- Scala
- Seed7
- Self
- Sidef
- Simula
- Slate
- Smalltalk
- Swift
- Tcl
- TclOO
- TXR
- Visual Basic .NET
- Vorpal
- Wren
- XLISP
- Zkl
- 6502 Assembly/Omit
- 68000 Assembly/Omit
- 8080 Assembly/Omit
- 8086 Assembly/Omit
- ALGOL 68/Omit
- ARM Assembly/Omit
- AWK/Omit
- Axe/Omit
- Batch File/Omit
- M4/Omit
- Mathematica/Omit
- Maxima/Omit
- Metafont/Omit
- MIPS Assembly/Omit
- ML/I/Omit
- Modula-2/Omit
- PARI/GP/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Z80 Assembly/Omit