Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Some sort of interface method needed?

Posted on 2005-04-01
Medium Priority
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:

B mb=new B();

//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?

Question by:Unimatrix_001
  • 2
LVL 16

Assisted Solution

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.
LVL 15

Accepted Solution

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();
                  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
            new GetCaller();

      public void run() {}

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

Expert Comment

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).

Author Comment

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

Featured Post

Independent Software Vendors: 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

578 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