import class in different directory - java

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.

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.

I can see the following issues:
You are missing the 'package <a/b/c>' declaration in your classes.
Your a.IIGetResult interface should be public, otherwise it won't be visible in the 'b' package.
The Java convention is for class name to start with an upper case, thus your inner class insided PracticeClass should be named 'Inner' instead.
Your inner class should have a public constructor, so that the later can be invoked from a class extending PracticeClass defined in another package.
The overriden inner.getResult() method should be public (but out-of-topic).
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.

Your class needs to be inside of your interface. This needs to be in a file called IIGetResult.java
interface IIGetResult {
String getResult();
public class IgetResult implements IIGetResult{
#Override
String getResult() { return null; }
}
}

Related

Using interface type in a class

I'm trying to understand this concept. Where Path type is being created using Paths's class method.
I have two doubts:
How can we have Path type in class we are creating without any Path interface reference in a class (For ex, implements).
Code snippet:
Path dir = Paths.get(args[dirArg]);
The class where I have found this snippet, there is no implementation of Interface Path.
Imported packages in class:
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
import java.io.*;
import java.util.*;
In class how can we create a method which returns Interface type. For ex Paths's class get method. In Paths class there is no link that is connected to Path interface. So how its defined in Java ? How can I understand this ?
Please put some light on me...!
The get-method you call will create (and return) an implementation of your interface.
Let's say you have an interface Action:
public interface Action {
void doSomething();
}
and you have a call:
Action action = X.getAction();
With the following X-class:
public class X {
public Action getAction() {
return new Action() {
public void doSomething() {
System.out.println("done");
};
}
}
So, even though there isn't a specific class that implements Action, the getAction() method returns an (anonymous) class that implements your interface.
The code is not all that different if there is a class implementing your interface: at that point, the getAction() method might return an instance of that class.
How can we have Path type in class we are creating without any Path interface reference in a class (For ex, implements).
The real answer is, you don't need to care.
The get method returns some kind of object that implements Path, that's for sure. And you don't need to know any more information about what kind of path that is. All your code need to know is that it returns a Path, end of story.
From the outside you might not see a class that implements Path, but in fact there must be one. It's just that you don't know.
It is, however, possible to get the actual type of Path returned.
System.out.println(Paths.get("/Users/").getClass()); // getClass returns the type
This prints class sun.nio.fs.UnixPath, which does not seem to be a public class.
In class how can we create a method wich returns Interface type. For ex Paths's class get method. In Paths class there is no link that is connected to Path interface. So how its defined in Java ? How can I understand this ?
You certainly can create a method with an interface return type!
interface MyInterface { ... }
class A implements MyInterface { ... }
class B {
public static MyInterface myMethod() {
// obviously you can add more logic than this, this is just an example
return new A();
}
}
Now as a caller of that method,
MyInterface i = B.myMethod(); // you don't (need) know what actual type of object "i" is.

Find classes that does not override method

Is there any way in IntelliJ IDEA to search for classes that does not override a certain method?
Let's say I have these classes:
public abstract class BaseClass {
public void methodThatMightBeOverridden() {
}
}
public class ConcreteClassWithOverriddenMethod extends BaseClass {
#Override
public void methodThatMightBeOverridden() {
super.methodThatMightBeOverridden();
}
}
public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
}
public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
}
Is it possible in IntelliJ to find all extensions of BaseClass that does not override methodThatMightBeOverridden()? Note that I would need to find classes even though they do not directly implement BaseClass. In the example above, that would be ConcreteClassWithoutOverriddenMethod and AnotherConcreteClassWithoutOverriddenMethod.
I know how to use type hierarchy to find classes where the method is overridden, but have not found a way to do it the other way around.
I have tried googling for it without any luck. Also, this is a simplified example. In the real code we have many implementations of the sub classes, of which some does not extend the class.
I found the support for this. By standing on the method and pressing Ctrl + Shift + H (or Navigate -> Method Hierarchy in the menu), you get a nice view of all overriding methods:
Here I put all four classes in a class called Test as below, hence the "in Test" text after the class names. The picture is pretty self explanatory, but the minus sign means that the class does not override the method.
package test;
public class Test {
public abstract class BaseClass {
public void methodThatMightBeOverridden() {
}
}
public class ConcreteClassWithOverriddenMethod extends BaseClass {
#Override
public void methodThatMightBeOverridden() {
super.methodThatMightBeOverridden();
}
}
public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
}
public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
}
}
You could use Structural Search. Use a search template like this:
class $A$ extends $B$ {
void $m$();
}
Where under Edit Variables variable m has text/regexp methodThatMightBeOverridden and min+max occurrences count of 0. And variable B has text/regexp BaseClass and the Apply constraint within type hierarchy checkbox enabled.

how do i access a public method of a default class outside the package

