Search code examples
javamultithreadingextending

Java - Extend a class that extend a Thread class


I'm studying for an exam about multi-threading, I started an exercise by myself in order to improve my ability with Java but I have a question:

Am I able to extend a class with another class made by me that extends Thread too?

I'm aware I can extend only 1 class in Java (and that I could implement Runnable, though I'm not very comfortable with that right now), but can I do it in cascade as said before? Or am I forced to use implements? I'll throw my example to make you get exactly what would I do:

Character class -> extends Thread
Warrior class -> extends Character
Wizard class -> extends Character

and If I can do it, where could I use the super() function?


Solution

  • As you mentions, a class can only inherit (extend) from one other class But hierarchical structure of extending classes does exist.

    For example, you could have a

    public class Animal{
    }
    
    public class Mammal extends Animal{
    }
    
    public class Monkey extends Mammal{
    }
    

    the super() is used within the constructor of each class