• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How come we can instantiate array abstract class?

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!

How come we could instantiate array of objects from abstract class? Accg to the book I am reading, we cannot instantiate/create an instance from abstract classes. From this example:






This does not produce an error. However, if I replace 'AbstractClass abstract1[] = new AbstractClass[0] ;' with
AbstractClass abstract1 = new Abstract();

I am getting error?

Could someone please help me and enlighten me with this matter?

Thanks in advance guys
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Winston!

The reason is that this is not creating an abstract class:


It is an array that can hold instances of anything that inherits from AbstractClass. What you actually put in the array have to be concrete subclasses. The reason is that maybe you want to put both ConcreteClass1 and ConcreteClass2 in the same array. So you use their common superclass - AbstractClass as the array type.
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This does not produce an error. However, if I replace 'AbstractClass abstract1[] = new AbstractClass[0] ;' with
AbstractClass abstract1 = new Abstract();



Like Jeanne says, the following statement



has not instantiated an AbstractClass type object yet. It is sometimes a commonly misunderstood thing that a instantiates Someclass.
It does not. The statement just declares and creates an array of type Someclass that can hold n number of Someclass objects.
instantiates a Someclass type.

You've already seen that you can't instantiate an abstract class or an interface.
 
Winston Liek
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow thank you for your responses. Now I understand how it can happen
Thanks for the greetings Jeanne. Actually I'm a newbie trying to study for OCAJP cert.

This forum really helps for people like me.
reply
    Bookmark Topic Watch Topic
  • New Topic