|
Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer!
TAStudent ta; //Extends student PhDStudent phd; //Extends TAStudent Student student;
In TAStudent display() { System.out.println(“I am a TA”); } In PhDStudent display() { System.out.println(“I am a PhD student”); } In Student display() { System.out.println(“I am a regular student”); }
student = phd; // line 1 student.display(); // line 2
Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class.
I guess they want us to outprint "I am a regular student"?
|
|
On September 23 2010 00:57 Qzy wrote:Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer! TAStudent ta = new TAStudent(); //Extends student PhDStudent phd = new PhDStudent(); //Extends TAStudent Student student = new Student(); //assuming Student is not abstractIn TAStudent display() { System.out.println(“I am a TA”); } In PhDStudent display() { System.out.println(“I am a PhD student”); } In Student display() { System.out.println(“I am a regular student”); } student = phd; // line 1 student.display(); // line 2 Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class. I guess they want us to outprint "I am a regular student"?
I don't really get the question, must you assign something to student? Otherwise, if you comment out line 1, you'd get exactly what you are looking for? If you can't comment it out, you can just do
student = student;
|
On September 23 2010 01:02 Cambium wrote:Show nested quote +On September 23 2010 00:57 Qzy wrote:Okay, my uni-teacher likes to give out small brain teasers, to think about. I'm pretty good with java, but this one I couldn't solve - so please do give a hint, if anyone does know the answer! TAStudent ta = new TAStudent(); //Extends student PhDStudent phd = new PhDStudent(); //Extends TAStudent Student student = new Student(); //assuming Student is not abstractIn TAStudent display() { System.out.println(“I am a TA”); } In PhDStudent display() { System.out.println(“I am a PhD student”); } In Student display() { System.out.println(“I am a regular student”); } student = phd; // line 1 student.display(); // line 2 Modify, if necessary, line 1 in order to get the call to display() in line 2 run the method in the base class. I guess they want us to outprint "I am a regular student"? I don't really get the question, must you assign something to student? Otherwise, if you comment out line 1, you'd get exactly what you are looking for? If you can't comment it out, you can just do student = student;
It had to be cast, or method call - can't change ie student = student.
|
public class Student { public void display() { System.out.println("I am a regular student."); } }
public class TAStudent extends Student { public void display() { System.out.println("I am a TA."); } }
public class PhDStudent extends TAStudent { public void display() { System.out.println("I am a PhD student."); } }
TAStudent ta; PhDStudent phd; Student student;
student = phd; student.display();
This way is easier to picture the whole. Im short on time, gotta get the bus in 5 minutes, so I will be trying to help more when I get to my University, if no one has answered yet.
|
I'll just this line in (it's an extra line i didn't paste)
"Whatever modification you perform, be it a casting, another method call, etc., the variable student MUST appear on the left side of the statement and the variable phd MUST appear on the right side of the same statement."
|
Ok I'm more of a C# programmer, so I might be wrong. However in C# you must use virtual and override keywords to make inheritance work for methods properly. So if the same applies for Java, then there is no need to modify line 1 - the program is correct. The student.display() always calls the base method no matter which class it was instanced to.
|
Hmm... I remember having something like this in my class a year ago. I'm not sure if I'm right but here is my thought process.
During compile time, Java only checks to see if types are valid and if there is a method call for that type. This means during compile time it checks to see if a Student can be a PhDStudent in line 1 and if the class Student has a display method. I am pretty sure both lines are valid.
During runtime in line 1, you are trying to store a PhDStudent into a Student. (This is where it gets iffy for me). I think you can cast phd to Student so then the variable student will be treated as a Student. Then when it calls on student.display(). It will recognize that student is still Student type and call on the base display method.
|
On September 23 2010 01:24 LastWish wrote: Ok I'm more of a C# programmer, so I might be wrong. However in C# you must use virtual and override keywords to make inheritance work for methods properly. So if the same applies for Java, then there is no need to modify line 1 - the program is correct. The student.display() always calls the base method no matter which class it was instanced to.
Yeah usually you write a @override, but that's not the point . The methods _are_ overridden Nicely spotted tho, sir!
|
On September 23 2010 01:24 LastWish wrote: Ok I'm more of a C# programmer, so I might be wrong. However in C# you must use virtual and override keywords to make inheritance work for methods properly. So if the same applies for Java, then there is no need to modify line 1 - the program is correct. The student.display() always calls the base method no matter which class it was instanced to.
Java methods allow override by default.
|
On September 23 2010 01:30 NoUShutUp wrote: Hmm... I remember having something like this in my class a year ago. I'm not sure if I'm right but here is my thought process.
During compile time, Java only checks to see if types are valid and if there is a method call for that type. This means during compile time it checks to see if a Student can be a PhDStudent in line 1 and if the class Student has a display method. I am pretty sure both lines are valid.
During runtime in line 1, you are trying to store a PhDStudent into a Student. (This is where it gets iffy for me). I think you can cast phd to Student so then the variable student will be treated as a Student. Then when it calls on student.display(). It will recognize that student is still Student type and call on the base display method.
PhDStudent is a subclass of Student, regardless whether the phd object is casted to Student or TAStudent, the instance will always be of the PhD class. This is the underlying principle of polymorphism.
|
As far as I know, there is no way to invoke Student's display method through the phd object.
This is what I would do:
student = phd == null ? student : student;
It satisfies the requirement of having student on the left side of the = operator, and phd on the right.
You might also be able to do something tricky with reflection by using the phd object to get the Student class's full name.
|
Probably cheating but... student = phd.getClass().getSuperClass().getSuperClass().newInstance();
though as written it seems impossible/pointless because phd is null so you'd just use the logic of phd == null ?... explained in the post above mine.
|
as someone above said, either comment out the first line, or if you need to keep it as student = phd, you can do something cute like
student = (phd.super(phd)).super(phd);
|
Isn't it just
student = (Student)phd;
Seems really simple
|
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
|
On September 23 2010 01:51 gale_f0rce wrote:Isn't it just student = (Student)phd;
Seems really simple
Nooo, that would just throw out "I'm a phd student" .
|
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!
|
On September 23 2010 01:54 Qzy wrote:Show nested quote +On September 23 2010 01:51 gale_f0rce wrote:Isn't it just student = (Student)phd;
Seems really simple Nooo, that would just throw out "I'm a phd student" .
No, the variable student is now of type Student, since (Student)phd is of type Student. This means that calling student.display() prints “I am a regular student.”
|
On September 23 2010 02:00 gale_f0rce wrote:Show nested quote +On September 23 2010 01:54 Qzy wrote:On September 23 2010 01:51 gale_f0rce wrote:Isn't it just student = (Student)phd;
Seems really simple Nooo, that would just throw out "I'm a phd student" . No, the variable student is now of type Student, since (Student)phd is of type Student. This means that calling student.display() prints “I am a regular student.”
It doesn't work like that =/. The reference to the object thinks it's a student, but the object itself is still a phd student.
Put it like this if you had PhdStudent phd = new PhdStudent(); Student s = (Student)phd; phd.displayName();
What would the 3rd line put out? Clearly not "I Am a regular student". Yet Both line 1 and line 2 point to the same object so how could they have different outputs.
|
|
|
|