|
|
super.getSuperclass() and getClass.getSuperclass() return different results! Code compiled with javac returns the same value, but code compiled with JBuilder does not! |
| JB5 also produces two different outputs, but I believe it's javac in error, not JBuilder. But, I'm not a JLS lawyer, so I used an experiment to test it.
super.method() is having your object run the superclass's method() body. It's still your object running the method, not the parent class. this.getClass.method() is calling the method on the Class object corresponding to the superclass. This may not be what you intended. That object is of type Class, not the type of 'this'. Here are two source files which illustrate the problem you're having more clearly. Run this and look at the output closely. Note that I had to comment part of Untitled2 to make it compile. You've been fooled because the method you're calling happens to exist all the way up the inheritance chain, but in my example below it does not.
package xperiment1;
public class Untitled1 {
public Untitled1() {
System.out.println("1 " + this.getClass() + " with parent " + super.getClass());
System.out.println("1 " + this.getClass() + " with parent " + this.getClass().getSuperclass());
}
public static void main(String[] args) {
Untitled1 untitled11 = new Untitled1();
}
public String getFoo() {
return "untitled1 foo";
}
}
package xperiment1;
public class Untitled2 extends Untitled1 {
public Untitled2() {
System.out.println("2 " + this.getClass() + " with parent " + super.getClass());
System.out.println("2 " + this.getClass() + " with parent " + this.getClass().getSuperclass());
System.out.println("2 " + this.getFoo() + " with parent " + super.getFoo());
// System.out.println(this.getFoo() + " with parent " + this.getClass().getFoo());
}
public static void main(String[] args) {
Untitled1 untitled12 = new Untitled2();
}
public String getFoo() {
return "untitled2 foo";
}
}
2001-Jun-14 11:57am gyles19@visi.com | |
| [Append to This Answer] | |
| 2001-Jun-14 11:57am |
| ||||||||
|
Java Community
Java Tools |
Code Central JB OpenTools |
Community Recent Threads |
Borland Chat Chat FAQ |
||||||||||||||
|
Feature Matrices 3.0 | 3.5 | 4.0 5.0 | 6.0 | 7.0 |
Shop for JBuilder |
JBuilder Downloads and reg keys |
Report Piracy | ||||||||||||||
|
Online Manuals 4.0 | 5.0 | 6.0 7.0 |
Borland DevSupport's Bug/RFE Form Quality Central Client |
JB Patches & Updates |
JBuilder FAQs TIs | ||||||||||||||
|
JBuilder Newsgroups |
Tamaracka's News Archive |
Mr. Haki's JBuilder Machine |
JGuru's JBuilder FAQ |
||||||||||||||
|
Sun's Bug Parade |
|||||||||||||||||
|
|||||||||||||||||