Interfaces and Implementing classes

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1160 Views - Last Post: 11 November 2011 - 03:33 PM Rate Topic: ***-- 2 Votes

#1 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Interfaces and Implementing classes

Posted 10 November 2011 - 02:38 AM

Hai,

Can someone tell me a way to find the classes that implement a particular interface.
I have an interface called "check", and I have a number of classes implementing it. I want to find out which of these classes implement the interface "Check". I have coded the following:
     public void FindImplementingClasses(){
        
        Class[] impclass= Check.class.getClasses();
        System.out.println(impclass);
}



But the output am getting is
[Ljava.lang.Class;@49bdc9d8
[Ljava.lang.Class;@4b0ea9ba

Its not giving the classname. Can you give me some ideas on how to do it, please

Is This A Good Question/Topic? 0
  • +

Replies To: Interfaces and Implementing classes

#2 fromTheSprawl  Icon User is offline

  • Bloodborne
  • member icon

Reputation: 522
  • View blog
  • Posts: 2,102
  • Joined: 28-December 10

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 02:56 AM

Can't you edit/create a toString() method to show the name of the method in your classes?
By the way you're printing an array. Why not try looping with your array, then use getClass().getName()?

Sample:
for(int x = 0;  x < impclass.length ; x++){
System.out.println(impclass[x].getClass().getName());
//or since you already have an array of Classes do this
System.out.println(impclass[x].getName());
//whichever works.
}



^^
Was This Post Helpful? 0
  • +
  • -

#3 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 03:05 AM

I am modifying the question. Actually, I have a set of Tabs in a Tabbed Pane. Each of these Tabs implement the interface called "check". Each Tab is a separate class in the Package. I want to find which of these tabs implement the interface. I think something like getInstanceOf() would do, but not sure of how to implement it properly.
Was This Post Helpful? 0
  • +
  • -

#4 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 05:08 AM

View PostfromTheSprawl, on 10 November 2011 - 02:56 AM, said:

Can't you edit/create a toString() method to show the name of the method in your classes?
By the way you're printing an array. Why not try looping with your array, then use getClass().getName()?

Sample:
for(int x = 0;  x < impclass.length ; x++){
System.out.println(impclass[x].getClass().getName());
//or since you already have an array of Classes do this
System.out.println(impclass[x].getName());
//whichever works.
}



^^



Its not printing anything if i implement those
Was This Post Helpful? 0
  • +
  • -

#5 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 07:23 AM

I have got a start, can anyone guide me further please?

for(int i=0;i<jTabbedPane1.getTabCount();i++)
jTabbedPane1.getComponentAt(i);



Now, I have to check whether each component, i.e. each tab is implementing the interface. How should I proceed

This post has been edited by rosebrit3: 10 November 2011 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 08:11 AM

Is it necessary to do it programmatically?
If yes this is a beginning:
if(myTab instanceof MyInterface){
   System.out.println(myTab.getClass().getName());
}


If no you can use your IDE to get the classes. I.e. in Eclipse rightclick on the interface and "Open Type Hierarchy"
Was This Post Helpful? 0
  • +
  • -

#7 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 08:12 AM

Solved:
 public void FindImplementingClasses(){
           
      int tabCount=jTabbedPane1.getTabCount();
      for(int i=0;i<tabCount;i++){
          
            Component index= jTabbedPane1.getComponent(i);
            if (index instanceof Check) {
             System.out.println("Implemented");
              
          }


Was This Post Helpful? 0
  • +
  • -

#8 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 08:20 AM

View PostVeitch, on 10 November 2011 - 08:11 AM, said:

Is it necessary to do it programmatically?
If yes this is a beginning:
if(myTab instanceof MyInterface){
   System.out.println(myTab.getClass().getName());
}


If no you can use your IDE to get the classes. I.e. in Eclipse rightclick on the interface and "Open Type Hierarchy"



Thanks for your valuable help
Was This Post Helpful? 0
  • +
  • -

#9 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 09:48 AM

View Postrosebrit3, on 10 November 2011 - 08:12 AM, said:

Solved:
 public void FindImplementingClasses(){
           
      int tabCount=jTabbedPane1.getTabCount();
      for(int i=0;i<tabCount;i++){
          
            Component index= jTabbedPane1.getComponent(i);
            if (index instanceof Check) {
             System.out.println("Implemented");
              
          }



It should be
Component index= jTabbedPane1.getComponentAt(i);
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10624
  • View blog
  • Posts: 18,179
  • Joined: 19-March 11

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 10:22 AM

Class.getInterfaces() should do the work.
Was This Post Helpful? 2
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Interfaces and Implementing classes

Posted 10 November 2011 - 10:33 PM

View PostVeitch, on 10 November 2011 - 11:11 AM, said:

Is it necessary to do it programmatically?
If yes this is a beginning:
if(myTab instanceof MyInterface){
   System.out.println(myTab.getClass().getName());
}


If no you can use your IDE to get the classes. I.e. in Eclipse rightclick on the interface and "Open Type Hierarchy"

Yark !!! Thera are better alternatives
Was This Post Helpful? 0
  • +
  • -

#12 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Interfaces and Implementing classes

Posted 11 November 2011 - 01:15 AM

View Postpbl, on 10 November 2011 - 10:33 PM, said:

View PostVeitch, on 10 November 2011 - 11:11 AM, said:

Is it necessary to do it programmatically?
If yes this is a beginning:
if(myTab instanceof MyInterface){
   System.out.println(myTab.getClass().getName());
}


If no you can use your IDE to get the classes. I.e. in Eclipse rightclick on the interface and "Open Type Hierarchy"

Yark !!! Thera are better alternatives

Then why don't you tell them instead of just complaining?
No one learns a thing if you just vomit all over the place.

Edit:

View Postjon.kiparsky, on 10 November 2011 - 10:22 AM, said:

Class.getInterfaces() should do the work.

From my understanding the TO wanted it the other way around: Finding the classes that implement the interface and not finding the interfaces that are implemented by the class.

This post has been edited by Veitch: 11 November 2011 - 01:45 AM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Interfaces and Implementing classes

Posted 11 November 2011 - 06:52 AM

View PostVeitch, on 11 November 2011 - 04:15 AM, said:

View Postpbl, on 10 November 2011 - 10:33 PM, said:

Yark !!! Thera are better alternatives

Then why don't you tell them instead of just complaining?
No one learns a thing if you just vomit all over the place.

Sorry, but I just can't imagine why you would need, at runtime, to figure out which classes implement a certain Interface. Most probably denotes a poor design.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10624
  • View blog
  • Posts: 18,179
  • Joined: 19-March 11

Re: Interfaces and Implementing classes

Posted 11 November 2011 - 07:13 AM

View PostVeitch, on 11 November 2011 - 03:15 AM, said:

View Postpbl, on 10 November 2011 - 10:33 PM, said:

View PostVeitch, on 10 November 2011 - 11:11 AM, said:

Is it necessary to do it programmatically?
If yes this is a beginning:
if(myTab instanceof MyInterface){
   System.out.println(myTab.getClass().getName());
}


If no you can use your IDE to get the classes. I.e. in Eclipse rightclick on the interface and "Open Type Hierarchy"

Yark !!! Thera are better alternatives

Then why don't you tell them instead of just complaining?
No one learns a thing if you just vomit all over the place.

Edit:

View Postjon.kiparsky, on 10 November 2011 - 10:22 AM, said:

Class.getInterfaces() should do the work.

From my understanding the TO wanted it the other way around: Finding the classes that implement the interface and not finding the interfaces that are implemented by the class.

Class[] impclass= Check.class.getClasses();
for (Class c: impclass)
  System.out.println(c.getName()+": "+ c.getInterfaces().contains(i)?"yes":"no");



Where I is the interface you're interested in.
Should do it, right?

Though I agree with pbl, I'd find it easier to address this problem if I understood why someone was interested in this.
I can only think that it's either for practice, or for some sort of code analysis application. The latter might be interesting to know more about.
Was This Post Helpful? 0
  • +
  • -

#15 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Interfaces and Implementing classes

Posted 11 November 2011 - 10:49 AM

View Postpbl, on 11 November 2011 - 06:52 AM, said:

View PostVeitch, on 11 November 2011 - 04:15 AM, said:

View Postpbl, on 10 November 2011 - 10:33 PM, said:

Yark !!! Thera are better alternatives

Then why don't you tell them instead of just complaining?
No one learns a thing if you just vomit all over the place.

Sorry, but I just can't imagine why you would need, at runtime, to figure out which classes implement a certain Interface. Most probably denotes a poor design.

I agree with that, but that is maybe a poor design of the TOs code which we only see a part of. We can't say much about it without more information. You were citing my code, not the TO, and I don't see anything that is so bad in my code that you have to vomit about it. You gave john a +rep, so you seem to agree with his approach. What is the advantage of his code over mine? Let me know that, I want to learn and this really confuses me.

Quote

Where I is the interface you're interested in.
Should do it, right?

Yes it does. But you have to call contains() for an array which loops through the array. What is the advantage over instanceof? However I don't know how instanceof is implemented. So there might be something I am not aware of.

Quote

Though I agree with pbl, I'd find it easier to address this problem if I understood why someone was interested in this.
I can only think that it's either for practice, or for some sort of code analysis application. The latter might be interesting to know more about.

I agree with that too.

This post has been edited by Veitch: 11 November 2011 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2