Accname
2D-Graphics enthusiast
- Reaction score
- 1,462
Hi there.
I am curious as to what would be the best way of implementing multiple inheritance in programming languages.
I have made some thoughts about this topic and came up with the following solution.
I would like to know what you guys think about it, what the problems would be with this and how it is done in different languages.
Heres an example of what I have in mind:
I am curious as to what would be the best way of implementing multiple inheritance in programming languages.
I have made some thoughts about this topic and came up with the following solution.
I would like to know what you guys think about it, what the problems would be with this and how it is done in different languages.
Heres an example of what I have in mind:
Code:
public class Artist {
public void draw() {
System.out.println("A nice sketch is being drawn.");
}
}
public class Cowboy {
public void draw() {
System.out.println("A bullet is fired.");
}
}
// This class inherits the methods of both Cowboys and Artists
public class ArtisticCowboy extends Cowboy, Artist {
// This method will be called when a method expects an Cowboy but gets an ArtisticCowboy
public void (Cowboy) draw() {
super(Cowboy).draw();
}
// This method will be called when a method expects an Artist but gets an ArtisticCowboy
public void (Artist) draw() {
super(Artist).draw();
}
}
public class Competition {
public static void main(String[] args) {
ArtistCowboy ac = new ArtistCowboy();
addCowboyContestant(ac); // Out: "A bullet is fired."
addArtistContestant(ac); // Out: "A nice sketch is being drawn."
}
public void addCowboyContestant(Cowboy cowboy) {
cowboy.draw();
}
public void addArtistContestant(Artist artist) {
artist.draw();
}
}