Niminim
Niminim

Reputation: 678

Java constructor of a subclass of an abstract class

I started to develop my first java project, it's a kind of basic roleplaying game. I want to create a character.

First I'll explain a few things:

What I want to do is choose a character class in Game (let's assume that I choose Fighter), and get all the stats by displayStats. When the character chooses a class then Game should call something like: Chracter ch = new Character(name, scan), and then ch.displayCharacter.

My problem is Fighter's constructor and assigning this data to Character's constructor.

public class Game {
public static void main(String[] args) {

    System.out.println("Choose a character: ");
    System.out.println("1. Fighter");
    System.out.println("2. Rogue");
    System.out.println("3. Mage");
    System.out.println("4. Cleric");


    Scanner sc = new Scanner(System.in);
    int scan = sc.nextInt();
    System.out.println("Character Class: " + CharacterUtil.getCharacterClass(scan));

    System.out.println("Choose Name:");
    Scanner nameIn = new Scanner(System.in);
    String name = nameIn.next();
    System.out.println("Name: " + name);

}
}


public class Character {

private String name;
private String characterClass;
private int level;
private int hp;
private int currentHp;
private long xp;
/*private int BAB; /*Base attack bonus*/

private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;

Character(String name, String chracterClass){

    this.name = name;
    this.characterClass = chracterClass;
    level =  ;
    hp = ;
    currentHp = hp;
    xp = 0;
    strength = ;
    constitution = ;
    dexterity = ;
    intelligence = ;
    wisdom = ;
    charisma = ;


}


void displayCharacter(){
    System.out.println("Name: " + name);
    System.out.println("Level: " + level);
    System.out.println("Class: " + characterClass);
    System.out.println("HP: " + hp);
    System.out.println("Attributes: ");
    System.out.println("Strength: " + strength);
    System.out.println("Constitution: " + constitution);
    System.out.println("Dexterity: " + dexterity);
    System.out.println("Intelligence: " + intelligence);
    System.out.println("Wisdom: " + wisdom);
    System.out.println("Charisma: " + strength);
    System.out.println("XP: " + xp);

}

}

abstract class CharacterClass {

private int level;
private int hp;

private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;

CharacterClass(){

    level = 1;
    hp = 10;
    strength = 10;
    constitution = 10;
    dexterity = 10;
    intelligence = 10;
    wisdom = 10;
    charisma = 10;
}

class Fighter extends CharacterClass {

Fighter(){
    super.level = 1;
    super.hp = 10;
    super.strength = 16;
    super.constitution = 14;
    super.dexterity = 14;
    super.intelligence = 10;
    super.wisdom= 10;
    super.charisma = 10;
}
}

Upvotes: 0

Views: 513

Answers (1)

Aaron
Aaron

Reputation: 24812

Either make your CharacterClass private fields protected instead of private so they can be accessed by subclasses or implement getters/setters in your abstract class and use them in your subclass constructor.

Also super.field does not mean anything : your are instanciating an object which will have the fields from the abstract class and the subclass, it will be this.fields that you want to access.

Sample code :

public abstract class CharacterClass {

    private int intelligence;
    private int strength;
    private int dexterity;
    private int vitality;

    protected CharacterClass() {
        setIntelligence(10);
        setStrength(10);
        setDexterity(10);
        setVitality(10);
    }

    public int getDexterity() {
        return dexterity;
    }
    protected void setDexterity(int dexterity) {
        this.dexterity = dexterity;
    }
    public int getVitality() {
        return vitality;
    }
    protected void setVitality(int vitality) {
        this.vitality = vitality;
    }
    public int getStrength() {
        return strength;
    }
    protected void setStrength(int strength) {
        this.strength = strength;
    }
    public int getIntelligence() {
        return intelligence;
    }
    protected void setIntelligence(int intelligence) {
        this.intelligence = intelligence;
    }

}

public class Fighter extends CharacterClass {

    public Fighter() {
        setStrength(15);
        setVitality(15);
    }

}

public class Main {

    public static void main(String[] args) {
        CharacterClass player = new Fighter();

        System.out.println(player.getStrength());
        System.out.println(player.getIntelligence());
    }

}

This will print 15 followed by 10, because the strength has been modified by figther, but the intelligence is still the one defined in CharacterClass.

Note that you probably want your player to be more than a CharacterClass or a Figther. While you will have a lot of Figther, Rogues and Mages as PNJs, the player will have a whole other range of possibilities, like storing items in his inventory, interacting with the world, etc.

Good luck with your game !

Upvotes: 2

Related Questions