I have a class with no modifier(default), which has a public method called mymeth. I know I could access the method when I am within the package. However I would like to access the method when I am outside the package. does anyone has an Idea on how it could be done. theoretically I think it should be possible since public method means access by the world. here is the example of my class and method:
class myclass{
public void mymeth(int i,int b){
.....
}
}
set myclass class to be public.
**FYI, Classes in Java start from upper Case letter
Directly you cannot. 'public' makes everything visible. But if you can't see the class, it's difficult to call anything. However,
You can extend the default class with a public class, eventually myMeth is exposed.
PubClass.java
package p1;
class DefClass{
public void myMeth(){
System.out.println("from myMeth!");
}
}
public class PubClass extends DefClass{
public PubClass(){
super();
}
}
MainClass.java
package p2;
class MainClass {
public static void main(String[] args) {
p1.PubClass pub = new p1.PubClass();
pub.myMeth();
}
}
output:
from myMeth!
A real practical use for this would be, overriding a public known method in that hidden class. You can implement a public method in a hidden class, so the world can call your public method (public implementation rather) without the class being exposed. For example the public method of the Object class is overridden here by DefClass:
PubClass.java
package p1;
class DefClass{
public String toString(){
return "DefClass here. Trying to explain a concept.";
}
}
public class PubClass extends DefClass{
public PubClass(){
super();
}
}
MainClass.java
package p2;
class MainClass {
public static void main(String[] args) {
p1.PubClass pub = new p1.PubClass();
System.out.println(pub.toString());
}
}
output:
DefClass here. Trying to explain a concept.
public interface SomeInterface{
public void mymeth();
}
class MyClass implements SomeInterface{
public void mymeth(){
}
}
//is in the same package as MyClass
public MyClassFactory{
public SomeInterface create(/*parameters*/){
//create instance from parameters
//For your case
MyClass instanceOfAnyClassThatImplementsSomeInterface = new MyClass(/*pass the parameters*/);
return instanceOfAnyClassThatImplementsSomeInterface;
}
}
One of the ways is already defined in answers but If you want to restrict the public access of the class then you can create an interface and access the method through it.
Set myclass as public then put it in the build path of the class you need to use myclass.
In your code, myclass has the default (package-level) access modifier. It should be declared using the public access modifier so that it is accessible outside its package. For details, read more about Controlling Access in Java.
As a side note, the Java standards require you to capitalize each word in the class name, so you should use MyClass. I recommend you the Java Conventions document.
Consider making another public class MyChild with the same package name as MyClass and expose the method from MyChild class
public class MyChild extends MyClass {
public void myTestMethod(){
super.myTestMethod
}
}
Now in your class where you want to use the method, simply use the instance of MyChild class
MyChild m = new MyChild();
m.myTestMethod();
Cheers :)

Is there any reason for public methods in a package protected class?

I wonder if it makes any difference if a method is public or package protected in a class that is package protected.
class Example {
public void test() {}
}
instead of
class Example {
void test() {}
}
I guess the maximum visibility is given by the class. And a method can only reduce the visibility and increasing the visibility has no effect.
But it's valid syntax, so perhaps I've overseen something?
If we subclass Example to a public class , then code outside the package can access test() method using the subclass instance if it is public .
Example:
package A;
class Example {
public void test() {}
}
package A;
public class SubExample extends Example {
}
package B;
import A.SubExample;
class OutsidePackage {
public void some method(SubExample e){
// Had test been defined with default access in class Example
// the below line would be a compilation error.
e.test();
}
}
If Example implemented an interface of some kind you'd have to make them public, because you can't reduce access in that case. All interface methods are, by default, public.
As written, it does nothing. If it's a subclass or interface implementation, then it may be implementing or overriding methods that are declared public elsewhere.

Java inner class and inheritance

I am reading Thinking In Java at the moment and I encountered one small problem. I am doing exercise 12 from chapter 8.
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 these .java files:
A.java
package c08;
public interface A
{
void one();
}
Pr2.java
package c082;
import c08.*;
public class Pr2
{
protected class InPr2 implements A
{
public void one() {System.out.println("Pr2.InPr2.one");}
protected InPr2() {}
}
}
Ex.java
package c083;
import c082.*;
import c08.*;
class Cl extends Pr2
{
A foo()
{
InPr2 bar=new InPr2();
return bar;
}
}
And my NetBeans IDE underlines
InPr2();
and says that:InPr2() has protected access in C082.Pr2.InPr2 and I am wondering why.
If I didn't explicitly state that constructor in InPr2 should be protected it would be only accessible in C082 package, but when I am inheriting class Pr2 shoudn't it be available in class Cl, because InPr2 is protected? Everything is fine when I change constructor to public.
The constructor of InPr2 is protected, meaning that only classes inheriting from InPr2 (not Pr2) can call it. Classes that inherit from Pr2 can see the class Pr2, but they can't call its protected members, like the protected constructor.
It should work just fine as you have it, except changing the protected InPr2() {} to public InPr2() { }. In other words "Anyone can instantiate this class IF they can see the class to begin with."
Even though the class InPr2 is accessible in Cl, its constructor is not. A protected constructor is accessible only to subclasses and classes in the same package.
Change:
Pr2.java
package c082;
import c08.*;
public class Pr2
{
protected class InPr2 implements A
{
public void one() {System.out.println("Pr2.InPr2.one");}
// This constructor was available only
// to a class inheriting form Pr2.InPr2 - protected InPr2() {}
public InPr2() {}
}
}
The constructor fromPr2.InPr2 was just available to a class if it extended Pr2.InPr2.
Protected member variables, methods and constructors are not accessible outside the package unless they are inherited.
When we try to create an object of InPr2 the compiler will show error as the protected constructor is not accessible outside the package.
The creation of an object depends on the access modifier of the Constructor too.
You can do one thing: InPr2 can be inherited inside class C.
no class in java can be protected.

Categories

Resources