1

I have an interface source code which I must create a class for. The interface includes a user-defined object which is only declared in the interface, and must be overridden and defined in my own class. My class is later used in a separate file with main to call its methods.

source code (pseudo code):

package folder1.subfolder;

public interface IObj {
   void setVal(int val);
   int getVal();
   obj someFunc(obj obj2);
   obj anotherFunc();
   ...
}

I need to write a class which should define obj used in the method declarations in IObj (I know obj should be capitalized, but that's how it was given in the source code). The class file is placed in the same folder that contains folder1, and I don't have any errors importing the package.

My class (pseudo code):

import folder.subfolder.*;

public class obj implements IObj {
   private int objValue;
   obj(int val) {
      objValue = val;
   }
   obj() {
      objValue = 0;
   }
   @Override
   public void setVal(int val) {
      this.objValue = val;
   }
   @Override
   public int getVal() {
      return this.objValue;
   }
   @Override
   public obj someFunc(obj obj2) {
      //perform logic on this obj and obj2, return as new obj
   }
   @Override
   public obj anotherFunc() {
      //perform logic on this obj, return as new obj
   }   
   ...
}

I don't have any errors with the details of the class methods, but I hope this gives an idea of how I'm trying to use the interface in my class. When I attempt to compile my class, I get this message:

IObj.java:9: error: cannot find symbol
  
    obj someFunc(**obj** obj2);
 
  symbol: class obj
  location: interface IObj

I'm assuming this has something to do with how the object isn't a recognized type (like int or float). I've tried including all of the files in a project (I'm using JGrasp), triple checking file location and import code, including the interface in my class file (more of a last ditch attempt), and considered upcasting to the IObj in my obj constructors (although I'm not sure that would work either.)

I'm wondering if there was a way for the compiler to ignore the object in the interface as it is defined in my class, or if I've missed a step in defining the object in my class which causes an error, or maybe another approach I haven't considered yet! In this case I can't change the source code, so I'm looking for a way to implement the source code in a different way. I appreciate any suggestions!

nolanish
  • 11
  • 1
  • 1
    I think you'd benefit from using generics here. – JustAnotherDeveloper Sep 02 '20 at 16:21
  • It seems like the compiler cannot find type `obj` in interface `IObj`. – Turing85 Sep 02 '20 at 16:23
  • I think the issue you have here is that the inteface `IObj` is given in pseudo code, as in the comments you give. Its definition is required for your class to extend it, however it is relying on your class to be defined. A bit of a chicken and egg situation. So either use Object as the type in `IObj` or use generics as suggested by @JustAnotherDeveloper – Bill Naylor Sep 02 '20 at 16:31
  • @BillNaylor the actual source code isn't pseudo code, I was just simplifying for clarity. The only things I changed were object/method names, which now seems redundant. In the source code, obj is named bit, and the interface is IBit, etc. Thanks for the suggestion. – nolanish Sep 02 '20 at 16:54
  • @JustAnotherDeveloper. Can I use generics without changing the source code? – nolanish Sep 02 '20 at 16:54
  • @nolanish: Well, no. The code as you have posted it won't work, so obviously you need to make changes to it. [This](https://docs.oracle.com/javase/tutorial/java/generics/types.html) will teach you all you need to know about generics. – JustAnotherDeveloper Sep 02 '20 at 16:56
  • @nolanish, I think my explanation still stands though, whether you call it `obj` or `bit`, it needs to know where to start. Your base interfaces/classes need to be using standard java classes/interfaces ... and it would be good to gen up on Generics ;-) – Bill Naylor Sep 02 '20 at 21:08

0 Answers0