Skip to content

Tag: inheritance

Set parent class attribute in a child class

I trying to make a chess game and I will have a class for each chess piece but they will all extend class Piece. Piece class has a maxMovementDistance attribute but I would like to set that attribute in the child class (king, queen, pawn etc.) and it should also be final. What’s the best way to do this? Or

Why does the call to a parent class constructor not call the parent’s methods that are overriden in the child class?

As shown in the code below, given a superclass Parent with a method calculate() that is called in Parent’s constructor, but also overriden in the class Child, why does the implicit call to Parent’s constructor within Child’s constructor not call Parent’s calculate() instead of Child’s calculate()? Output: I would have thought that to correctly construct a Parent object, its constructor

How to call methods in different parent interfaces using child class name?

I am migrating a project from python to java. There is a multiple inheritanced python tool class, called Util(TimeUtil, MathUtil, FileUtil, …), it extend several Util classes. For example Normally, I would call Util.date_to_str(), Util.add(), Util.read_file() in python, importing class Util is enough, and I can call the static method of parent classes by the class name I want to

override graphstream’s DefaultMouseManager

I’m using GraphStream to show a map of an area and I’ve tried to inherit from the default MouseManager DefaultMouseManager and to override the mouseClicked method so that when clicking on a node the following will happened: node’s color will change. node’s label will show. node’s data will show in terminal. I do know that the method works because the

Print a value of an extended class object from mother class object

I’ve declared a table that has the mother class type and i’ve filled it with many objects that have the extended classe type, everything looks fine and the table is successfully filled, the problem is when i’m trying to access to the table values i cannot ( in the exemple i’m trying to get the salaire attribut ) MOTHER CLASS

How to show that a Catch block for the Parent-exception will handle subclasses as well

I have met this problem. I have created a class CatHandler, with 3 inner classes (ExceptionAlpha extends Exception, ExceptionBeta extends ExceptionAlpha, ExceptionGammer extends ExceptionBeta). These three exception subclasses are empty; they contain no code. All code should be written in CatHandler. Now the question is to write some code in CatHandler to show that ExceptionBeta and ExceptionGammer will be caught

Inheritance, setters and getters (Game Class Output)

How am I supposed to decrease its armor first and next the health? I want not to print the negative of armor, and I want to print if the armor gets 0, the health should be the next to decrease. For Instance in Testing Class, I use wizard and cast a spell to fireball 3 times to knight. Since knight