0

I declared an Interface like the following

public interface One {
    void setNo(int no);
}

Then I have a class to implement the interface

public class Two implements One{
    private int no;

    @Override
    public void setNo(int no){
        this.no = no;
    }
}

I will have a lot of classes to implement the interface and then I want to create the corresponding Object according to user input.

Let's say we have the classes Three, Four, Five described as the Two class.

The user picks to create the class Three. So I try to do something like the following

One base;

Class c = Class.forName("Three");
base = (One) c.newInstance();

base.setNo(5);

the call to base.setNo() fails. Am I doing this right?

Error

java.lang.ClassNotFoundException: Three
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:260)
    at com.company.Main.main(Main.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
3
  • What's the error? and is your code above even compiling? Commented Nov 23, 2014 at 13:38
  • 1
    Try using the fully qualified class name Commented Nov 23, 2014 at 13:39
  • In addition, if the code of Three is similar to Two, setNo is package private, which means you can't access it from a class that's not in the same package. Commented Nov 23, 2014 at 13:42

2 Answers 2

2

Include package name so if your class Three in package com.test then you should write this way

Class c = Class.forName("com.test.Three");
    base = (One) c.newInstance();

    base.setNo(5);
Sign up to request clarification or add additional context in comments.

1 Comment

well it is obvious, because forName method requires fully qualified class name, for example there are two classes/files with same name "List" (java.awt and java.utils) how do you think forName method will distinguish them without package name?
1

It should give compilation error:

Cannot reduce the visibility of the inherited method from One

Try to make method as public.

public class Two implements One{
    private int no;

    @Override
    public void setNo(int no){
        this.no = no;
    }
}

I don't know where are you created Three class, but I can help with your Two class.

public class Two implements One {
     private int no;

        @Override
        public void setNo(int no){
            this.no = no;
            System.out.println("No-->");
        }
        public static void main(String[] args) {
            One base;

            Class c;
            try {
                c = Class.forName("com.test.stackoverflow.Two");
                base = (One) c.newInstance();
                base.setNo(5);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.