0
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));
TomRob
  • 13
  • 1
  • 4
  • Additionally, the Java standard is to use UpperCamelCase when naming classes. – Tony Sep 15 '11 at 17:49
  • knew that..but it hasn't really become second nature. I use it only when I'm coding something seriously. – TomRob Sep 15 '11 at 18:04

4 Answers4

3

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

Ben
  • 13,297
  • 4
  • 47
  • 68
  • Is there a reason why you wrote it twice? – Tony Sep 15 '11 at 17:36
  • wouldn't that defeat the purpose of overloading ? About sharing an interface could you elaborate, please ? – TomRob Sep 15 '11 at 17:39
  • im sorry, deleted my post because my answer was plain wrong, should be better now – Ben Sep 15 '11 at 17:40
  • Overloading is mainly used in Java to account for optional parameters (in my experience). This often results in the creation of what are known as helper methods. – Tony Sep 15 '11 at 17:46
  • The problem you would have with having the generic `function(Base arg0, Base arg1)` is now you have manage casting objects down if you want to use subclass functionality. – Tony Sep 15 '11 at 17:48
  • Actually I have an array of base class references pointing to derived class objects, (not just a and b) so I can't change the variable type. Either I will have to check their types and cast them or.. is there another way? – TomRob Sep 15 '11 at 17:52
  • maybe you should tell what you exactly want to achieve with this polymorphism and childclasses – Ben Sep 15 '11 at 18:03
  • like explaining the purpose of base, childA and childB – Ben Sep 15 '11 at 18:21
1

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 ) ;
    }
}
emory
  • 10,725
  • 2
  • 30
  • 58
0
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.

Tony
  • 2,473
  • 1
  • 21
  • 34
  • Thanks, this is how I've implemented it currently. What I wanted to know is,if there is a way to let the compiler figure it out without explicitly checking the type and down casting, seems its not possible :( – TomRob Sep 15 '11 at 18:01
  • You should check out this question: http://stackoverflow.com/questions/380813/downcasting-in-java – Tony Sep 15 '11 at 18:07
0

You may be interested in the Visitor pattern.

emory
  • 10,725
  • 2
  • 30
  • 58