Solved

Calling a class not included in your application package

Posted on 2004-08-02
10
158 Views
Last Modified: 2010-03-31
Hi all,

I hope I can explain this clearly and that someone can assist me with this issue.

I have an application that needs to call a class from a dependant application.   However, I need to alleviate the need of having the dependant code included in the applications release package, yet still compile as though it were there.

I was instructed to create an interface for the class that I will be dependant on, and have that class implement the interface of my application.  Then store the class name in a property file and use that property to instantiate the class name in my code and call the necessary methods at that time.

So, there are two problems I have.  One, is the interface design.  The dependant package has relatively simple public methods, but there is one method that is dependant on another class in that package. i.e., the dependant classes are similar to this:

class Test1 {
 public void addMethod() {
  // they have code here
 }

 public void testMethod () {
  //they have code here
 }

 public void exampleMethod () {
   //again code here
 }

 public Test2 createMethod () {
  //code here that uses another class
}
}

class Test2 {
 Test2 () { //constructor which I call }
 //additional code
}

Now I'm supposed to create an interface for Test1 for the exampleMethod and the createMethod...but like I said the createMethod references another class.  Do I need to create an interface for the Test2 class?  How do I do that, if you can't put constructors in interfaces?

Then in my code, I need to reference my interfaces and instantiate the Test1 class via the classname defined in the properties file, then call the methods defined in the interface:

//get property value
//instantiate the object
Test1 test1 = new Test1();
test1.exampleMethod();
Test2 test2 = test1.createMethod();

Hopefully that made sense!  Can anyone help me using the example above to show me how the code would look and work correctly?

Thanks
0
Comment
Question by:sapientconceptions
[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
  • 4
  • 3
  • 2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11700445
what class (referring to your example) do you not have access to?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11701387
You do not have to create an interface for the Test2 class. You can simple do:

public interface Test1Interface
{
    public void addMethod();
    public void testMethod();
    public void exampleMethod();
    // You could also declare the following but it is not necessary
    public Test2 createMethod();
}

0
 
LVL 92

Expert Comment

by:objects
ID: 11701653

public interface A
{
   public void method();
}

public class AImpl implements A
{
   public void method()
   {
   }
}


then to create instance use:

String classname = "AImpl";
A instance = (A) Class.forName(classname).newInstance();
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:sapientconceptions
ID: 11703480
Thanks all.  

Objects,
Test1 and Test2 are the two dependant classes that I must call in my code, that I will not be included in my application at deployment (they will be made available later).  So I need to make sure it builds fine prior to the code being included -- hence the declarations of the interface and my use of it.

Girionis, you say I don't have to create an interface for Test2, but if Test2 isn't a part of my application and I reference it in the interface, wouldn't the compiler throw an error because it can't find the class?
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 250 total points
ID: 11703555
> but if Test2 isn't a part of my application and I reference it in the interface, wouldn't the
> compiler throw an error because it can't find the class?

You will have to import it in order to use it.

import mypackage.package.Test2;

public interface Test1Interface
{
    // method declaration here.
}

public class Test1 implement Test1Interface
{
...
...
}
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11710862
you'll need to change the Test1 classes createMethod signature then

public Test2Interface createMethod () {

and your interfaces will look like:

class Test1Interface {
 public void addMethod();

 public void testMethod ();

 public void exampleMethod ();

 public Test2Interface createMethod ();
}

interface Test2Interface {
 //additional methods
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11900386
:)
0
 

Author Comment

by:sapientconceptions
ID: 11908920
Thanks guys, sorry for the delay in passing out the points, I forgot the question was still open.  I've split the points between the two of you.  Enjoy.
0
 
LVL 92

Expert Comment

by:objects
ID: 11908938
Thats ok, thanks for closing :)
0

Featured Post

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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

740 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