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 8221509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:56:53+00:00 2026-06-07T13:56:53+00:00

Today I come across a Function that makes me really wondering. So lets assume

  • 0

Today I come across a Function that makes me really wondering. So lets assume this simple structure for clearification.

public class Animal{

  public String getName(){ return null; }

}

public class Dog extends Animal{

  @Override
  public String getName(){
    //I'm aware that not any Dog's name is 'Pluto', but its just a Sample ;)
    return "Pluto"
  }

}

public class Cat extends Animal{

  protected final String mName;  

  public Cat(String name){
    mName = name;
  }

  @Override
  public String getName(){
     //cats have different names, because the internet loves cats
    return mName;
  }

  public void miao(){
    //just a dummy
  }
}

Now it is absolut valid to assign a Dog to an Animal Pointer, but invalid to Assign an Animal to a Dog Pointer like this:

Animal animal = new Dog(); //valid, any Dog is at least an Animal
Dog dog = new Animal();  // invalid, of course not any Animal is a Dog!

Lets Assume an AnimalCage class, where the “Magic” happens:

public class AnimalCage{

  private ArrayList<Animal> mCage = new ArrayList<Animal>();

  public addAnimal(Animal animal){
    mCage.add(animal);
  }

  // HERE is where the "Magic" happens:
  public <A extends Animal> A getAnimalByName(String name){
    //try catch block not mandatory
    try{
      for (Animal a: mCage){
        if (name.equals(a.getName()) return (A)a; 
      }
    } catch(ClassCastException cce){}
    return null;
  }
}

With the use of the AnimalCage it is possible to do this:

//all valid
AnimalCage cage = new AnimalCage();
Dog dog = new Dog();
Cat cat = new Cat("Mauzi");
Cat cat2 = new Cat("Garfield");
cage.add(dog);
cage.add(cat);
cage.add(cat2);
// and later get it back
//will return dog
Dog pluto = cage.getAnimalByName("Pluto"); 
//will find nothing and return null
Dog snoopy = cage.getAnimalByName("Snoopy);
//will raise ClassCastException and return null 
snoopy = cage.getAnimalByName("Mauzi"); 
//will return Mauzi
Animal mauzi = cage.getAnimalByName("Mauzi");

so I can Do anything of this WITHOUT casting explicit. This leads me to the assumption, that Erasures aren’t erased at Runtime, although I know better. Before I thought I have to give at least an Indicator on what to Cast like this Function:

public <A extends Animal> A getAnimalByName(String name, Class<A> animalClass){
  try{
    for (Animal a: mCage){
      if (name.equals(a.getName()) return (A)a;          }
  } catch(ClassCastException cce){}
  return null;
}

//use
Dog dog = cage.getAnimalByName("Pluto", Dog.class);

I’m really wondering on how Java lets me assign Animals on Cats/Dogs, and on what specialization of Animal it has to Cast

  • 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-06-07T13:56:55+00:00Added an answer on June 7, 2026 at 1:56 pm

    I don’t quite understand your question, but perhaps I can clarify some points:

    • Singnature such as <A extends Animal> A getAnimalByName(String name) involves a technique called type inference – i.e. actual type of A in particular invocation of getAnimalByName() is inferred from the left side of assignment.

      Note that it’s a purely compile-time feature – code such as

      <A extends Animal> A getAnimalByName(String name) { ... }
      ...
      Dog dog = getAnimalByName("foo");
      

      turns into the following code when compiled (due to type erasure):

      Animal getAnimalByName(String name) { ... }
      ...
      Dog dog = (Dog) getAnimalByName("foo");
      
    • As you can see, your code breaks type safety guarantees – it happens when you do a cast in return (A) a, and compiler emits a warning about it. It’s a basic guarentee of generics – if your code compiles without warnings, it doesn’t break type safety.

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

Sidebar

Related Questions

Today, reading Servlet 3.0 specification, I've come across a sentence: We emphasize that this
I'm a beginner in wpf, today I come across an odd issue that the
Today, I have come across a bug in my Zend Framework application. This is
I have come across this roadblock today. Maybe due to the lack of sleep/coffee,
I have come across a situation today which has me wondering about best practices.
I come across the google project http://www.20thingsilearned.com/ today and I found the canvas-based page
Today I have come across a surprising definition of a metaclass in Python here
I've come across some data where the date for today's value is 77026 and
So I've come across an issue with a site today regarding strange behavior with
Today i'm come across with the blog in msdn and i noticed how to

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.