Inheritance/Single: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created task with Java)
 
(C++ version)
Line 9: Line 9:
/ \
/ \
Lab Collie</pre>
Lab Collie</pre>

=={{header|C++}}==
<cpp>
class Animal
{
// ...
};

class Dog: public Animal
{
// ...
};

class Lab: public Dog
{
// ...
};

class Collie: public Dog
{
// ...
};

class Cat: public Animal
{
// ...
};
</cpp>

=={{header|Java}}==
=={{header|Java}}==
<java>public class Animal{
<java>public class Animal{

Revision as of 16:45, 27 April 2008

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

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:

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

C++

<cpp> class Animal {

 // ... 

};

class Dog: public Animal {

 // ... 

};

class Lab: public Dog {

 // ...

};

class Collie: public Dog {

 // ...

};

class Cat: public Animal {

 // ...

}; </cpp>

Java

<java>public class Animal{

  //functions go here...

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

  //functions go here...

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

  //functions go here...

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

  //functions go here...

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

  //functions go here...

}</java>