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

  • Home
  • SEARCH
  • 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 6902741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:50:09+00:00 2026-05-27T07:50:09+00:00

The following code examples compiles but with a compiler warning class Animal{} class Dog

  • 0

The following code examples compiles but with a compiler warning

class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}

class SubArrayList<T> extends ArrayList{}

class ZiggyTest2{

    public static void main(String[] args){                 
        ArrayList<Animal> nums = new SubArrayList<Animal>();

    }

    public static void testMethod(ArrayList<Animal> anim){
        System.out.println("In TestMethod");
    }
}   

When i compile the above i get the following warning

Note: ZiggyTest2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Or if i compile it with -Xlint:unchecked i get the following warning

ZiggyTest2.java:12: warning: [unchecked] unchecked conversion
found   : SubArrayList<Animal>
required: java.util.ArrayList<Animal>
                ArrayList<Animal> nums = new SubArrayList<Animal>();
                                         ^
1 warning

If i change the initialisation of nums to

List<Animal> nums = new ArrayList<Animal>(); 

then i dont get any warnings.

Why are they behaving differently. ArrayList is a subtype of List and SubArrayList is a subtype of ArrayList so i was expecting the instantiation to be the same.

Thanks.

Edit

Also if i call the method and pass it an reference initialised generically it also produces a warning.

class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}

class SubArrayList<T> extends ArrayList{}

class ZiggyTest2{

    public static void main(String[] args){                 
        SubArrayList<Animal> nums = new SubArrayList<Animal>();
        testMethod(nums);

    }

    public static void testMethod(ArrayList<Animal> anim){
        System.out.println("In TestMethod");
    }
}

I thought compiler warnings will only appear if you mix generic code with non-generic code.

  • 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-27T07:50:09+00:00Added an answer on May 27, 2026 at 7:50 am

    Your SubArrayList<T> class only extends ArrayList (essentially ArrayList<?>), and not ArrayList<T>, making a cast from SubArrayList<T> to ArrayList<T> unchecked.

    If you change it to class SubArrayList<T> extends ArrayList<T> {}, you will not have the problem anymore 🙂

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

Sidebar

Related Questions

I took the following code from the examples page on Asio class tcp_connection :
So the following code compiles with no warnings etc with gcc, but for some
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =
Ive got the following code which I hacked together from website examples. It works
What is the difference between the following 2 examples of code: (function(){ var myFunc
Consider the following short code snippet. namespace B { public class Foo { public
The following code (which compiles and executes properly, doing what I want) is a
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
In the following code example how do the user/userParam references relate to the Customer
The C# 3.0 spec has the following code example in section 10.6.1.3 Output parameters:

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.