C++
Consider the following base class:
This in and of itself is slightly worthless. In our world we have all sorts of animals with all sorts of particular quirks and abilities.
Bird is a subclass of animal, it inherits all of Animal's protected data members. If Bird was our final destination, then I would probably make birdNoise private, but we'll see more on that later.
Horse is also a class derived from Animal. It also has its own horseNoise() method that is also protected (more on this later).
What we have created is a small hierarchy of relationships between classes. Now, this is all good if we just want an Animal, Bird, or Horse object. What if we need a class that has characteristics of say both a Horse and a Bird? Pegasus:
Pegasus derives from both Bird and Horse, which in turn derive from Animal (declared virtual to allow override for future expansion and to avoid inheritance issues). Since all we have at this point is just structure, here is an example program illustrating all the neat tricks Pegasus can do (with a few added items not present above):
Java
Now we can't do this directly with Java, as it only supports single direct inheritance, such as:
What we can do is use an interface to simulate multiple inheritance:
Now, instead of having Bird and Horse classes extend Animal (single inheritance), we'll make them interfaces. Any class that implements an interface is required to override the methods presented in them. This is for a programmer's own protection:
The java syntax for inheritance and interfaces:
Here is the Pegasus class with a main() example:
Hope this helps! Feel free to copy, paste, redistribute as you see fit.
--KYA
Consider the following base class:
//base class
class Animal {
protected: //derived classes get these
int age;
string name;
public:
Animal() {};
};
This in and of itself is slightly worthless. In our world we have all sorts of animals with all sorts of particular quirks and abilities.
//Bird is a derived class from Animal
class Bird : public virtual Animal {
public:
void birdNoise() { cout << "Bird Noise!\n";};
Bird() {};
};
Bird is a subclass of animal, it inherits all of Animal's protected data members. If Bird was our final destination, then I would probably make birdNoise private, but we'll see more on that later.
//Horse also derives from Animal
class Horse : public virtual Animal {
public:
void horseNoise() { cout << "Horse Noise!\n";};
Horse() {};
};
Horse is also a class derived from Animal. It also has its own horseNoise() method that is also protected (more on this later).
What we have created is a small hierarchy of relationships between classes. Now, this is all good if we just want an Animal, Bird, or Horse object. What if we need a class that has characteristics of say both a Horse and a Bird? Pegasus:
class Pegasus : public Bird, public Horse {
//reserved for future items
public:
Pegasus() {};
};
Pegasus derives from both Bird and Horse, which in turn derive from Animal (declared virtual to allow override for future expansion and to avoid inheritance issues). Since all we have at this point is just structure, here is an example program illustrating all the neat tricks Pegasus can do (with a few added items not present above):
#include <iostream>
#include <string>
using namespace std;
//base class
class Animal {
protected: //derived classes get these
int age;
string name;
public:
Animal() {};
string getName() {return name;};
int getAge() {return age;};
};
//Bird is a derived class from Animal
class Bird : public virtual Animal {
public:
void birdNoise() { cout << "Bird Noise!\n";};
Bird() {};
};
//Horse also derives from Animal
class Horse : public virtual Animal {
public:
void horseNoise() { cout << "Horse Noise!\n";};
Horse() {};
};
class Pegasus : public Bird, public Horse {
//reserved for future items
public:
Pegasus(string n, int a) {name = n; age = a;};
};
int main()
{
Pegasus* peggi = new Pegasus("Pegasus", 5); //create the pegasus object
cout << peggi->getName() << "'s age is " << peggi->getAge() << endl;
peggi->birdNoise();
peggi->horseNoise();
delete peggi;
return 0;
}
Java
Now we can't do this directly with Java, as it only supports single direct inheritance, such as:
public class Animal {
//animal stuff
}
public class Bird extends Animal {
//Bird stuff
}
//However we can't have another class extend Bird vis a vis extending Animal as well
What we can do is use an interface to simulate multiple inheritance:
//Base class
public class Animal {
protected int age;
protected String name;
public Animal() {}
public String getName() {return name;}
public int getAge() {return age;}
}
Now, instead of having Bird and Horse classes extend Animal (single inheritance), we'll make them interfaces. Any class that implements an interface is required to override the methods presented in them. This is for a programmer's own protection:
public interface Bird {
//all implementers have to overwrite methods
void birdNoise();
}
public interface Horse {
//all implementers have to overwrite methods
void horseNoise();
}
The java syntax for inheritance and interfaces:
public class className extends superClass implements interfaceOne, interfaceTwo, ...
Here is the Pegasus class with a main() example:
import java.io.*;
//single inheritance, multiple interfaces
public class Pegasus extends Animal implements Bird, Horse {
public Pegasus(String n, int a)
{
name = n;
age = a;
}
//overriding interface methods
public void horseNoise()
{
System.out.println("Horse Noise!");
}
public void birdNoise()
{
System.out.println("Bird Noise!");
}
public static void main(String[] args)
{
Pegasus peggi = new Pegasus("Pegasus", 5);
System.out.println(peggi.getName() + "'s age is " + peggi.getAge());
peggi.birdNoise();
peggi.horseNoise();
}
}
Hope this helps! Feel free to copy, paste, redistribute as you see fit.
--KYA
2 Comments On This Entry
Page 1 of 1
tivrfoa
20 February 2009 - 10:28 AM
I think this is better:
calling the method:
noise("Bird"); .......
.......
what do you think?
//base class
class Animal {
protected: //derived classes get these
int age;
string name;
public:
Animal() {};
void noise(string animal) { cout << animal + " Noise!\n";};
};
calling the method:
noise("Bird"); .......
.......
what do you think?
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
20 user(s) viewing
20 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



2 Comments









|