Inheritance is a core concept in OOP (Object-Oriented Programming). It represents an IS-A relationship. It literally means in programming as it means in english, inheriting features from parent (in programming features is normally functions and variables).
Consider a class, Animal
as shown,
//Creating an Animal class with bark() as a member function.
public class Animal {
public void bark() {
System.out.println("This is an animal");
}
}
Animal
is a parent class, because the properties this class has can be extended to all the animals in general.
Consider an animal named Lion
, having a class like,
//Lion class is a child class of Animal.
public class Lion extends Animal {
@Override
public void bark() {
System.out.println("Lion here!!");
}
}
The Override
annotation is used to indicate that a method in a subclass is overriding a method of its superclass.
It's not strictly necessary but it's a best practice to use it.
Now whenever we do,
Animal animal = new Lion(); //creating instance of Animal, of type Lion
animal.bark();
Note: Initialising the Animal
class with Lion
.
The output will look like
Lion here!!
According to OOP, there are many types of inheritance, but Java supports only some of them (Multi-level and Hierarchical). To read more about it, please read this.
In this exercise you're playing a role-playing game where different types of fighters can combat each other. The game has different rules for each type of fighter. We are going to focus on two specific types: Wizards and Warriors.
For a Warrior, these are the rules:
6
points of damage if the fighter they are attacking is not vulnerable.10
points of damage if the fighter they are attacking is vulnerable.For a Wizard, these are the rules:
12
points of damage if they prepared a spell in advance.3
points of damage if they did not prepare a spell in advance.Create a new class called Warrior
.
This class should inherit from the existing Fighter
class.
Update the Warrior
class so that its toString()
method describes what kind of fighter they are.
The method should return the string "Fighter is a Warrior"
.
Warrior warrior = new Warrior();
warrior.toString();
// => "Fighter is a Warrior"
Update the Warrior
class so that its isVulnerable()
method always returns false
.
Warrior warrior = new Warrior();
warrior.isVulnerable();
// => false
Update the Warrior
class so that its getDamagePoints(Fighter)
method calculates the damage dealt by a Warrior according to the rules above.
Warrior warrior = new Warrior();
Wizard wizard = new Wizard();
warrior.getDamagePoints(wizard);
// => 10
Create another new class called Wizard
.
This class should also inherit from the existing Fighter
class.
Update the Wizard
class so that its toString()
method describes what kind of fighter they are.
The method should return the string "Fighter is a Wizard"
.
Wizard wizard = new Wizard();
wizard.toString();
// => "Fighter is a Wizard"
Update the Wizard
class to add a method called prepareSpell()
.
The class should remember when this method is called, and make sure that its isVulnerable()
method returns false
only when a spell is prepared.
Wizard wizard = new Wizard();
wizard.prepareSpell();
wizard.isVulnerable();
// => false
Update the Wizard
class so that its getDamagePoints(Fighter)
method calculates the damage dealt by a Wizard according to the rules above.
Wizard wizard = new Wizard();
Warrior warrior = new Warrior();
wizard.prepareSpell();
wizard.getDamagePoints(warrior);
// => 12
Sign up to Exercism to learn and master Java with 26 concepts, 149 exercises, and real human mentoring, all for free.