|
New idea... just type it into a java compiler and test it out >_>
|
On September 23 2010 01:55 Cambium wrote:Show nested quote +On September 23 2010 01:47 Logo wrote: Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
I like this, except you have to fight Java generics to get it working properly!
Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception.
public class Runner { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student = new Student(); @SuppressWarnings("unused") TAStudent ta = new TAStudent(); PhDStudent phd = new PhDStudent();
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd .getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } }
Output:
I am a regular student.
Reflection is such a hack =\
|
On September 23 2010 02:02 NoUShutUp wrote: New idea... just type it into a java compiler and test it out >_>
logo's right; it's the basic principles of polymorphism.
|
On September 23 2010 02:04 Cambium wrote:Show nested quote +On September 23 2010 01:55 Cambium wrote:On September 23 2010 01:47 Logo wrote: Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
I like this, except you have to fight Java generics to get it working properly! Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception. public class Runner { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student = new Student(); @SuppressWarnings("unused") TAStudent ta = new TAStudent(); PhDStudent phd = new PhDStudent();
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd .getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } }
Output: I am a regular student.
Reflection is such a hack =\
Wont work.. Im getting the exception. What does reflection do?
TAStudent ta = new TAStudent("1"); PhDStudent phd = new PhDStudent("2"); Student student = new Student("2"); try { student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } catch (IllegalAccessException ex) { Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex); }
|
On September 23 2010 02:04 Cambium wrote:Show nested quote +On September 23 2010 02:02 NoUShutUp wrote: New idea... just type it into a java compiler and test it out >_> logo's right; it's the basic principles of polymorphism.
I know it is... it just bugs me cause I don't remember :p
|
student = true ? student : phd;
|
On September 23 2010 02:44 FreeZEternal wrote: student = true ? student : phd;
Read whole thread... . student and phd needs to be on the left and right side, respectively.
|
On September 23 2010 01:52 Qzy wrote:Show nested quote +On September 23 2010 01:50 AcrossFiveJulys wrote: student = (phd.super(phd)).super(phd); Okay, give me like 1 hour to figure out what the heck that does :D
It doesn't compile
|
On September 23 2010 02:51 Qzy wrote:Show nested quote +On September 23 2010 02:44 FreeZEternal wrote: student = true ? student : phd; Read whole thread... . student and phd needs to be on the left and right side, respectively.
I see at least one instance of student on the left and one instance of phd on the right. Isn't that what the rule is saying? Unless you u saying that student cannot be on the right hand side at all?
|
On September 23 2010 03:34 Slithe wrote:Show nested quote +On September 23 2010 02:51 Qzy wrote:On September 23 2010 02:44 FreeZEternal wrote: student = true ? student : phd; Read whole thread... . student and phd needs to be on the left and right side, respectively. I see at least one instance of student on the left and one instance of phd on the right. Isn't that what the rule is saying? Unless you u saying that student cannot be on the right hand side at all?
I think the point is to apply the object address to student-variable, and then some how call the objects super-super-method .
|
On September 23 2010 02:31 Qzy wrote:Show nested quote +On September 23 2010 02:04 Cambium wrote:On September 23 2010 01:55 Cambium wrote:On September 23 2010 01:47 Logo wrote: Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
I like this, except you have to fight Java generics to get it working properly! Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception. public class Runner { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student = new Student(); @SuppressWarnings("unused") TAStudent ta = new TAStudent(); PhDStudent phd = new PhDStudent();
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd .getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } }
Output: I am a regular student.
Reflection is such a hack =\ Wont work.. Im getting the exception. What does reflection do? TAStudent ta = new TAStudent("1"); PhDStudent phd = new PhDStudent("2"); Student student = new Student("2"); try { student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } catch (IllegalAccessException ex) { Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex); }
What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem).
|
On September 23 2010 05:17 Cambium wrote:Show nested quote +On September 23 2010 02:31 Qzy wrote:On September 23 2010 02:04 Cambium wrote:On September 23 2010 01:55 Cambium wrote:On September 23 2010 01:47 Logo wrote: Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
I like this, except you have to fight Java generics to get it working properly! Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception. public class Runner { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student = new Student(); @SuppressWarnings("unused") TAStudent ta = new TAStudent(); PhDStudent phd = new PhDStudent();
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd .getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } }
Output: I am a regular student.
Reflection is such a hack =\ Wont work.. Im getting the exception. What does reflection do? TAStudent ta = new TAStudent("1"); PhDStudent phd = new PhDStudent("2"); Student student = new Student("2"); try { student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } catch (IllegalAccessException ex) { Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex); }
What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem).
Yeah sorry, it takes a string - does that matter?
|
On September 23 2010 05:26 Qzy wrote:Show nested quote +On September 23 2010 05:17 Cambium wrote:On September 23 2010 02:31 Qzy wrote:On September 23 2010 02:04 Cambium wrote:On September 23 2010 01:55 Cambium wrote:On September 23 2010 01:47 Logo wrote: Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
I like this, except you have to fight Java generics to get it working properly! Going along with Logo's idea, the problem is that you need to add a try/catch block or make your caller method throw an Exception. public class Runner { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Student student = new Student(); @SuppressWarnings("unused") TAStudent ta = new TAStudent(); PhDStudent phd = new PhDStudent();
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd .getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } }
Output: I am a regular student.
Reflection is such a hack =\ Wont work.. Im getting the exception. What does reflection do? TAStudent ta = new TAStudent("1"); PhDStudent phd = new PhDStudent("2"); Student student = new Student("2"); try { student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).newInstance(); student.display(); } catch (IllegalAccessException ex) { Logger.getLogger(PhDStudent.class.getName()).log(Level.SEVERE, null, ex); }
What's the exception? It's probably throwing an exception because the constructors take in parameters (you never specified this in the original problem). Yeah sorry, it takes a string - does that matter?
Yes it does, because the newInstance() method throws IllegalAccessException if the class or its nullary constructor is not accessible. In this case, because you have an explicit constructor, we cannot access the default constructor.
|
To fix it, you need to retrieve the constructor from the Class object and pass in the argument in the Constructor object's newInstance() method
student = ((Class<Student>) ((Class<TAStudent>) ((Class<PhDStudent>) phd.getClass()).getSuperclass()).getSuperclass()).getDeclaredConstructors()[0].newInstance({"1"});
|
|
|
|