What is the restrictions in castng...
Inthe following prog if i uncomment c = p.
It throws,
Casting.java:28 : incompatible types
found : test.Parent
required: test.Child
is it possible to cast both up and down the hierarchy ?
Thanks,
P.Jerald
Inthe following prog if i uncomment c = p.
It throws,
Casting.java:28 : incompatible types
found : test.Parent
required: test.Child
is it possible to cast both up and down the hierarchy ?
Code:
package test;
class Parent {
public static void print(){
System.out.println("Parent");
}
}
class Child extends Parent {
public static void print()
{
System.out.print("Child");
}
}
class GrandChild extends Child {
public static void print()
{
System.out.print("GrandChild");
}
}
public class Casting {
public static void main(String args[]){
Parent p = new Parent();
Child c = new Child();
GrandChild g = new GrandChild();
p = c;
// c = p;
System.out.println("Compiles Good");
}
}
P.Jerald
Comment