how to get the immediate superclass of a given object

Y

yog

Hi all,
I have a query.plz answer this if possible.

Suppose there 4 classes

class a{}

class b extends a{}

class c extends b{}

Class d extends a{}


public class implementor
{
m1(Object obj)
{
}
}

THIS METHOD IS CALLED FROM SOMEWHERE ELSE AND THE OBJECT OF ONE THESE
ABOVE FOUR CLASSES IS SENT AS PARAMETER.NOW I WANT TO FIND THAT THE
OBJECT IS OF EXACTLY WHICH CLASS.BECAUSE IF U USE instanceof THEN THIS
WOULD BE TRUE EVEN FOR SUPERCLASSES

eg. suppose the object is of class c then
obj instanceof A --true

obj instanceof B --true

obj instanceof C --true

Now HOW DO U FIND THE OBJECT IS OF EXACTLY WHICH CLASS


THANKS
YOGESH
 
A

Andy Flowers

yog said:
Suppose there 4 classes

class a{}

class b extends a{}

class c extends b{}

Class d extends a{}


public class implementor
{
m1(Object obj)
{
}
}

Reflection is the true way to do this kind of thing, but in your case this
could work (with no syntax checking...) as you know how the heirarchy works.

String getType(Object obj)
{
if( obj instanceof d)
{
return "d";
}
else if( obj instanceof c)
{
// it's not a b or a
return "c";
}
else if( obj instanceof b)
{
return "b";
}
else if( obj instanceof a)
{
// it's not a b or c or d so...
return "a";
}
}
 
O

Owen Jacobson

Hi all,
I have a query.plz answer this if possible.

Suppose there 4 classes

class a{}

class b extends a{}

class c extends b{}

Class d extends a{}


public class implementor
{
m1(Object obj)
{
}
}
}
THIS METHOD IS CALLED FROM SOMEWHERE ELSE AND THE OBJECT OF ONE THESE
ABOVE FOUR CLASSES IS SENT AS PARAMETER.NOW I WANT TO FIND THAT THE OBJECT
IS OF EXACTLY WHICH CLASS.BECAUSE IF U USE instanceof THEN THIS WOULD BE
TRUE EVEN FOR SUPERCLASSES

eg. suppose the object is of class c then
obj instanceof A --true

obj instanceof B --true

obj instanceof C --true

Now HOW DO U FIND THE OBJECT IS OF EXACTLY WHICH CLASS

I know you didn't mean to shout, but typing messages in all-caps (or, in
this case, mostly-caps) is considered mildly rude. As you're asking for
help, being rude is not in your interest.

That said: first of all, if you find you need to know the exact class of
an object which may be a subtype of a given class, you probably have a
design problem. Ideally you should never need this information; try to
move the class-dependent behaviour into the classes. Why don't you
describe what you're doing and why you're doing it? There might be a
better way.

But: you can always get a Class instance from any object using the
Object#getClass() method:

Class objClass = obj.getClass ();
if (A.class == objClass) {
// Handle objects of class A
} else if (B.class == objClass) {
// Handle objects of class B
} else if (C.class == objcClass) {
// Handle objects of class C
} else {
// do something sane if it's some other subclass.
}

The else clause there is important; if any of those classes are non-final
then you can't ever be sure that nobody has created a class D that fits in
the hierarchy that isn't A, B, or C. You might think you can; write the
else anyways, if only to develop better habits.

Owen
 
T

Thomas Weidenfeller

yog said:
THIS METHOD IS CALLED FROM SOMEWHERE ELSE AND THE OBJECT OF ONE THESE
ABOVE FOUR CLASSES IS SENT AS PARAMETER.NOW I WANT TO FIND THAT THE
OBJECT IS OF EXACTLY WHICH CLASS.BECAUSE IF U USE instanceof THEN THIS
WOULD BE TRUE EVEN FOR SUPERCLASSES

First, please read and follow

http://www.catb.org/~esr/faqs/smart-questions.html#writewell

Many people will not even consider answering you because of your
shouting. Also, beginner's questions are best asked in comp.lang.java.help.
eg. suppose the object is of class c then
obj instanceof A --true

obj instanceof B --true

obj instanceof C --true

Now HOW DO U FIND THE OBJECT IS OF EXACTLY WHICH CLASS

Instanceof tests the type, and of course the answer is correct. What you
probably want is a test like

obj.getClass().equals(A.class)

Note, however, that relying on instanceof or getClass() in the above way
in some program design is usually considered bad style. There are
usually much better ways to achieve the desired result. instanceof and
the like are useful as band-aids and duct tape. If two existing things
don't fit together, this is often a practical way to "fix" it. But if
you develop your own stuff from scratch it is a bad idea to rely on this
stuff.

/Thomas
 
T

Tor Iver Wilhelmsen

yog said:
Now HOW DO U FIND THE OBJECT IS OF EXACTLY WHICH CLASS

Er, use getClass().

e.g.

if obj.getClass() == b.class will give false for an object of type a,
c or d.
 
P

P.Hill

Thomas said:
There are
usually much better ways to achieve the desired result.

The visitor pattern comes to mind as perfect in this case.
I just wrote the following code which results in:

I am a visit.A which is either a visit.A or a subclass of visit.A.
I am a visit.B which is either a visit.A or a subclass of visit.A.
I am a visit.C which is either a visit.A or a subclass of visit.A. I am
the coolest one of all!
I am a visit.D which is either a visit.A or a subclass of visit.A.

Notice how only some of the classes overrode the original behavior.

Change the code to (1) do something useful in each "visitMe" routine
and (2) change everything to have names relevant to what your are doing.

package visit;

public class WhoIsWho {
public static void main(String[] args) {
whoIs( new A() );
whoIs( new B() );
whoIs( new C() );
whoIs( new D() );
}

// This is the one the OP wanted to have the logic in, but we'll just
// ask each class to do its own specific work.
public static void whoIs( A a ) {
System.out.println( a.iAm() );
}
}

class A{
public String iAm() {
return "I am a " + this.getClass().getName() +
" which is either a visit.A or a subclass of visit.A.";
}}
class B extends A{}

class C extends B{
public String iAm() {
return super.iAm() + " I am the coolest one of all!";
}
}

class D extends A{}

-Paul
 
T

Thomas G. Marshall

Owen Jacobson coughed up:
I know you didn't mean to shout, but typing messages in all-caps (or,
in this case, mostly-caps) is considered mildly rude. As you're
asking for help, being rude is not in your interest.

That said: first of all, if you find you need to know the exact class
of an object which may be a subtype of a given class, you probably
have a design problem.

Yes, bingo, absolutely, ditto, what he said, I agree, etc....
 
A

Andrew McDonagh

Thomas said:
Owen Jacobson coughed up:



Yes, bingo, absolutely, ditto, what he said, I agree, etc....

This very strange, just this week on comp.lang.c++ there's been someone
wanting to do something as equally wrong. They wanted a :

public:
DerivedClass getMe();

method in their base class. :)

Weird world this week.
 
T

Thomas G. Marshall

Andrew McDonagh coughed up:
This very strange, just this week on comp.lang.c++ there's been
someone wanting to do something as equally wrong. They wanted a :

public:
DerivedClass getMe();

method in their base class. :)

Weird world this week.

Somewhat related: In one of my emails with Bjarne Stroustrup (only a few, so
please don't think I'm "name dropping" :) ) I asked him why there was no
super() in C++. He said that he was against it, because specifically naming
a class was all you needed. But he did point out that after he released the
C++ language design to consortium, there /had/ been strong discussions about
adding an "inherited::". I found it interesting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top