kechap
kechap

Reputation: 2087

Polymorphism and creation of Object using String

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)

Upvotes: 0

Views: 481

Answers (2)

NFE
NFE

Reputation: 1187

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();
            }

        }
}

Upvotes: 1

Vilen
Vilen

Reputation: 5061

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);

Upvotes: 2

Related Questions