Question

I have two questions - I'm a beginner in Java but have a huge java project :(

  1. If I implement a class (which is an interface that has two void methods) and I try to write another method in my class, it compiles but at run time the method is skipped? What could I be doing wrong?

  2. How do I run a class from another class that is not the main. So basically the main runs a class which then calls another class...

I know these questions are pretty limited in terms of content but the program is really complexed at its just impossible to explain everything :(

Any help would be greatly Appreciated :) Thanks!!

--- updated - as requested

 class FriendLists implements Results {
     public void processCommunication (Communication d)  {...}

     public void postProcess() {...}

     //the above two run perfectly

     //I don't know what to do next. 
     //I'm trying to create a link to my other class, to use the values values 
     //but when compiled and running it skips this method (and all other methods except the above two

     public void processCommunication(AggregatedDirect f) {
     //does something from the class I'm trying to run
      man = f.getNumTargets(); //getNumTargets is a value in the AggregatedDirect Class
     }

,

interface Results {
void processCommunication (Communication c) throws SAXException;
    void postProcess();

}

,

  public  class AggregatedDirect extends Communication {
ArrayList<Integer> targets;


public AggregatedDirect(Direct d) {
    super();
    targets = new ArrayList<Integer>();
    this.type = d.getType();
    this.invocSerial = d.getInvocSerial();
    this.serial = d.getSerial();
    this.usage = d.getUsage();
    this.messageType = d.getMessageType();
    this.characterID = d.getCharacterID();
    this.characterStatus = d.getCharacterStatus();
    this.locationID = d.getLocationID();
    targets.add(d.getTargetCharacterID());
    this.targetCharacterID = -1;
    this.targetCharacterStatus = -1;
    this.targetCharacterLocationID = -1;
    this.message = d.getMessage();
    this.time = d.getTime();
    this.annotation = d.getAnnotation();
}

public void addTarget(int targetCharacterID) {
    targets.add(targetCharacterID);
}

public void addTarget(Direct d){
    addTarget(d.getTargetCharacterID());
}

public int getNumTargets() {
    if (targets == null)
        return -1;
    else
        return targets.size();
}
public ArrayList<Integer> getTargets() {


     return targets;
    }
}
  • Describe class Communication here.
  • Abstract superclass of all the communication
  • subclasses.
  • in effect - processes an XML file and separates it

    • Direct extends communications // it basically one of the String Values in the XML file
Was it helpful?

Solution

If you add another method to your class, but that method is not defined in your interface, and you're using the Interface as the type when instantiating the object, then the Java runtime will not be aware that method exists.

For instance:

Class MyClass implements List {

    public void method1() {
        // do stuff
    }

    public void method2() {
        // do other stuff
    }

    public void method3() {  // not defined in Interface
        // do yet other stuff
    }

}


Interface List {

    void method1();

    void method2();

}

Now, if a service class in your code returns an object of type "List", then your calling class won't know the subtype:

 List list = getMyList();

 list.method3();  // doesn't know what this is because it's type List not type MyClass

OTHER TIPS

You have to call a method to run it, so let's say you have a method called

public void someMethod(){ 
   // method contents
}

in order to run it you'll have to call someMethod(); from within that method's scope.

To call a method from another class you generally have to create an instance or object of that class first. Let's say the class is called OtherClass and there's a method in that class called otherMethod(), in your current class you'll have to create an object of type OtherClass like this:

OtherClass otherClass = new OtherClass();

then call the other method using

otherClass.otherMethod();

If this answer is too simple let us know. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top