import class in different directory

49,515

Solution 1

As per the quote:

Create an interface with at least one method, in its own package.

So we create IGetResult.java file in folder a:

package a;

public interface IGetResult {
    String getResult();
}

Create a class in a separate package. Add a protected inner class that implements the interface.

Now we create a class in a separate package (folder), with inner class which implements the interface:

package b;
import a.IGetResult;

public class InnterTest {
   protected class GetResultImpl implements IGetResult {
       @Override
       String getResult() { return "result"; }
   }

} 

In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return

So now we create a sub-class of InnerTest class in third separate package:

package c;
import a.IGetResult;
import b.InnterTest;

public class InnerTestSubclass extends InnerTest {

   public IGetResult getResultClass() {
      //Up-casting happens automatically since GetResultImpl is sub-class of IGetResult
      return new GetResultImpl();
   }
}

I typed it by hand, but you should get the idea. Hope that helps.

Solution 2

I can see the following issues:

  1. You are missing the 'package <a/b/c>' declaration in your classes.

  2. Your a.IIGetResult interface should be public, otherwise it won't be visible in the 'b' package.

  3. The Java convention is for class name to start with an upper case, thus your inner class insided PracticeClass should be named 'Inner' instead.

  4. Your inner class should have a public constructor, so that the later can be invoked from a class extending PracticeClass defined in another package.

  5. The overriden inner.getResult() method should be public (but out-of-topic).

  6. Your class IGetResult should be defined in a third package (c?) and should extends PracticeClass (though I must admit your instructions are a little bit confusing to me).

Aplly the above points along with @dimoniy's answer and you should be OK.

Share:
49,515
shanwu
Author by

shanwu

A fighting geek!

Updated on July 09, 2022

Comments

  • shanwu
    shanwu almost 2 years

    Actually, I am trying to finish this practice in "Think in Java" for self-learning purpose --

    Exercise 6: (2) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return.

    so I created a class named IgetResult.java under directory "a" which has a IIGetResult Interface.

    interface IIGetResult {
        String getResult();
    }
    public class IgetResult {
    } 
    

    then I create another class in another directory -- directory b

    import a.IgetResult.IIGetResult;
    
    public class PracticeClass {
        protected class inner implements IIGetResult {
            @Override
            String getResult(){ return "result";}
        }
    
        public static void main(String[] args) {
            System.out.println("practice start");
        }
    
    }
    

    In the final step, I compile the two java classes with command:

    # javac a/.java b/.java

    and get the following error:

    ./a/IgetResult.java:1: duplicate class: IIGetResult
    interface IIGetResult {
    ^
    ./a/IgetResult.java:4: duplicate class: IgetResult
    public class IgetResult {
           ^
    b/PracticeClass.java:1: cannot access a.IgetResult
    bad class file: ./a/IgetResult.java
    file does not contain class a.IgetResult
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    import a.IgetResult.IIGetResult;
        ^
    

    Please teach me go through this practice, thanks in advance.

  • cs1349459
    cs1349459 about 3 years
    Also, you need to put the package statement as the first line. Otherwise the error "class, interface, or enum expected" will be thrown.