How to access A and B function that is only in a sword class and is not in Weapon interface. Because the sword, bow class are child so that they can be more extended than the weapon class.
The parent class 'Weapon' has the abstract function A from its child classes 'Sword' and 'Bow'. However, due to the unique characteristics of 'Sword' and 'Bow', 'Sword' possesses function B, while 'Bow' possesses function C."
The client can access the 'Weapon' class through the 'BattleService' class, which takes it as an argument in function A. However, it cannot access functions B and C, which are exclusive to the 'Sword' and 'Bow' classes respectively, as they are not part of the interface. How can access be granted in such cases based on dependency Inversion?
I want to output functions B and C from BattleService while adhering to the principle of dependency inversion.
public interface Weapon {
void A();
}
public class Sword implements Weapon {
@Override
public void A() {
System.out.println("Sword A");
}
public void B() {
System.out.println("Sword B");
}
public class Bow implements Weapon {
@Override
public void A() {
System.out.println("Bow A");
}
public void C() {
System.out.println("Bow C");
}
}
public class BattleService {
private Weapon weapon;
public BattleService(Weapon weapon) {
this.weapon = weapon;
}
public void A() {
weapon.A();
}
}
public class Main {
public static void main(String[] args) {
Weapon sword = new Sword();
Weapon bow = new Bow();
BattleService battleService = new BattleService(sword);
battleService.A();
battleService = new BattleService(bow);
battleService.A();
}
}