Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing superclass as parameter to method expecting sub class

I have an object tree that looks something like

           Ball
          /    \
  LegalBall    IllegalBall

And I have 2 methods:

class o {
AddBall(LegalBall l)
AddBall(IllegalBall i)
}

in another class I'd like to do the following:

o.AddBall(myBall);

where myBall is of type Ball. And get it to call the correct method depending on the subtype. Apparently I can't do this... the arguments are not applicable.

Does anyone know how I can achieve what I want? Or if there is a good work around

Thanks

EDIT : the application I'm trying to build is a Cricket scorecard type thing. So depending on the type of ball that is bowled various other elements should change.

my original intention was to be able to specify the ball type and runs scored from some form of UI and then create an appropriate type ball from a BallFactory and then for example when I send a no ball to the team score it will add the value onto the team score but also add the value to the no balls counter. But when i give the same ball to the Batsmens Analysis to deal with it should only score value-1 to the batsmens total..

I hope thats not too bad an explanation of my original intention.

like image 773
Rocco Avatar asked Dec 26 '08 21:12

Rocco


People also ask

Can we pass an object of a subclass to a method expecting an object of superclass as argument?

Answer: Yes, you can pass that because subclass and superclass are related to each other by Inheritance which provides IS-A property.

Can Super class access sub class methods?

No, a superclass has no knowledge of its subclasses.

Can you cast a superclass to a subclass?

You can always successfully cast a superclass to a subclass. An interface can be a separate unit and can be compiled into a bytecode file. The order in which modifiers appear before a class or a method is important. Every class has a toString() method and an equals() method.

Can we assign superclass to subclass in Java?

No. It makes zero sense to allow that. The reason is because subclasses generally define additional behavior. If you could assign a superclass object to a subclass reference, you would run into problems at runtime when you try to access class members that don't actually exist.


1 Answers

You could use the Visitor pattern.

class Basket {
    void AddBall(LegalBall l) {
        System.out.println("LegalBall added to basket");
    }

    void AddBall(IllegalBall i) {
        System.out.println("IllegalBall added to basket");
    }
}

interface Ball {
    void AddBall(Basket b);
}

class LegalBall implements Ball {
    void AddBall(Basket b) {
        b.AddBall(this);
    }
}

class IllegalBall implements Ball {
    void AddBall(Basket b) {
        b.AddBall(this);
    }
}

or to make it more general:

interface BallVisitor {
    void visit(LegalBall l);
    void visit(IllegalBall i);
}

interface Ball {
    void accept(BallVisitor v);
}

class LegalBall implements Ball {
    void accept(BallVisitor v) {
        v.visit(this);
    }
}

class IllegalBall implements Ball {
    void accept(BallVisitor v) {
        v.visit(this);
    }
}

class Basket implements BallVisitor {
    void visit(LegalBall l) {
        System.out.println("LegalBall added to basket");
    }

    void visit(IllegalBall i) {
        System.out.println("IllegalBall added to basket");
    }
}
like image 121
dalle Avatar answered Nov 14 '22 21:11

dalle