Johnzzz
Johnzzz

Reputation: 119

java - find out the type of class which implements of other classes

I have a kind of specific problem, let's say, that I have

public interface A {
}

//------------------------------

public class B implements A {

    static int countx = 0;
}

//----------------------------------
public class C implements A {

    static int county = 0;
}

//----------------------------------

public class Arc {

    public A from;
    public A to;

//========================================

and now I have an object a (which is an instance of Arc) and I want to find out whether it is an instance of B or C and get to the atributes countX or countY (stg like a.from.countX) any ideas? :)

Upvotes: 0

Views: 91

Answers (6)

Amir Afghani
Amir Afghani

Reputation: 38531

Think about it like this :

What makes me want to discriminate between a B and a C ? Is it an operation? If so, just implement those operations appropriately in B vs C and let dynamic dispatch take care of the rest.

interface Operation { 
    public Result operate(String[] args);
}

class A implements Operation { 

    @Override
    public Result operate(String[] args) { 
        //I need to do some special calculations for an A...           
        for(String arg : args) { 

        }
        .
        .
        .
    }
}

class B implements Operation { 

    @Override
    public Result operate(String[] args) { 
        //i know this does nothing so return the empty result
        return Result.EMPTY;
    }
}

Resist the tempation to use instanceof. In most cases you don't need it - and its not OO.

Upvotes: 0

Anthales
Anthales

Reputation: 1148

Use instanceof and a typecast:

if (a.from instanceof B) {
    B b = (B)a.from;
    b.countx;
}

Edit: But you should really not need such a thing! If you do, you can probably redesign it to not produce ugly code like this.

For example you could let your interface A have a method getCount() and let your classes B and C implement these, by returning countx or county.

Edit2: I just noticed that your countx and county members are static! Why would you do that? static means, that they don't "act" upon instances of your class but on your class object (they are "class members"). This means that you can access and modify these variables everywhere by accessing them through A.countx or B.county; you most probably don't want this, because multiple instances of class A will share the same countx!

Upvotes: 1

Erwald
Erwald

Reputation: 2206

If you simply want to have information about the classes :

getClass().getSuperclass()

Regards, Erwald

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200168

Your current design is not good from the OOP standpoint. You need some encapsulation and polymorphism. In an OOP language, you don't explicitly check for the type of an object, you arrange for that to happen automatically via dynamic dispatch. So whatever data you need from both B and C, add a method to the interface A that will get that data and then implement the method in B and C accordingly. Also, don't use public instance fields, that breaks encapuslation.

Upvotes: 1

lrAndroid
lrAndroid

Reputation: 2854

If you have an object that is an instance of Arc, then how is it also an instance of B or C? I feel like your code is flawed and needs restructuring. For example, as Tudor pointed out, "There is no relation between the classes Arc and A in your hierarchy."

Upvotes: 0

AturSams
AturSams

Reputation: 7952

I think you could use instanceof to solve this issue

as in

if(a instanceof B) return a.countx
if(a instanceof C) return a.county

Upvotes: 1

Related Questions