0

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...");
    }
}
ryandawkins
  • 1,537
  • 5
  • 25
  • 38
  • 2
    That code will not work unless `Parent` has a print `method` too... – Stefano Sanfilippo Feb 16 '14 at 22:01
  • @delnan I just filled the code, but won't there by some typechecking errors? – ryandawkins Feb 16 '14 at 22:01
  • @RyanDawkins No. There won't be type checking errors. Here, child is a parent, so it can return itself in `setName` with no errors. – William Morrison Feb 16 '14 at 22:12
  • @WilliamMorrison As explained by Stefano above, the error comes from the fact that `Parent` has no `print`, coupled with the result of `setName` being a `Parent`. –  Feb 16 '14 at 22:29
  • @delnan That's not what OP asked. OP said he filled in required code, asked if there'd be type-checking errors. The answer is no. As long as `print` is defined in parent when he filled things out, there'll be no errors. – William Morrison Feb 16 '14 at 22:50
  • @WilliamMorrison The code *was* filled out an hour ago. The current code in the question is what OP considers filled out. –  Feb 16 '14 at 23:09

1 Answers1

1

Your code is possible in Java, and is safe. It may seem a little odd to you in implementation though. Consider:

public class Parent{
    private String name;

    public Parent setName(String name){
        this.name = name;
        return this;
    }

    public String getName(){ return this.name;}

    public void print(){
        System.out.println("Say my name!");
    }
}

public class Child extends Parent{
    public void doChildStuff(){
        //child stuff.
    }

    @Override
    public void print(){
        System.out.println(this.getName());
    }
}

Child child = new Child();
//this works as Parent defines print, and setName returns a Parent object.
child.setName("Bill gates").print();

//Compile error, as setName returns Parent, and Parent does not define 'doChildStuff.`
child.setName("Bill Gates").doChildStuff();

Your approach is fine, just be aware chaining calls won't work with any subclass of Parent which adds new methods.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • Is it possible to have a generic based off of the class that is extending the child class if the parent class is abstract? – ryandawkins Feb 17 '14 at 16:14
  • 1
    If I understand you, sure, you can do this. That's what the question you were linked to yesterday suggests. [Here it is again.](http://stackoverflow.com/questions/1069528/method-chaining-inheritance-dont-play-well-together-java) – William Morrison Feb 17 '14 at 18:02