Create a few simple classes (one extends the other)
1: class A { 2: public String prop_a1 = "property a1";
3:
4: public String method_a2() { 5: return "return from method_a2";
6: }
7: }
1: class B extends A { 2: public String prop_b1 = "property b1";
3: public String method_b2() { 4: return "return from method_b2";
5: }
6: }
Now test the methods from inheritance chain...
1: class C { 2: public static void main(String [] args) { 3: A a = new B(); // this is upcasting
4:
5: System.out.println(a.prop_a1);
6: System.out.println(a.method_a2());
7:
8:
9: /**
10: ** The following code is compile-time error
11: ** System.out.println(a.prop_b1);
12: ** System.out.println(a.method_b2());
13:
14: C:\Java\test4>javac A.java
15: A.java:24: cannot find symbol
16: symbol : variable prop_b1
17: location: class A
18: System.out.println(a.prop_b1);
19: ^
20: A.java:25: cannot find symbol
21: symbol : method method_b2()
22: location: class A
23: System.out.println(a.method_b2());
24: ^
25: 2 errors
26: **/
27:
28:
29:
30:
31: B b = new A();
32: /**
33: ** B b = new A();
34: ** The above code is "downcasting" [I stand corrected]
35: * C:\Java\test4>javac A.java
36: A.java:45: incompatible types
37: found : A
38: required: B
39: B b = new A();
40: ^
41: 1 error
42: **/
43:
44: B b = (B)new A();
45: /**
46: * Casting will compile
47: * But causes a runtime error
48: **/
49:
50: }
51: }
[Edit]
Looking at the comments below, I feel compelled to write the reason for these snippets of code. In ActionScript 3.0, the following works in standard mode (deferred type checking)
1: class ClassBase
2: { 3: }
You can subsequently create a subclass of ClassBase named ClassExtender, which has one property named someString, as follows:
1: class ClassExtender extends ClassBase
2: { 3: var someString:String;
4: }
and finally:
1: var myClass:ClassBase = new ClassExtender();
2: myClass.someString =
3: "hello";
4: // no error in ActionScript 3.0 standard mode
[Edit]
Few more:
public static void main(String[] args) {
int i = 10;
Integer I = new Integer(10);
System.out.println("I == i : "+ (I == i)); // true
System.out.println("I.equals(i) : "+ I.equals(i)); // true
long l = 10L;
System.out.println("I == l : " + (I == l)); // true
Long L = new Long((long)10);
System.out.println("I == L : Compiler Error...Incompatible datatypes");
System.out.println("I.equals(L) : " + I.equals(L)); // false
}