Inheritance/Single: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 29: Line 29:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<actionscript>
<lang actionscript>
public class Animal {
public class Animal {
// ...
// ...
}
}
</lang>
</actionscript>
<actionscript>
<lang actionscript>
public class Cat extends Animal {
public class Cat extends Animal {
// ...
// ...
}
}
</lang>
</actionscript>
<actionscript>
<lang actionscript>
public class Dog extends Animal {
public class Dog extends Animal {
// ...
// ...
}
}
</lang>
</actionscript>
<actionscript>
<lang actionscript>
public class Lab extends Dog {
public class Lab extends Dog {
// ...
// ...
}
}
</lang>
</actionscript>
<actionscript>
<lang actionscript>
public class Collie extends Dog {
public class Collie extends Dog {
// ...
// ...
}
}
</lang>
</actionscript>


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
<lang ada>
package Inheritance is
package Inheritance is
type Animal is tagged private;
type Animal is tagged private;
Line 70: Line 70:
type Collie is new Dog with null record;
type Collie is new Dog with null record;
end Inheritance;
end Inheritance;
</ada>
</lang>
=={{header|C++}}==
=={{header|C++}}==
<cpp>
<lang cpp>
class Animal
class Animal
{
{
Line 97: Line 97:
// ...
// ...
};
};
</cpp>
</lang>


=={{header|D}}==
=={{header|D}}==
<d>class Animal
<lang d>class Animal
{
{
// ...
// ...
Line 123: Line 123:
{
{
// ...
// ...
}</d>
}</lang>


=={{header|Io}}==
=={{header|Io}}==
Line 134: Line 134:


=={{header|Java}}==
=={{header|Java}}==
<java>public class Animal{
<lang java>public class Animal{
//functions go here...
//functions go here...
}</java>
}</lang>
<java>public class Dog extends Animal{
<lang java>public class Dog extends Animal{
//functions go here...
//functions go here...
}</java>
}</lang>
<java>public class Cat extends Animal{
<lang java>public class Cat extends Animal{
//functions go here...
//functions go here...
}</java>
}</lang>
<java>public class Lab extends Dog{
<lang java>public class Lab extends Dog{
//functions go here...
//functions go here...
}</java>
}</lang>
<java>public class Collie extends Dog{
<lang java>public class Collie extends Dog{
//functions go here...
//functions go here...
}</java>
}</lang>




=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>class animal =
<lang ocaml>class animal =
object (self)
object (self)
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class dog =
<lang ocaml>class dog =
object (self)
object (self)
inherit animal
inherit animal
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class cat =
<lang ocaml>class cat =
object (self)
object (self)
inherit animal
inherit animal
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class lab =
<lang ocaml>class lab =
object (self)
object (self)
inherit dog
inherit dog
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>
<ocaml>class collie =
<lang ocaml>class collie =
object (self)
object (self)
inherit dog
inherit dog
(*functions go here...*)
(*functions go here...*)
end</ocaml>
end</lang>


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


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


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


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


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


=={{header|PHP}}==
=={{header|PHP}}==
<ruby>class Animal {
<lang ruby>class Animal {
// functions go here...
// functions go here...
}
}
Line 225: Line 225:
class Collie extends Dog {
class Collie extends Dog {
// functions go here...
// functions go here...
}</ruby>
}</lang>


=={{header|Python}}==
=={{header|Python}}==
Unrevised style classes:
Unrevised style classes:
<python>class Animal:
<lang python>class Animal:
pass #functions go here...
pass #functions go here...


Line 242: Line 242:


class Collie(Dog):
class Collie(Dog):
pass #functions go here...</python>
pass #functions go here...</lang>


New style classes:
New style classes:
<python>
<lang python>
import time
import time


Line 292: Line 292:
buddy = Labrador()
buddy = Labrador()
buddy.kill()
buddy.kill()
print "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")</python>
print "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")</lang>
Output:<pre>
Output:<pre>
Felix has 6 lives, Buddy is not alive!
Felix has 6 lives, Buddy is not alive!
Line 298: Line 298:


=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>class Animal
<lang ruby>class Animal
#functions go here...
#functions go here...
end
end
Line 316: Line 316:
class Collie < Dog
class Collie < Dog
#functions go here...
#functions go here...
end</ruby>
end</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==

Revision as of 15:36, 3 February 2009

Task
Inheritance/Single
You are encouraged to solve this task according to the task description, using any language you may know.

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 directed acyclic graph (ignoring reflexivity). With single inheritance it becomes a tree.

Task: Show a tree of types 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:

    Animal
      /\
     /  \
    /    \
   Dog   Cat
   /\
  /  \
 /    \
Lab   Collie

ActionScript

<lang actionscript> public class Animal {

   // ...

} </lang> <lang actionscript> public class Cat extends Animal {

   // ...

} </lang> <lang actionscript> public class Dog extends Animal {

   // ...

} </lang> <lang actionscript> public class Lab extends Dog {

   // ...

} </lang> <lang actionscript> public class Collie extends Dog {

   // ...

} </lang>

Ada

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

C++

<lang cpp> class Animal {

 // ... 

};

class Dog: public Animal {

 // ... 

};

class Lab: public Dog {

 // ...

};

class Collie: public Dog {

 // ...

};

class Cat: public Animal {

 // ...

}; </lang>

D

<lang d>class Animal {

 // ... 

}

class Dog: Animal {

 // ... 

}

class Lab: Dog {

 // ...

}

class Collie: Dog {

 // ...

}

class Cat: Animal {

 // ...

}</lang>

Io

Animal := Object clone
Cat := Animal clone
Dog := Animal clone
Collie := Dog clone
Lab := Dog clone

Java

<lang java>public class Animal{

  //functions go here...

}</lang> <lang java>public class Dog extends Animal{

  //functions go here...

}</lang> <lang java>public class Cat extends Animal{

  //functions go here...

}</lang> <lang java>public class Lab extends Dog{

  //functions go here...

}</lang> <lang java>public class Collie extends Dog{

  //functions go here...

}</lang>


OCaml

<lang ocaml>class animal =

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

<lang ocaml>class dog =

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

<lang ocaml>class cat =

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

<lang ocaml>class lab =

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

<lang ocaml>class collie =

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

Perl

<lang perl>package Animal;

  1. functions go here...

1;</lang>

<lang perl>package Dog; use Animal; @ISA = qw( Animal );

  1. functions go here...

1;</lang>

<lang perl>package Cat; use Animal; @ISA = qw( Animal );

  1. functions go here...

1;</lang>

<lang perl>package Lab; use Dog; @ISA = qw( Dog );

  1. functions go here...

1;</lang>

<lang perl>package Collie; use Dog; @ISA = qw( Dog );

  1. functions go here...

1;</lang>

PHP

<lang ruby>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...

}</lang>

Python

Unrevised style classes: <lang 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...</lang>

New style classes: <lang python> 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 "Felix has",felix.lives, "lives, ","Buddy is %salive!"%("" if buddy.alive else "not ")</lang>

Output:

Felix has 6 lives,  Buddy is not alive!

Ruby

<lang ruby>class Animal

  #functions go here...

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

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. *" !