Interface and Object class
Aaron Scott-Boddendijk
aaron.scott-boddendijk at jump.co.nz
Thu May 18 19:09:00 PDT 2000
> Are Interfaces derived from Object class ?
> The following java program works, how ??
>
> class Test implements ITest{
>
> public static void main(String[] args){
> Test t = new Test();
> ITest it = t;
> Object ob = it;
> System.out.println(it.toString());
> }
>
> public void add(){
> }
> }
> ------------------------------------------
> interface ITest{
> void add();
> }
> -------------------------------------------
The assignments all have implicit casts - these are resolved by the compiler. If
the cast could not be resolved at compile-time you would be required to perform
an explicit cast... The reason that the explicit cast isn't necessary is that by definition
anything That is a 'Test' is also a 'ITest' and a 'Object'... I'm guessing that the
toString() works because Object implements interface which declares toString();
- the other possibility is a compiler hack that states that any instance must be an
Object and any dereference of an interface is accessing an Object therefore all
methods of Object are supported.
This second case is more likely as this explains the implicit cast accepted for
Object ob = it;
This implies that the compiler treats them as equivelent for the purposes of casting.
--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax
---
To unsubscribe, mail advanced-java-unsubscribe at xcf.berkeley.edu
To get help, mail advanced-java-help at xcf.berkeley.edu
More information about the Advanced-java
mailing list