File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes run time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "run time error" Watch "run time error" New topic
Author

run time error

kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
public class Parent {
public void method(){
System.out.println("method from parent");
}
}
public class Child extends Parent {
public void method(){
System.out.println("method in child");
}}

public class use{
public static void main(String [] ar) {
Parent p=new Parent();
Parent c=new Child();
Child cc=(Child) new Parent();
p.method();
c.method();
cc.method();
}
}
why it gives run time error
Kalidas Pavi
Ranch Hand

Joined: Nov 20, 2000
Posts: 42
too lazy to run the code and try...
You are not supposed to have more than one public class in a .java file. This code will not compile!!
Kalidas
Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
Kaladis,
I've run the code (after eliminating the multiple public classes). The runtime error occurs at the line:
Child cc=(Child) new Parent();
The resulting error message reads:
java.lang.ClassCastException: Parent
at Use.main(Use.java:24)
Exception in thread "main"
I'm not sure why one would want to cast the Parent object as a Child at this point. Of course, by eliminating the cast you get a compile error. Any thoughts on what is trying to be accomplished here?


Allen
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
It's quite correct for a runtime error to be thrown at this point, as opposed to a compile-time error.
Remember that p, c, and cc are all *references* to objects in memory. As such, p may OR MAY NOT be referencing a "Child" object. Whether it is or not is unknown at compile-time - hence no compile-time error.
At run-time, we find it is not - it's referencing a Parent. Now, a Child "is a" Parent, but a Parent is NOT a child. Therefore, you cannot make a Child variable reference a Parent object - Hence your run-time error when you try to do this.
Think of the following:
class Animal {}
class Dog extends Animal {}
class Fish extends Animal {}
Animal a = new Fish(); // Legal since Fish "is a" Animal
Fish = (Fish) a; // OK at compile-time and run-time
Dog = (Dog) a; // Runtime Error, since 'a' is actually a Fish
kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
public class StaticOverridingTest {
public static void main(String s[]) {
Child c = new Child();
c.doStuff(); // This will invoke Child.doStuff()
Parent p = new Parent();
p.doStuff(); // This will invoke Parent.doStuff()
p = c;
p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff()
}
}
class Parent {
static int x = 100;
public static void doStuff() {
System.out.println("In Parent..doStuff");
System.out.println(x);
}
}
class Child extends Parent {
static int x = 200;
public static void doStuff() {
System.out.println("In Child..doStuff");
System.out.println(x);
}
}
dear java gurus here is an other version of code what i m trying to ask is in this code there r two static methods both in parent and child namel (do stuff) and as they r static
i know if u invoke parents type variable p.dostuff() it will invoke parents dostuff() at it is declared static and can't be overridden .)
now see this code
public class StaticOverridingTest {
public static void main(String s[]) {
Child c = new Child();
Child nc=new Child();
c.doStuff(); // This will invoke Child.doStuff()
Parent p = new Parent();
p.doStuff(); // This will invoke Parent.doStuff()
p=c;
nc=(Child) p;
p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff()
}
}
class Parent {

public static void doStuff() {
System.out.println("In Parent..doStuff");

}
}
class Child extends Parent {

public static void doStuff() {
System.out.println("In Child..doStuff");

}
}
this code runs althoug it assings nc (child variable ) =parent's p
and it give result like
G:\java>java StaticOverridingTest
In Child..doStuff
In Parent..doStuff
In Parent..doStuff
plz any comment as it is child referencing parent and it is compiling and no error at run time
2 ther is no static but result is again parent not child's dostuff.
thanx in advance for all pain
and plz do ignore comments as i have copied the code without eliminating them
kashif
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Kashif,
The reason the Parent and Child classes work that way in your previous example is because of the static keyword... This tells Java that the variable or method belongs to the class as a whole rather than an object created from that class. So if you are casting an object to a Parent type and then calls a static method in Parent... Well, Java is going to go, "They mean to call the static method in Parent, since all objects of type Parent have this same static method."
Hope this helps,
Nathan


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run time error
 
Similar Threads
Downcasting
Q on variable Initialization
Help
same method inheritance
Problem -- Virtual Method Invocation