TomRob
TomRob

Reputation: 13

Overloading methods using derived class objects referenced by base class - java

class base{}

class childA extends base{}

class childB extends base{}

I have two functions (overloaded) like this:

function(childA,childA){}

function(childA,childB){}

//main program

base a = new childA();
base b = new childB();

function(a,b);   
function(a,a);  //problem

The function calls won't compile obviously.

But is there a way to get the same effect without complicating the code too much or type checking each time the functions are called.

Note: The overloaded functions are independent of the classes. The classes are just data structures, I would rather not have any interdependent code in them.

PS. I went through quite a few topics covering similar problems, but they don't seem to address the problems mentioned above. Sorry if I missed something, (newbie, first post etc :)).

Edit :

Seems my example was a bit vague, I just wanted to understand the concept in general instead of just a solution to the immediate problem. Seems strange that the above code doesn't work, would have been a powerful feature if it did.

Ok another example, this is pretty much what I'm trying to do.

class Shape{}

class Rectangle extends Shape{
//rectangle data
}

class Circle extends Shape{
//circle data
}

Overloaded functions (members of another class)

boolean checkIntersection(Rectangle r, Circle c){}
boolean checkIntersection(Circle c, Circle c){}

//main program

Vector<Shape> shapes = new Vector<Shape>();
shapes.add(new Rectangle());
shapes.add(new Circle());
shapes.add(new Circle());

checkIntersection(shapes.get(0),shapes.get(1));
checkIntersection(shapes.get(1),shapes.get(2));

Upvotes: 0

Views: 3647

Answers (4)

Ben
Ben

Reputation: 13615

The problem is that your method takes a childA or childB object as argument and you give it a base object instead

change the method signature to take the base class as argument like so would fix the problem but you lose the polymorphism

function(base a,base b){}

what you can do instead is change the variables a and b to

childA a = new childA();
childB b = new childB();

Maybe you should have a look at method override instead of overload if you want to hold onto using base instead of childA or childB.

you define a method in base

someMethod(){
  //do something
}

and then override it in your child classes like

@override
someMethod(){
  //do something specific to childA
}

then when you do

base a = new childA();

and call

a.doSomething();

it will call the overrided method in childA

Upvotes: 3

emory
emory

Reputation: 10891

You may be interested in the Visitor pattern.

Upvotes: 0

Tony
Tony

Reputation: 2483

abstract class Base{};

class ChildA extends Base{};
class ChildB extends Base{};

public class JavaTest {

    public static void function( ChildA a, ChildA a2 ) {
        //do something
    }

    public static void function( ChildA a, ChildB b ) {
        //do something else
    }

    public static void function( Base a, Base a2 ) {
        //do something
    }

    public static void main(String[] args) {
        function( new ChildA(), new ChildA() );
        function( new ChildA(), new ChildB() );
        function( new ChildB(), new ChildA() ); //Uses function(Base, Base)
    }
}

Here's sample code that uses the 2 overloads you specify, and the generic-fied overload @Ben suggested. As mentioned in my comment, you have to cast down when using the generic overload if you want to use specific ChildA/B functions.

Upvotes: 0

emory
emory

Reputation: 10891

The following worked for me:

class user9
{
    static class base
    {
    }

    static class childA extends base
    {
    }

    static class childB extends base
    {
    }

    static void function ( childA a , childB b )
    {
    }

    static void function ( childA a1  , childA a2 )
    {
    }

    public static void main ( String [ ] args )
    {
        childA a = new childA ( ) ;
        childB b = new childB (  ) ;
        function ( a , b ) ;
        function ( a , a ) ;
    }
}

Upvotes: 1

Related Questions