I want to write a class in Java that is a parent class to abstract things out of the child class.
I want to be able to use this code, but I'm not sure if it's not possible with Java.
Frodo frodo = new Frodo();
child.addGold(10).goToMordor();
But isn't this code unsafe?
public class Bilbo
{
private int gold;
public Parent()
{
// Does something awesome
}
public Bilbo addGold(int amount)
{
this.gold += gold;
return this;
}
public int getGold()
{
return this.gold;
}
}
// Child class:
public class Frodo extends Bilbo
{
// Does cool stuff
public void goToMordor()
{
System.out.println("Traveling to Mordor...");
}
}