• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why interface array can be declared without compilation error?

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I cannot instantiate interface and abstract class. But why I can create an interface array without generate compilation error?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:I know I cannot instantiate interface and abstract class. But why I can create an interface array without generate compilation error?



There is no concrete method in your interface I. The

I i2 = new I() {}


creates an anonymous inner implementation if I. The same holds for the Array. Try adding an abstract method in the Interface I and see what happens!
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding void method1(); to the body of interface, I i2=new I(){}; is an error because The type new I(){} must implement the inherited abstract method. However, I[] i3=new I[10]; is still OK.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:
There is no concrete method in your interface I. The

I i2 = new I() {}


creates an anonymous inner implementation if I. The same holds for the Array.

Not the same with array. Array of I is-not-a I. Array of I is a class by itself and it is not abstract that is why new I[10]; is valid. Check out section 10.8 of JLS for more details.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul! Thanks for pointing that out!
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see if you fully have understood the concept of an array. How many objects are created in the following code snippet



(Disclaimer: the statement creates an anonymous inner class which is not on the OCA exam. Inner/nested classes is an exam objective of the OCP exam.)
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Let's see if you fully have understood the concept of an array. How many objects are created in the following code snippet


i3 contains 10 null objects, length of i3 is 10. Is this correct?

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:
Roel De Nijs wrote:Let's see if you fully have understood the concept of an array. How many objects are created in the following code snippet


i3 contains 10 null objects, length of i3 is 10. Is this correct?

No, you are not correct!

Length of i3 is indeed 10. But you are creating an array of 10 reference variables of type I and every array element refers to null (which is not an Object). In fact, the statement creates only 1 object: the array itself. Every array IS-A Object, meaning both print-statements in this code snippet will print true

And finally to illustrate null IS-NOT-A Object, this statement prints false (if it would have been an Object, it would have printed true)

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic