4

As part of learning, here is the pathological example below that am trying to understand,

class C{}; interface I{}; class S extends C implements I{};
class B{};

With these declarations, I can say that, class C class B are immediate subclass of Object class and can access all methods of Object class from within those classes. But,

1) When i declare interface I{}; How is interface I related to Object class?

In continuation, Below are some assignment to array types, followed by subclass to super class assignment and vice-verse.

C[] c = new C[4];
S[] s = new S[6];
I[] i = new S[0];
B[] b = new B[2];

//i = new C[2]; Compile error
i =s; c=s; s = (S[])c; i = (I[])c;
i = (I[])b;  //Run-time error

I learnt that arrays are 'first class objects' and immediate subclass of Object class,

Object o = c;  // i mean `I i = c;` will not work `I[] i =c;` works 

2) With respect to above definition(syntax), What is the meaning of 'arrays are first class objects'?Because Object[] oa = c; make sense to me as class C is immediate subclass of Object class.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • for your second question : http://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java – Kanagavelu Sugumar Sep 22 '14 at 08:52
  • 1
    Think of arrays as an internal class which extends (like any other classes in Java) the `Object` class. – NoDataFound Sep 22 '14 at 08:54
  • @NoDataFound Despite i consider arrays as internal class, How would i consider reference variable `o` pointing to array `c`? Is it something like, python functions can be assigned to any name? Because after you assign to `o`, you have to again cast back back to array type to use `c`? – overexchange Sep 22 '14 at 08:58
  • 1
    Don't be confused by runtime type and compile type. If you downcast your array to an Object, it is an Object for the compiler. But at runtime, it still be a `I[]`. You can test it by doing this: `System.out.println(o.getClass());` (will print `L[I` or something like that) – NoDataFound Sep 22 '14 at 09:15

1 Answers1

4

When i declare interface I{}; How is interface I related to Object class?

From the Java Language Specification:

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

How would i consider reference variable o pointing to array c

As stated in the comments, the array is itself a subclass of Object, so the following assignment is valid:

Object o = c;

The relevant part from the Java Language Specification says:

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).

This is also what is meant by "arrays are first class objects". In Java, an array is not a special type or some special construct - it is essentially a sub class of Object with additional fields (in particular length) (and some additional compiler support to be able to syntactically describe and initialize an array).

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • why java is allowing to assign arrays to variables of type `Object`? Because to use that array again we need to typecast back to an array something like `c = (C[])o;` if we are typecasting back to array, then how arrays are first class objects? – overexchange Sep 22 '14 at 09:06
  • Essentially because the language designers decided so, by making it a sub class of `Object`. This allows to pass around arrays as Objects, while casting them back to the array when necessary. Today you should probably use `ArrayList`, btw. – Andreas Fester Sep 22 '14 at 09:09
  • Andreas, One more point, as you said, In java, an array is not a special type but a subclass of `Object` class, but we also say, `An object is a class instance or an array.` How do i relate these two points? – overexchange Sep 25 '14 at 09:30
  • Not sure if I understand your question right, but `An array is a subclass of Object class` and `An object is a class instance or an array` are the same from an inheritance perspective - the first statement views the inheritance from the sub classes point of view (`array`), the second statement views the inheritance from the super classes point of view (`Object`) – Andreas Fester Sep 25 '14 at 10:04
  • super class point of view: Do you mean, instance of `Object` class is nothing but a class instance or an array? – overexchange Sep 25 '14 at 11:28