Inheritance
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
Animal
/\
/ \
/ \
Dog Cat
/\
/ \
/ \
Lab Collie
Contents |
[edit] 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;
[edit] C++
class Animal { // ... }; class Dog: public Animal { // ... }; class Lab: public Dog { // ... }; class Collie: public Dog { // ... }; class Cat: public Animal { // ... };
[edit] D
class Animal { // ... } class Dog: Animal { // ... } class Lab: Dog { // ... } class Collie: Dog { // ... } class Cat: Animal { // ... }
[edit] Io
Animal := Object clone Cat := Animal clone Dog := Animal clone Collie := Dog clone Lab := Dog clone
[edit] 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... }
[edit] 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
[edit] 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;
[edit] Python
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...
[edit] 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. *" !
Categories: Programming Tasks | Basic language learning | Ada | C++ | D | Io | Java | OCaml | Perl | Python | Smalltalk

