Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run time error

 
kashif sohail
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic