-1

I need some advice regarding my doubt. I need to declare a function in a class/ file and to define it in another class is it possible to do so?

My instance is I have a billing module integrated to my application. I am just accepting some parameters to this module from different modules and process it in a function declared and defined in the billing module. When I am incorporating this billing module to all other modules I need to run some set of code in each module which will be unique for each module. So kindly suggest me a way to declare a function in billing module and it can be definable in all other modules and it should be executable in billing module..

akshaivk
  • 427
  • 5
  • 24

4 Answers4

2

Use Interfaces

  • Declare your method in that interface
  • Implement the interface in your class(using implements clause)
  • and define them in the class

See here And also an example here (from one of the questions asked here on StackOverflow)

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
2

It sounds like you should create an interface that declares the methods, and then have whatever classes you want implement that interface.

In billing module

public interface SomeInterface
{
    public void someMethod (...);
}

In other modules :

public class SomeClass1 implements SomeInterface
{
    public void someMethod (...)
    {
        ....
    }
}

public class SomeClass2 implements SomeInterface
{
    public void someMethod (...)
    {
        ....
    }
}

Now, if your billing module has references to objects of classes of the other modules, you can call someMethod (...) for each of them.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

This is what an interface is used for.

We can create interface to write the method declaration and then we can implement that interface.Whenever we implement an interface then it is required to override all the methods that are in the interface in the implementing class.

For eg : syntax

public interface NameOfInterface
{
   //Any number of final, static fields
   //Any number of abstract method declarations\
}

//this is an interface

interface Animal {

   public void eat();
   public void travel();
}
//class is implementing the interface
public class MammalInt implements Animal{

   public void eat(){
      System.out.println("Mammal eats");
   }

   public void travel(){
      System.out.println("Mammal travels");
   } 
}
kirti
  • 4,499
  • 4
  • 31
  • 60
1

Is this what you trying to achieve!! Billing module : define a interface which accepts some parameters to process. Implement this interface somewhere in billing module. In Other modules: accept billing module interface object and call the function to process the supplied parameters.

BILLING MODULE:

public interface myinterface{
  public void process(Object parameter1);
}

public class myinterfaceImpl implements myinterface{
public void process(Object parameter1){
  // process parameter1
}
}

Other Modules
myMethod(myinterface myinterfaceObj){
myinterfaceObj.process(Object myparam1)
}
Rahul Winner
  • 430
  • 3
  • 16