[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check the class of an object

Posted on 2006-06-08
14
Medium Priority
?
371 Views
Last Modified: 2013-11-23
Hi,

I have an interface IntfA where ClassB and ClassC both inplements. So I have a function whose parameter is:

public void functionD(IntfA objA){

}

Can someone tell me how do I determine if objA is an object of ClassB or ClassC?

Thanks!
0
Comment
Question by:amai
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16861729
You can use getClass ()
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 16861735
Or you can also use:

if ( objA instanceof ClassB )
{
  // it is an object of class B
}

else if ( objA instanceof ClassC )
{
  // it is an object of class C
}
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 16861739
In general you shouldn't need to. But there is instanceof operator and getClass() method.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16861744
I guess you could also do:

if ( objA.getClass ().equals ( ClassB.class ) )
{
 // it is an object of class B
}

else if ( objA.getClass ().equals ( ClassC.class ) )
{
 // it is an object of class C
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16861748
Out of interest - why do you need to do this?
0
 

Author Comment

by:amai
ID: 16866735
i'm trying to do deep copy of my object, so i need to check what object is it before i can create a new obj. tks!
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16867811
If you are trying to clone (deep) the object, you should do a super.clone() in your clone method. That will ensure that you have the right object, you shouldn't be creating the new object yourself.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 16867919
Yes, and don't forget that you don't always have way to create a new instance of the known class--constructors may be private or protected. Note alseo that after the instance creation you will need to set fields. do you have an option to implement ckone() for the objects you wish to copy?

Another note. In some cases you don't really need a new instance (e.g. String, Integer, enum types)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16868000
clone () should be implemented in the implementing classes and you should just call it using objA.clone ()
0
 

Author Comment

by:amai
ID: 16868001
I have tried super.clone(), but it's doing shallow copy.......

      @Override
       public Object clone() throws CloneNotSupportedException {
          return super.clone();
        }

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16868002
You should implement it and return a deep copy.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16868057
super.clone() is only the first line of your clone method, following it you should write statements to do the actual copying.
0
 
LVL 3

Assisted Solution

by:runa_paathak
runa_paathak earned 80 total points
ID: 16868072
0
 

Author Comment

by:amai
ID: 17003355
as I started this question for how to identify the class of an object, so I'm awarding bulk of the points to the answer for this question... hope all of you don't mind...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question