?
Solved

Some sort of interface method needed?

Posted on 2005-04-01
4
Medium Priority
?
201 Views
Last Modified: 2010-03-31
Hi all,

I've got a normal class that does a couple of things, nothing fancy. I want an interface method, so that:

///A.java
B mb=new B();

///B.java
//When something happens in this class, call an interface method (Z for instance) in this class.

However, when the method Z in B.java is called, I want the exact same method to be called in A.java. It seems like I need some interface method, where you can't create an instance of class B unless the class creating the instance (A) has this interface method (Z) in it... Anybody any ideas on how I can do this?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:imladris
imladris earned 100 total points
ID: 13681985
You seem to be wanting to have a method in class A, that will only create an instance of B, if class A (the class itself) contains method Z.....

But since A is doing the creating, why would the method in A not "know" whether there is a Z method in the class or not?

There are methods that will tell you what interfaces a class implements (Class.getInterfaces), and the Reflection stuff can tell you all kinds of other things about classes. However, most "normal" code does not need this kind of obscure functionality.

Perhaps if you explain in more detail *what* you are trying to solve (rather than *how*) we might be able to suggest something more concrete.
0
 
LVL 15

Accepted Solution

by:
aozarov earned 400 total points
ID: 13681996
This is one way to disable creating an object unless that object implements a specific interface:

public class GetCaller
{
      public GetCaller()
      {
            String className = new Exception().getStackTrace()[1].getClassName();
            System.out.println(className);
            
            try
            {
                  Class clazz = Class.forName(className);
                  // check if implements Runnable
                  if (!Runnable.class.isAssignableFrom(clazz))
                        throw new RuntimeException("Only runnable can create an instance");
            }
            catch (Exception ex)
            {
                  throw new RuntimeException("Could not find caller class");
            }
      }

// comment out "implements Runnable" to get the exception
public class Caller implements Runnable
{
      Caller()
      {
            new GetCaller();
      }

      public void run() {}

      public static void main(String args[]) throws Exception
      {
            Caller c = new Caller();
      }
}      
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13682003
BTW, I agree with imladris that most normal code will not need such functionality and this sort of problems can be solved by a better object oriented design.
But still, the above logic will work if needed (in those rare cases).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 13683061
Agreed, it does go against the grain so to speak of OO design, but that is what I need... thank you both. :-)
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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