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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:23:29+00:00 2026-06-01T18:23:29+00:00

I am a bit confused on the purpose of declaring instances as specific types.

  • 0

I am a bit confused on the purpose of declaring instances as specific types. For example,

Integer intOb1 = new Integer(3);
Object intOb2 = new Integer(4);

I know the type of intOb1 is Integer, and the type of intOb2 is Object, but what does declaring intOb2 as Object allow one to do? Use methods in Object? Can’t you already use those methods as an Integer? Or is the main purpose just to be able to “treat” intOb2 as an Object?

As you can see, I’m befuddled.

  • 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-01T18:23:31+00:00Added an answer on June 1, 2026 at 6:23 pm

    This actual isn’t called casting this is an example of polymorphism. Which allows variables to take on different types based on their inheritance relationships with other classes.

    For example, suppose you were writing a program to simulate a zoo. You would have a class called Zoo and a class called Animal. There are also several classes that extend from the Animal class: Lion, Zebra, and Elephant.

    It would be extremely useful to keep all of these objects grouped together in a single list but since they are of different types, i.e: Lion, Zebra, and Elephant, you can’t store them in a single list, you would have to maintain a separate list for each type of animal. This is where polymorphism comes into play.

    Since each of the classes Lion, Zebra, and Elephant all extend from the Animal class we can simply store them in a list of type Animal.

    code example:

    public class Zoo
    {
       private List<Animal> animals; 
    
       public Zoo()
       {
          this.animals = new ArrayList<>();
       }
    
       //notice this method takes an Animal object as a parameter
       public void add(Animal a)
       {
          this.animals.add(a);
       }
    }
    
    public abstract class Animal
    {
       private String name;
       private String type;
    
       public Animal(String name, String type)
       {
          this.name = name;
          this.type = type;
       }
    
       //all subclasses must implement this method
       public abstract void speak();
    }
    
    public class Lion extends Animal
    {
       private String animalType = "Lion";
    
       public Lion(String name)
       {
          super(name, animalType);
       }
    
       public void speak()
       {
          System.out.println("ROAR");
       }
    }
    
    //....etc for the other two animals
    
    public class TestZoo
    {
       public static void main(String[] args)
       {
          Zoo myZoo  = new Zoo();
          Lion l     = new Lion("Larry");
          Elephant e = new Elephant("Eli");
          Zebra    z = new Zebra("Zayne");
    
          myZoo.add(l);  //<-- note that here I don't pass Animal objects to the 
          myZoo.add(e);  //    add method but subclasses of Animal
          myZoo.add(z);
       }
    }
    

    Hope this helps, even with a silly example.

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

Sidebar

Related Questions

I am a bit confused at this point on what is an object, what
I am a bit confused about the purpose of the enum value Success of
I'm a bit confused on how to return an object(successfully) back to a pool.
I'm a bit confused about the new Wi-Fi Direct feature in Android ICS. Is
I'm a bit confused as to the purpose of constructor functions in javascript. I
I am new to java development ! Facing some problem , and bit confused.
I'm a little bit confused about the purpose of components and extensions folders in
I'm a bit confused about The purpose of Marker Interface Vs Attributes. Their purpose
I'm a bit confused. What is the purpose of the parameters when you initiate
I'm new to branching in git and I'm a bit confused by the behaviour

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.