Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:22:52+00:00 2026-05-26T22:22:52+00:00

I know in Java arrays are covariant. So for example: Assume Dog is a

  • 0

I know in Java arrays are covariant. So for example:

Assume Dog is a subclass of Animal
In java the arrays are covariant making: Animal[] a supertype of Dog[]
But in java generic collections are not covariant such as: 
ArrayList<Animal> is not a supertype of ArrayList<Dog>

My question is are arrays in Ada Covariant?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:22:52+00:00Added an answer on May 26, 2026 at 10:22 pm

    I think that by “Animal[] [is] a supertype of Dog[]” you mean that Animal[42] might in fact be a Dog? If so, then the answer is No.

    In Java, variables (including array elements) are in fact references (think pointers).

    Given

    type Animal is tagged null record;
    type Dog is new Animal with null record;
    

    you can of course say

    type Plain_Array is array (Positive range <>) of Animal;
    

    but then all the elements have to be Animals.

    To get dispatching in Ada, you have to have a class-wide value to dispatch on, so you could try

    type Class_Array is array (Positive range <>) of Animal'Class;
    

    but then the compiler would tell you

    gnatmake -c -u -f covariant_arrays.ads
    gcc -c covariant_arrays.ads
    covariant_arrays.ads:8:59: unconstrained element type in array declaration
    gnatmake: "covariant_arrays.ads" compilation error
    

    (Animal and Dog objects aren’t the same size). You could try

    type Access_Array is array (Positive range <>) of access Animal'Class;
    

    which allows you to say

    AA : Access_Array := (1 => new Animal, 2 => new Dog);
    

    but then you’re left with memory management problems, because Ada doesn’t do garbage collection (at least, with any of the native code compilers I’m aware of). You could save yourself a lot of grief by using Ada.Containers.Indefinite_Vectors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to build Dynamically allocated arrays, but not how to grow them.
I know Java's generics are somewhat inferior to .Net's. I have a generic class
I would like to know how to declare 2 dimensional arrays in java script
I'm not very familiar with Java, but got the job to reverse the following
Goal I am making a Java class that will give enhanced usability to arrays,
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html Sun does not mention any complexity for their binary-search implemention. Is this a
I know that arrays in java extends Objects,So why passing them as params doesn't
Yes, I know you could use regular objects as associative arrays in JavaScript, but
I have this code to sort a text file using arrays in java, but
By using java reflection, we can easily know if an object is an array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.