Inheritance/Single: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 67: | Line 67: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">T Animal |
||
{ |
{ |
||
} |
} |
||
Line 81: | Line 81: | ||
T Collie(Dog) |
T Collie(Dog) |
||
{ |
{ |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">public class Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="actionscript">public class Cat extends Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="actionscript">public class Dog extends Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="actionscript">public class Lab extends Dog { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="actionscript">public class Collie extends Dog { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="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 113: | Line 113: | ||
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> |
||
=={{header|Aikido}}== |
=={{header|Aikido}}== |
||
< |
<syntaxhighlight lang="aikido ">class Animal{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="aikido ">class Dog extends Animal { |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="aikido ">class Cat extends Animal { |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="aikido ">class Lab extends Dog { |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="aikido ">class Collie extends Dog { |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
< |
<syntaxhighlight lang="amigae"> |
||
OBJECT animal |
OBJECT animal |
||
ENDOBJECT |
ENDOBJECT |
||
Line 148: | Line 148: | ||
OBJECT collie OF dog |
OBJECT collie OF dog |
||
ENDOBJECT |
ENDOBJECT |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">script Animal |
||
end script |
end script |
||
Line 169: | Line 169: | ||
script Collie |
script Collie |
||
property parent : Dog |
property parent : Dog |
||
end script</ |
end script</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 175: | Line 175: | ||
AutoHotkey_L is prototype-based. However, for convenience, class-syntax may be used to create a base object. |
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." |
MsgBox, % "A " dog.__Class " is a " dog.base.base.__Class " and is part of the " dog.kingdom " kingdom." |
||
Line 188: | Line 188: | ||
} |
} |
||
class Collie extends Dog { |
class Collie extends Dog { |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"CLASSLIB" |
||
DIM Animal{method} |
DIM Animal{method} |
||
Line 211: | Line 211: | ||
DIM Collie{method} |
DIM Collie{method} |
||
PROC_inherit(Collie{}, Dog{}) |
PROC_inherit(Collie{}, Dog{}) |
||
PROC_class(Collie{})</ |
PROC_class(Collie{})</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 217: | Line 217: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">class Animal |
||
{ |
{ |
||
/* ... */ |
/* ... */ |
||
Line 245: | Line 245: | ||
/* ... */ |
/* ... */ |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">class Animal |
||
{ |
{ |
||
// ... |
// ... |
||
Line 271: | Line 271: | ||
{ |
{ |
||
// ... |
// ... |
||
};</ |
};</syntaxhighlight> |
||
=={{header|ChucK}}== |
=={{header|ChucK}}== |
||
< |
<syntaxhighlight lang="chuck">public class Drums{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="chuck">public class LatinKit extends Drums{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="chuck">public class ElectronicKit extends Drums{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="chuck">public class Congas extends LatinKit{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="chuck">public class TechnoDrums extends ElectronicKit{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 294: | Line 294: | ||
This is not very useful in clojure |
This is not very useful in clojure |
||
< |
<syntaxhighlight lang="clojure">(gen-class :name Animal) |
||
(gen-class :name Dog :extends Animal) |
(gen-class :name Dog :extends Animal) |
||
(gen-class :name Cat :extends Animal) |
(gen-class :name Cat :extends Animal) |
||
(gen-class :name Lab :extends Dog) |
(gen-class :name Lab :extends Dog) |
||
(gen-class :name Collie :extends Dog)</ |
(gen-class :name Collie :extends Dog)</syntaxhighlight> |
||
More useful: |
More useful: |
||
< |
<syntaxhighlight lang="clojure">(derive ::dog ::animal) |
||
(derive ::cat ::animal) |
(derive ::cat ::animal) |
||
(derive ::lab ::dog) |
(derive ::lab ::dog) |
||
(derive ::collie ::dog)</ |
(derive ::collie ::dog)</syntaxhighlight> |
||
use: |
use: |
||
< |
<syntaxhighlight lang="clojure">user> (isa? ::dog ::animal) |
||
true |
true |
||
user> (isa? ::dog ::cat) |
user> (isa? ::dog ::cat) |
||
false |
false |
||
user> (isa? ::collie ::animal) |
user> (isa? ::collie ::animal) |
||
true</ |
true</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> CLASS-ID. Animal. |
||
*> ... |
*> ... |
||
END CLASS Animal. |
END CLASS Animal. |
||
Line 355: | Line 355: | ||
*> ... |
*> ... |
||
END CLASS Collie.</ |
END CLASS Collie.</syntaxhighlight> |
||
=={{header|Coco}}== |
=={{header|Coco}}== |
||
< |
<syntaxhighlight lang="coco">class Animal |
||
class Cat extends Animal |
class Cat extends Animal |
||
class Dog extends Animal |
class Dog extends Animal |
||
class Lab extends Dog |
class Lab extends Dog |
||
class Collie 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: |
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) -> |
(@name) -> |
||
Line 394: | Line 394: | ||
sam.move! |
sam.move! |
||
tom.move!</ |
tom.move!</syntaxhighlight> |
||
=={{header|Comal}}== |
=={{header|Comal}}== |
||
{{works with|UniComal}} |
{{works with|UniComal}} |
||
{{works with|AmiComal}} |
{{works with|AmiComal}} |
||
< |
<syntaxhighlight lang="comal"> STRUC Animal |
||
DIM Species$ OF 20 |
DIM Species$ OF 20 |
||
ENDSTRUC Animal |
ENDSTRUC Animal |
||
Line 431: | Line 431: | ||
Race$:="Collie" |
Race$:="Collie" |
||
ENDFUNC New |
ENDFUNC New |
||
ENDSTRUC Collie</ |
ENDSTRUC Collie</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 437: | Line 437: | ||
Using CLOS classes, we have the following: |
Using CLOS classes, we have the following: |
||
< |
<syntaxhighlight lang="lisp">(defclass animal () ()) |
||
(defclass dog (animal) ()) |
(defclass dog (animal) ()) |
||
(defclass lab (dog) ()) |
(defclass lab (dog) ()) |
||
(defclass collie (dog) ()) |
(defclass collie (dog) ()) |
||
(defclass cat (animal) ())</ |
(defclass cat (animal) ())</syntaxhighlight> |
||
Alternatively, since there is no multiple inheritance in the task requirement, structures could also be used: |
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 (dog (:include animal))) |
||
(defstruct (lab (:include dog))) |
(defstruct (lab (:include dog))) |
||
(defstruct (collie (:include dog))) |
(defstruct (collie (:include dog))) |
||
(defstruct (cat (:include animal)))</ |
(defstruct (cat (:include animal)))</syntaxhighlight> |
||
(Structures are less flexible than CLOS objects but often somewhat more efficiently implemented, due to those restrictions.) |
(Structures are less flexible than CLOS objects but often somewhat more efficiently implemented, due to those restrictions.) |
||
Line 457: | Line 457: | ||
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. |
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 |
;;; ASN.1 serialization logic specialized for animal class |
||
(defmethod serialize-to-asn-1 ((a animal)) |
(defmethod serialize-to-asn-1 ((a animal)) |
||
Line 466: | Line 466: | ||
(defmethod serialize-to-asn-1 ((s string)) |
(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. |
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. |
||
Line 472: | Line 472: | ||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
< |
<syntaxhighlight lang="oberon2"> |
||
TYPE |
TYPE |
||
Animal = ABSTRACT RECORD (* *) END; |
Animal = ABSTRACT RECORD (* *) END; |
||
Line 479: | Line 479: | ||
Lab = RECORD (Dog) (* *) END; |
Lab = RECORD (Dog) (* *) END; |
||
Collie = RECORD (Dog) (* *) END; |
Collie = RECORD (Dog) (* *) END; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">class Animal { |
||
// ... |
// ... |
||
} |
} |
||
Line 502: | Line 502: | ||
} |
} |
||
void main() {}</ |
void main() {}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang="delphi">type |
|||
<lang Delphi>type |
|||
Animal = class(TObject) |
Animal = class(TObject) |
||
private |
private |
||
Line 517: | Line 517: | ||
Cat = class(Animal); |
Cat = class(Animal); |
||
Collie = class(Dog); |
Collie = class(Dog); |
||
Lab = class(Dog);</ |
Lab = class(Dog);</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
<syntaxhighlight lang="delphi">type |
|||
<lang Delphi>type |
|||
Animal = class(TObject) |
Animal = class(TObject) |
||
private |
private |
||
Line 532: | Line 532: | ||
type Cat = class(Animal) end; |
type Cat = class(Animal) end; |
||
type Collie = class(Dog) end; |
type Collie = class(Dog) end; |
||
type Lab = class(Dog) end;</ |
type Lab = class(Dog) end;</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
Line 540: | Line 540: | ||
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. |
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 { |
def stamp { |
||
to audit(audition) { |
to audit(audition) { |
||
Line 557: | Line 557: | ||
} |
} |
||
return [guard, stamp] |
return [guard, stamp] |
||
}</ |
}</syntaxhighlight> |
||
Setting up the task's specified tree: |
Setting up the task's specified tree: |
||
< |
<syntaxhighlight lang="e">def [Animal, AnimalStamp] := makeType("Animal", []) |
||
def [Cat, CatStamp] := makeType("Cat", [AnimalStamp]) |
def [Cat, CatStamp] := makeType("Cat", [AnimalStamp]) |
||
Line 567: | Line 567: | ||
def [Lab, LabStamp] := makeType("Lab", [DogStamp]) |
def [Lab, LabStamp] := makeType("Lab", [DogStamp]) |
||
def [Collie, CollieStamp] := makeType("Collie", [DogStamp])</ |
def [Collie, CollieStamp] := makeType("Collie", [DogStamp])</syntaxhighlight> |
||
Some example objects: |
Some example objects: |
||
< |
<syntaxhighlight lang="e">def fido implements LabStamp {} |
||
def tom implements CatStamp {} |
def tom implements CatStamp {} |
||
def brick {} # not an animal</ |
def brick {} # not an animal</syntaxhighlight> |
||
Testing against the types: |
Testing against the types: |
||
< |
<syntaxhighlight lang="e">? fido :Animal |
||
# value: <fido> |
# value: <fido> |
||
Line 593: | Line 593: | ||
? brick :Animal |
? brick :Animal |
||
# problem: <brick> is not a Animal</ |
# problem: <brick> is not a Animal</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
ANIMAL |
ANIMAL |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
DOG |
DOG |
||
inherit |
inherit |
||
ANIMAL |
ANIMAL |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
CAT |
CAT |
||
inherit |
inherit |
||
ANIMAL |
ANIMAL |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
LAB |
LAB |
||
inherit |
inherit |
||
DOG |
DOG |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="eiffel ">class |
||
COLLIE |
COLLIE |
||
inherit |
inherit |
||
DOG |
DOG |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x : |
ELENA 4.x : |
||
< |
<syntaxhighlight lang="elena">class Animal |
||
{ |
{ |
||
// ... |
// ... |
||
Line 645: | Line 645: | ||
{ |
{ |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{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. |
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 |
class // explicit syntax needed for empty class |
||
end |
end |
||
Line 663: | Line 663: | ||
type Cat() = |
type Cat() = |
||
inherit Animal()</ |
inherit Animal()</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">TUPLE: animal ; |
||
TUPLE: dog < animal ; |
TUPLE: dog < animal ; |
||
TUPLE: cat < animal ; |
TUPLE: cat < animal ; |
||
TUPLE: lab < dog ; |
TUPLE: lab < dog ; |
||
TUPLE: collie < dog ;</ |
TUPLE: collie < dog ;</syntaxhighlight> |
||
=={{header|Fancy}}== |
=={{header|Fancy}}== |
||
< |
<syntaxhighlight lang="fancy">class Animal { |
||
# ... |
# ... |
||
} |
} |
||
Line 691: | Line 691: | ||
class Collie : Dog { |
class Collie : Dog { |
||
# ... |
# ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom">class Animal |
||
{ |
{ |
||
} |
} |
||
Line 712: | Line 712: | ||
class Collie : Dog |
class Collie : Dog |
||
{ |
{ |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|4tH|3.61.5}} |
{{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]]. |
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 {} ; |
:: Animal class end-class {} ; |
||
Line 723: | Line 723: | ||
:: Cat extends Animal end-extends {} ; |
:: Cat extends Animal end-extends {} ; |
||
:: Lab extends Dog end-extends {} ; |
:: Lab extends Dog end-extends {} ; |
||
:: Collie extends Dog end-extends {} ;</ |
:: Collie extends Dog end-extends {} ;</syntaxhighlight> |
||
Line 730: | Line 730: | ||
Needs the FMS2 library code located here: |
Needs the FMS2 library code located here: |
||
https://github.com/DouglasBHoffman/FMS2 |
https://github.com/DouglasBHoffman/FMS2 |
||
< |
<syntaxhighlight lang="forth">include FMS2LL.f |
||
:class Animal ;class |
:class Animal ;class |
||
Line 736: | Line 736: | ||
:class Cat <super Animal ;class |
:class Cat <super Animal ;class |
||
:class Lab <super Dog ;class |
:class Lab <super Dog ;class |
||
:class Collie <super Dog ;class</ |
:class Collie <super Dog ;class</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{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). |
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 |
type animal |
||
Line 758: | Line 758: | ||
end type collie |
end type collie |
||
end module anim</ |
end module anim</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Type Animal Extends Object ' to enable virtual methods etc. if needed |
Type Animal Extends Object ' to enable virtual methods etc. if needed |
||
Line 781: | Line 781: | ||
Type Collie Extends Dog |
Type Collie Extends Dog |
||
' ... |
' ... |
||
End Type</ |
End Type</syntaxhighlight> |
||
=={{header|Go}}== |
=={{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. |
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 { |
type animal struct { |
||
Line 817: | Line 817: | ||
pet.color = "yellow" |
pet.color = "yellow" |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">class Animal{ |
||
//contents go here... |
//contents go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="groovy">class Dog extends Animal{ |
||
//contents go here... |
//contents go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="groovy">class Cat extends Animal{ |
||
//contents go here... |
//contents go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="groovy">class Lab extends Dog{ |
||
//contents go here... |
//contents go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="groovy">class Collie extends Dog{ |
||
//contents go here... |
//contents go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{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. |
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 => Cat a |
||
class Animal a => Dog a |
class Animal a => Dog a |
||
class Dog a => Lab a |
class Dog a => Lab a |
||
class Dog a => Collie a</ |
class Dog a => Collie a</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">class Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="haxe">class Cat extends Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="haxe">class Dog extends Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="haxe">class Lab extends Dog { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="haxe">class Collie extends Dog { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
== Icon and {{header|Unicon}} == |
== Icon and {{header|Unicon}} == |
||
Line 866: | Line 866: | ||
This example only works in Unicon. |
This example only works in Unicon. |
||
<syntaxhighlight lang="unicon"> |
|||
<lang Unicon> |
|||
class Animal () |
class Animal () |
||
end |
end |
||
Line 881: | Line 881: | ||
class Collie : Dog () |
class Collie : Dog () |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |
||
< |
<syntaxhighlight lang="inform7">An animal is a kind of thing. |
||
A cat is a kind of animal. |
A cat is a kind of animal. |
||
A dog is a kind of animal. |
A dog is a kind of animal. |
||
A collie is a kind of dog. |
A collie is a kind of dog. |
||
A lab 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). |
"Animal" is actually a predefined kind in Inform 7, so its definition here is redundant (but legal). |
||
Line 894: | Line 894: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">Animal := Object clone |
||
Cat := Animal clone |
Cat := Animal clone |
||
Dog := Animal clone |
Dog := Animal clone |
||
Collie := Dog clone |
Collie := Dog clone |
||
Lab := Dog clone</ |
Lab := Dog clone</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 904: | Line 904: | ||
Here is how this would normally be done: |
Here is how this would normally be done: |
||
<lang |
<syntaxhighlight lang="j">coclass 'Animal'</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">coclass 'Dog' |
||
coinsert 'Animal'</ |
coinsert 'Animal'</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">coclass 'Cat' |
||
coinsert 'Animal'</ |
coinsert 'Animal'</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">coclass 'Lab' |
||
coinsert 'Dog'</ |
coinsert 'Dog'</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">coclass 'Collie' |
||
coinsert 'Dog'</ |
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). |
<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). |
||
Line 920: | Line 920: | ||
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: |
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_Cat_ 'Animal' |
||
coinsert_Lab_ 'Dog' |
coinsert_Lab_ 'Dog' |
||
coinsert_Collie_ '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). |
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">public class Animal{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="java">public class Dog extends Animal{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="java">public class Cat extends Animal{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="java">public class Lab extends Dog{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="java">public class Collie extends Dog{ |
||
//functions go here... |
//functions go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. |
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. |
||
< |
<syntaxhighlight lang="javascript">function Animal() { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">function Dog() { |
||
// ... |
// ... |
||
} |
} |
||
Dog.prototype = new Animal();</ |
Dog.prototype = new Animal();</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">function Cat() { |
||
// ... |
// ... |
||
} |
} |
||
Cat.prototype = new Animal();</ |
Cat.prototype = new Animal();</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">function Collie() { |
||
// ... |
// ... |
||
} |
} |
||
Collie.prototype = new Dog();</ |
Collie.prototype = new Dog();</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">function Lab() { |
||
// ... |
// ... |
||
} |
} |
||
Lab.prototype = new Dog();</ |
Lab.prototype = new Dog();</syntaxhighlight> |
||
< |
<syntaxhighlight lang="javascript">Animal.prototype.speak = function() {print("an animal makes a sound")}; |
||
var lab = new Lab(); |
var lab = new Lab(); |
||
lab.speak(); // shows "an animal makes a sound"</ |
lab.speak(); // shows "an animal makes a sound"</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{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. |
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 Animal end |
||
abstract type Dog <: Animal end |
abstract type Dog <: Animal end |
||
Line 984: | Line 984: | ||
struct Lab <: Dog end |
struct Lab <: Dog end |
||
struct Collie <: Dog end |
struct Collie <: Dog end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kite}}== |
=={{header|Kite}}== |
||
< |
<syntaxhighlight lang="kite">class Animal [ |
||
#Method goes here |
#Method goes here |
||
]; |
]; |
||
Line 1,002: | Line 1,002: | ||
#Method goes here |
#Method goes here |
||
]; |
]; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
open class Animal { |
open class Animal { |
||
Line 1,036: | Line 1,036: | ||
println("Bella is a $bella") |
println("Bella is a $bella") |
||
println("Casey is a $casey") |
println("Casey is a $casey") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,047: | Line 1,047: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">define animal => type { |
||
data public gender::string |
data public gender::string |
||
} |
} |
||
Line 1,070: | Line 1,070: | ||
#myanimal -> gender = 'Male' |
#myanimal -> gender = 'Male' |
||
#myanimal -> gender</ |
#myanimal -> gender</syntaxhighlight> |
||
-> Male |
-> Male |
||
Line 1,077: | Line 1,077: | ||
Latitude is a prototype-oriented language, so defining a subclass is equivalent to constructing an instance. |
Latitude is a prototype-oriented language, so defining a subclass is equivalent to constructing an instance. |
||
< |
<syntaxhighlight lang="latitude"> |
||
Animal ::= Object clone tap { |
Animal ::= Object clone tap { |
||
;; Methods go here... |
;; Methods go here... |
||
Line 1,096: | Line 1,096: | ||
Collie ::= Dog clone tap { |
Collie ::= Dog clone tap { |
||
;; Methods go here... |
;; 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". |
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". |
||
Line 1,102: | Line 1,102: | ||
=={{header|Lingo}}== |
=={{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). |
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" |
||
-- ...</ |
-- ...</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">-- parent script "Dog" |
||
property ancestor |
property ancestor |
||
Line 1,111: | Line 1,111: | ||
me.ancestor = script("Animal").new() |
me.ancestor = script("Animal").new() |
||
return me |
return me |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">-- parent script "Cat" |
||
property ancestor |
property ancestor |
||
Line 1,119: | Line 1,119: | ||
me.ancestor = script("Animal").new() |
me.ancestor = script("Animal").new() |
||
return me |
return me |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">-- parent script "Lab" |
||
property ancestor |
property ancestor |
||
Line 1,127: | Line 1,127: | ||
me.ancestor = script("Dog").new() |
me.ancestor = script("Dog").new() |
||
return me |
return me |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">-- parent script "Collie" |
||
property ancestor |
property ancestor |
||
Line 1,135: | Line 1,135: | ||
me.ancestor = script("Dog").new() |
me.ancestor = script("Dog").new() |
||
return me |
return me |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := ANIMAL; |
+ name := ANIMAL; |
||
// ...</ |
// ...</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := CAT; |
+ name := CAT; |
||
Section Inherit |
Section Inherit |
||
- parent : ANIMAL := ANIMAL; |
- parent : ANIMAL := ANIMAL; |
||
// ...</ |
// ...</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := DOG; |
+ name := DOG; |
||
Section Inherit |
Section Inherit |
||
- parent : ANIMAL := ANIMAL; |
- parent : ANIMAL := ANIMAL; |
||
// ...</ |
// ...</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := LAB; |
+ name := LAB; |
||
Section Inherit |
Section Inherit |
||
- parent : DOG := DOG; |
- parent : DOG := DOG; |
||
// ...</ |
// ...</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := COLLIE; |
+ name := COLLIE; |
||
Section Inherit |
Section Inherit |
||
- parent : DOG := DOG; |
- parent : DOG := DOG; |
||
// ...</ |
// ...</syntaxhighlight> |
||
=={{header|Logtalk}}== |
=={{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). |
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, |
:- object(thing, |
||
instantiates(thing)). |
instantiates(thing)). |
||
Line 1,192: | Line 1,192: | ||
specializes(dog)). |
specializes(dog)). |
||
... |
... |
||
:- end_object.</ |
:- end_object.</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Lua has no in-built formal OOP mechanism, though there are many possible ways of implementing work-alikes. |
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", |
classname = "Class aka Object aka Root-Of-Tree", |
||
new = function(s,t) |
new = function(s,t) |
||
Line 1,225: | Line 1,225: | ||
print("max's parent's parent's parent is (class): " .. max.parent.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 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))</ |
print("max's parent's parent's parent's parent's parent is (nil reference): " .. tostring(max.parent.parent.parent.parent.parent))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Animal:speak(): (Animal has no voice) |
<pre>Animal:speak(): (Animal has no voice) |
||
Line 1,241: | Line 1,241: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module CheckIt { |
Module CheckIt { |
||
Class Animal { |
Class Animal { |
||
Line 1,260: | Line 1,260: | ||
} |
} |
||
CheckIt |
CheckIt |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
< |
<syntaxhighlight lang="neko">var Animal = $new(null); |
||
var Dog = $new(null); |
var Dog = $new(null); |
||
Line 1,275: | Line 1,275: | ||
var Collie = $new(null); |
var Collie = $new(null); |
||
$objsetproto(Collie, Dog);</ |
$objsetproto(Collie, Dog);</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">class Animal { |
||
// ... |
// ... |
||
} |
} |
||
Line 1,296: | Line 1,296: | ||
class Cat: Animal { |
class Cat: Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
Line 1,302: | Line 1,302: | ||
For brevity, all classes are defined within the same source file. Normally classes exist as separate source units. |
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 |
options replace format comments java crossref symbols binary |
||
Line 1,357: | Line 1,357: | ||
-- Do Collie specific set-up |
-- Do Collie specific set-up |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,370: | Line 1,370: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">type |
||
Animal = object of RootObj |
Animal = object of RootObj |
||
Dog = object of Animal |
Dog = object of Animal |
||
Cat = object of Animal |
Cat = object of Animal |
||
Lab = object of Dog |
Lab = object of Dog |
||
Collie = object of Dog</ |
Collie = object of Dog</syntaxhighlight> |
||
=={{header|Oberon}}== |
=={{header|Oberon}}== |
||
Tested with [https://miasap.se/obnc OBNC]. |
Tested with [https://miasap.se/obnc OBNC]. |
||
< |
<syntaxhighlight lang="oberon">MODULE Animals; |
||
TYPE |
TYPE |
||
Line 1,389: | Line 1,389: | ||
END Animals. |
END Animals. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c Version 2 |
Works with oo2c Version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE Animals; |
MODULE Animals; |
||
TYPE |
TYPE |
||
Line 1,412: | Line 1,412: | ||
END Animals. |
END Animals. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">class Animal |
||
{ #~ ... ~# } |
{ #~ ... ~# } |
||
Line 1,428: | Line 1,428: | ||
class Cat from Animal |
class Cat from Animal |
||
{ #~ ... ~# }</ |
{ #~ ... ~# }</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
< |
<syntaxhighlight lang="objc">@interface Animal : NSObject |
||
{ |
{ |
||
// ... |
// ... |
||
Line 1,464: | Line 1,464: | ||
} |
} |
||
// ... |
// ... |
||
@end</ |
@end</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">class animal = |
||
object (self) |
object (self) |
||
(*functions go here...*) |
(*functions go here...*) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ocaml">class dog = |
||
object (self) |
object (self) |
||
inherit animal |
inherit animal |
||
(*functions go here...*) |
(*functions go here...*) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ocaml">class cat = |
||
object (self) |
object (self) |
||
inherit animal |
inherit animal |
||
(*functions go here...*) |
(*functions go here...*) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ocaml">class lab = |
||
object (self) |
object (self) |
||
inherit dog |
inherit dog |
||
(*functions go here...*) |
(*functions go here...*) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ocaml">class collie = |
||
object (self) |
object (self) |
||
inherit dog |
inherit dog |
||
(*functions go here...*) |
(*functions go here...*) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">Object Class new: Animal |
||
Animal Class new: Cat |
Animal Class new: Cat |
||
Animal Class new: Dog |
Animal Class new: Dog |
||
Dog Class new: Lab |
Dog Class new: Lab |
||
Dog Class new: Collie</ |
Dog Class new: Collie</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
<syntaxhighlight lang="oorexx"> |
|||
<lang ooRexx> |
|||
-- subclass of object by default |
-- subclass of object by default |
||
::class animal |
::class animal |
||
Line 1,512: | Line 1,512: | ||
::class collie subclass dog |
::class collie subclass dog |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OxygenBasic}}== |
=={{header|OxygenBasic}}== |
||
< |
<syntaxhighlight lang="oxygenbasic"> |
||
class animal |
class animal |
||
method show() as string |
method show() as string |
||
Line 1,553: | Line 1,553: | ||
Collie c |
Collie c |
||
print c.show 'result: Animal Dog Collie |
print c.show 'result: Animal Dog Collie |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">class Animal |
||
%% ... |
%% ... |
||
end |
end |
||
Line 1,574: | Line 1,574: | ||
class Cat from Animal |
class Cat from Animal |
||
%% ... |
%% ... |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 1,581: | Line 1,581: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">package Animal; |
||
#functions go here... |
#functions go here... |
||
1;</ |
1;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">package Dog; |
||
use Animal; |
use Animal; |
||
@ISA = qw( Animal ); |
@ISA = qw( Animal ); |
||
#functions go here... |
#functions go here... |
||
1;</ |
1;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">package Cat; |
||
use Animal; |
use Animal; |
||
@ISA = qw( Animal ); |
@ISA = qw( Animal ); |
||
#functions go here... |
#functions go here... |
||
1;</ |
1;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">package Lab; |
||
use Dog; |
use Dog; |
||
@ISA = qw( Dog ); |
@ISA = qw( Dog ); |
||
#functions go here... |
#functions go here... |
||
1;</ |
1;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">package Collie; |
||
use Dog; |
use Dog; |
||
@ISA = qw( Dog ); |
@ISA = qw( Dog ); |
||
#functions go here... |
#functions go here... |
||
1;</ |
1;</syntaxhighlight> |
||
The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module: |
The same using the [http://search.cpan.org/perldoc?MooseX::Declare MooseX::Declare] module: |
||
< |
<syntaxhighlight lang="perl">use MooseX::Declare; |
||
class Animal { |
class Animal { |
||
Line 1,627: | Line 1,627: | ||
class Collie extends Dog { |
class Collie extends Dog { |
||
# methods go here... |
# methods go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/Class}} |
{{libheader|Phix/Class}} |
||
Add (private|public) fields and methods as needed. Make Animal and Dog abstract (ie use "abstract class") to prevent instantiation. |
Add (private|public) fields and methods as needed. Make Animal and Dog abstract (ie use "abstract class") to prevent instantiation. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (class)</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (class)</span> |
||
<span style="color: #008080;">class</span> <span style="color: #000000;">Animal</span> |
<span style="color: #008080;">class</span> <span style="color: #000000;">Animal</span> |
||
Line 1,645: | Line 1,645: | ||
<span style="color: #008080;">class</span> <span style="color: #000000;">Collie</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Dog</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span> |
<span style="color: #008080;">class</span> <span style="color: #000000;">Collie</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Dog</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span> |
||
<span style="color: #008080;">class</span> <span style="color: #000000;">Cat</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Animal</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span> |
<span style="color: #008080;">class</span> <span style="color: #000000;">Cat</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Animal</span> <span style="color: #008080;">end</span> <span style="color: #008080;">class</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">class Animal { |
||
// functions go here... |
// functions go here... |
||
} |
} |
||
Line 1,666: | Line 1,666: | ||
class Collie extends Dog { |
class Collie extends Dog { |
||
// functions go here... |
// functions go here... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(class +Animal) |
||
(class +Dog +Animal) |
(class +Dog +Animal) |
||
Line 1,677: | Line 1,677: | ||
(class +Lab +Dog) |
(class +Lab +Dog) |
||
(class +Collie +Dog)</ |
(class +Collie +Dog)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="picolisp">: (dep '+Animal) |
||
+Animal |
+Animal |
||
+Cat |
+Cat |
||
+Dog |
+Dog |
||
+Collie |
+Collie |
||
+Lab</ |
+Lab</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{works with|PowerShell|5}} |
{{works with|PowerShell|5}} |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
class Animal {} |
class Animal {} |
||
class Dog : Animal {} |
class Dog : Animal {} |
||
Line 1,693: | Line 1,693: | ||
class Lab : Dog {} |
class Lab : Dog {} |
||
class Collie : Dog {} |
class Collie : Dog {} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{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. |
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=== |
===Native version=== |
||
< |
<syntaxhighlight lang="purebasic">Interface Animal |
||
Eat() |
Eat() |
||
Sleep() |
Sleep() |
||
Line 1,718: | Line 1,718: | ||
Interface Collie Extends Dog |
Interface Collie Extends Dog |
||
HeardSheep() |
HeardSheep() |
||
EndInterface</ |
EndInterface</syntaxhighlight> |
||
===Simple OOP Version=== |
===Simple OOP Version=== |
||
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP]. |
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP]. |
||
< |
<syntaxhighlight lang="purebasic">Class Animal |
||
EndClass |
EndClass |
||
Line 1,747: | Line 1,747: | ||
*Lassie.Collie = NewObject.Collie |
*Lassie.Collie = NewObject.Collie |
||
*Lassie\Bark() |
*Lassie\Bark() |
||
*Lassie\Fetch()</ |
*Lassie\Fetch()</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Unrevised style classes: |
Unrevised style classes: |
||
< |
<syntaxhighlight lang="python">class Animal: |
||
pass #functions go here... |
pass #functions go here... |
||
Line 1,764: | Line 1,764: | ||
class Collie(Dog): |
class Collie(Dog): |
||
pass #functions go here...</ |
pass #functions go here...</syntaxhighlight> |
||
New style classes: |
New style classes: |
||
< |
<syntaxhighlight lang="python">import time |
||
class Animal(object): |
class Animal(object): |
||
Line 1,813: | Line 1,813: | ||
buddy = Labrador() |
buddy = Labrador() |
||
buddy.kill() |
buddy.kill() |
||
print "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")</ |
print "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,822: | Line 1,822: | ||
===S3=== |
===S3=== |
||
Inheritance is implemented by setting the object's class attribute with a character vector. |
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")</ |
class(aCollie) <- c("Collie", "Dog", "Animal")</syntaxhighlight> |
||
===S4=== |
===S4=== |
||
Inheritance is implemented by using the 'contains' argument in setClass |
Inheritance is implemented by using the 'contains' argument in setClass |
||
< |
<syntaxhighlight lang="r">setClass("Animal", representation(), prototype()) |
||
setClass("Dog", representation(), prototype(), contains="Animal") |
setClass("Dog", representation(), prototype(), contains="Animal") |
||
setClass("Cat", representation(), prototype(), contains="Animal") |
setClass("Cat", representation(), prototype(), contains="Animal") |
||
setClass("Collie", representation(), prototype(), contains="Dog") |
setClass("Collie", representation(), prototype(), contains="Dog") |
||
setClass("Lab", representation(), prototype(), contains="Dog")</ |
setClass("Lab", representation(), prototype(), contains="Dog")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 1,848: | Line 1,848: | ||
(check-true (is-a? (new dog%) animal%)) |
(check-true (is-a? (new dog%) animal%)) |
||
(check-false (is-a? (new collie%) cat%)) |
(check-false (is-a? (new collie%) cat%)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,854: | Line 1,854: | ||
{{works with|Rakudo|2015-09-16}} |
{{works with|Rakudo|2015-09-16}} |
||
<lang |
<syntaxhighlight lang="raku" line>class Animal {} |
||
class Dog is Animal {} |
class Dog is Animal {} |
||
class Cat is Animal {} |
class Cat is Animal {} |
||
Line 1,861: | Line 1,861: | ||
say Collie.^parents; # undefined type object |
say Collie.^parents; # undefined type object |
||
say Collie.new.^parents; # instantiated object</ |
say Collie.new.^parents; # instantiated object</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>((Dog) (Animal)) |
<pre>((Dog) (Animal)) |
||
Line 1,869: | Line 1,869: | ||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Inheritance" |
Title: "Inheritance" |
||
URL: http://rosettacode.org/wiki/Inheritance |
URL: http://rosettacode.org/wiki/Inheritance |
||
Line 1,894: | Line 1,894: | ||
print ["Cat has" Cat/legs "legs."] |
print ["Cat has" Cat/legs "legs."] |
||
print ["Lab says:" Lab/says]</ |
print ["Lab says:" Lab/says]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,901: | Line 1,901: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
Class Animal |
Class Animal |
||
Class Dog from Animal |
Class Dog from Animal |
||
Line 1,907: | Line 1,907: | ||
Class Lab from Dog |
Class Lab from Dog |
||
Class Collie from Dog |
Class Collie from Dog |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{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). |
<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... |
#functions go here... |
||
def self.inherited(subclass) |
def self.inherited(subclass) |
||
Line 1,932: | Line 1,932: | ||
class Collie < Dog |
class Collie < Dog |
||
#functions go here... |
#functions go here... |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,942: | Line 1,942: | ||
=={{header|Rust}}== |
=={{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. |
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 Cat: Animal {} |
||
trait Dog: Animal {} |
trait Dog: Animal {} |
||
trait Lab: Dog {} |
trait Lab: Dog {} |
||
trait Collie: Dog {}</ |
trait Collie: Dog {}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 1,957: | Line 1,957: | ||
any (or all) of the <code>class</code> keywords below can be replaced with <code>trait</code> |
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 Dog extends Animal |
||
class Cat extends Animal |
class Cat extends Animal |
||
class Lab extends Dog |
class Lab extends Dog |
||
class Collie extends Dog</ |
class Collie extends Dog</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 1,967: | Line 1,967: | ||
The example below defines a hierarchy of implementation types. |
The example below defines a hierarchy of implementation types. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const type: Animal is new struct |
const type: Animal is new struct |
||
Line 1,987: | Line 1,987: | ||
const type: Cat is sub Animal struct |
const type: Cat is sub Animal struct |
||
# ... |
# ... |
||
end struct;</ |
end struct;</syntaxhighlight> |
||
=={{header|Self}}== |
=={{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. |
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 = ()</syntaxhighlight> |
||
< |
<syntaxhighlight lang="self">dog = (| parent* = animal |)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="self">cat = (| parent* = animal |)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="self">lab = (| parent* = dog |)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="self">collie = (| parent* = dog |)</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">class Animal {}; |
||
class Dog << Animal {}; |
class Dog << Animal {}; |
||
class Cat << Animal {}; |
class Cat << Animal {}; |
||
class Lab << Dog {}; |
class Lab << Dog {}; |
||
class Collie << Dog {};</ |
class Collie << Dog {};</syntaxhighlight> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">begin |
||
class Animal; |
class Animal; |
||
Line 2,029: | Line 2,029: | ||
end; |
end; |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">define: #Animal &parents: {Cloneable}. |
||
define: #Dog &parents: {Animal}. |
define: #Dog &parents: {Animal}. |
||
define: #Cat &parents: {Animal}. |
define: #Cat &parents: {Animal}. |
||
define: #Lab &parents: {Dog}. |
define: #Lab &parents: {Dog}. |
||
define: #Collie &parents: {Dog}.</ |
define: #Collie &parents: {Dog}.</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{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. |
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 *" |
instanceVariableNames: ' ' "* space separated list of names *" |
||
classVariableNames: ' ' |
classVariableNames: ' ' |
||
Line 2,062: | Line 2,062: | ||
!Dog subclass: #Collie |
!Dog subclass: #Collie |
||
"* etc. *" !</ |
"* etc. *" !</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">class Animal { |
||
// ... |
// ... |
||
} |
} |
||
Line 2,083: | Line 2,083: | ||
class Cat : Animal { |
class Cat : Animal { |
||
// ... |
// ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} or {{libheader|TclOO}} |
{{works with|Tcl|8.6}} or {{libheader|TclOO}} |
||
< |
<syntaxhighlight lang="tcl">package require TclOO |
||
oo::class create Animal { |
oo::class create Animal { |
||
# ... |
# ... |
||
Line 2,106: | Line 2,106: | ||
superclass Dog |
superclass Dog |
||
# ... |
# ... |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 2,112: | Line 2,112: | ||
====Inheritance among symbolic exception tags==== |
====Inheritance among symbolic exception tags==== |
||
< |
<syntaxhighlight lang="txr">@(defex cat animal) |
||
@(defex lab dog animal) |
@(defex lab dog animal) |
||
@(defex collie dog)</ |
@(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. |
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. |
||
Line 2,120: | Line 2,120: | ||
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: |
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") |
@ (throw lab "x") |
||
@(catch animal (arg)) |
@(catch animal (arg)) |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
{{out}} Test: |
{{out}} Test: |
||
Line 2,131: | Line 2,131: | ||
====OOP Inheritance in TXR Lisp==== |
====OOP Inheritance in TXR Lisp==== |
||
< |
<syntaxhighlight lang="txrlisp">(defstruct animal nil |
||
name |
name |
||
(:method get-name (me) |
(:method get-name (me) |
||
Line 2,153: | Line 2,153: | ||
(pet2 (new cat name "Max"))) |
(pet2 (new cat name "Max"))) |
||
pet1.(speak) |
pet1.(speak) |
||
pet2.(speak))</ |
pet2.(speak))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,161: | Line 2,161: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vbnet">Class Animal |
||
' ... |
' ... |
||
End Class |
End Class |
||
Line 2,183: | Line 2,183: | ||
Inherits Animal |
Inherits Animal |
||
' ... |
' ... |
||
End Class</ |
End Class</syntaxhighlight> |
||
=={{header|Vorpal}}== |
=={{header|Vorpal}}== |
||
< |
<syntaxhighlight lang="vorpal">pet = new() |
||
cat = new(pet) |
cat = new(pet) |
||
dog = new(pet) |
dog = new(pet) |
||
fido = new(dog) |
fido = new(dog) |
||
felix = new(cat)</ |
felix = new(cat)</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">class Animal { |
||
// methods |
// methods |
||
} |
} |
||
Line 2,211: | Line 2,211: | ||
class Collie is Dog { |
class Collie is Dog { |
||
// methods |
// methods |
||
}</ |
}</syntaxhighlight> |
||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
< |
<syntaxhighlight lang="lisp">(define-class animal) |
||
(define-class dog |
(define-class dog |
||
Line 2,226: | Line 2,226: | ||
(define-class lab |
(define-class lab |
||
(super-class dog))</ |
(super-class dog))</syntaxhighlight> |
||
A REPL session: |
A REPL session: |
||
< |
<syntaxhighlight lang="lisp">[1] (cat 'superclass) |
||
#<Class:ANIMAL #x57094c8> |
#<Class:ANIMAL #x57094c8> |
||
Line 2,248: | Line 2,248: | ||
IVARCNT = 0 |
IVARCNT = 0 |
||
IVARTOTAL = 0 |
IVARTOTAL = 0 |
||
#<Class:DOG #x57094c8></ |
#<Class:DOG #x57094c8></syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">class Animal{} |
||
class Dog(Animal){} class Cat(Animal){} |
class Dog(Animal){} class Cat(Animal){} |
||
class Lab(Dog){} class Collie(Dog){} |
class Lab(Dog){} class Collie(Dog){} |
||
Collie.linearizeParents</ |
Collie.linearizeParents</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |