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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:08:27+00:00 2026-06-12T20:08:27+00:00

Object aThing = new Integer(25); The method call aThing.intValue() is a compiler error. Why

  • 0
Object aThing = new Integer(25);

The method call aThing.intValue() is a compiler error. Why doesn’t polymorphism work in this case?

Also there’s a related statement in my textbook that is a bit convoluted:

The type of reference, not the type of the object referenced, determines that operations can be performed.

Can you briefly elaborate on that?

Where as Computer[] labComputers = new Computer[10]; works with polymorphism

public class Computer {
    int ram;
    public Computer(int rm){
        ram= rm;
    }
    public String toString(){
        String result = "ram is " + ram;
        return result;
    }
}


public class Notebook extends Computer{
int size;
public Notebook(int rm, int sz){
    super(rm);
    size = sz;
}
@Override
    public String toString(){
        String result = super.toString() + " size is " + size;
        return result;
    }

}

Added:
I believe somewhere in the middle, there would be

labComputer[1] = new Notebook(2,15);
labComputer[2] = new Computer(2);

For the method call labComputers[1].toString(), polymophism ensures that the correct toString is called. In my mind labComputer[1] = new Notebook(2,15); is equivalent to Object o = new Integer(25);. But polymorphism worked for my Computer example not in the Object-Integer example. Why?

  • 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-12T20:08:28+00:00Added an answer on June 12, 2026 at 8:08 pm

    It won’t work because your variable is from Object class, so you can only use methods in the Object class. If you want to use it as an Integer, you should first do a (down) type casting:

    Integer aInteger = (Integer)aThing;
    //it could maybe work
    aInteger.intValue();
    

    Now, why could maybe work? Because downcasting could throw a ClassCastException if the type casting won’t work.

    Based in your example, I would post a basic code to show how polymophism works:

    class Animal {
        public void move() {
            System.out.println("I'm an animal and I can move.");
        }
    }
    
    class Cat extends Animal {
        //this means a Cat would change the move behavior from the Animal instance
        @Override
        public void move() {
            System.out.println("I'm a cat and I can move.");
        }
    }
    
    class Dog extends Animal {
        //this means a Cat would change the move behavior from the Animal instance
        @Override
        public void move() {
            System.out.println("I'm a dog and I like to run.");
        }
        public void bark() {
            System.out.println("I can bark!");
        }
    }
    
    public class AnimalTest {
        public static void main(String[] args) {
             //it will take the behavior of the Animal class reference
             Animal animal = new Animal();
             //it will take the behavior of the Cat class reference
             Animal cat = new Cat();
             //it will take the behavior of the Dog class reference
             Animal dog = new Dog();
             //this will invoke the Animal#move method
             animal.move();
             //this will invoke the Cat#move method because it was overriden in the Cat class
             cat.move();
             //this will invoke the Dog#move method because it was overriden in the Dog class
             dog.move();
             //this line won't compile if uncommented because not all animals can bark.
             //dog.bark();
             //if you want to make the dog barks, then you should use the downcasting
             ((Dog)dog).bark();
             //note that this will only work for Dog class reference, not for any other class
             //uncomment this line and the code will compile but throw a ClassCastException
             //((Dog)cat).bark();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Object doesn't support this property or method It's this line. pthumb = $(#pthumb).attr(src); Does
Object aThing = new Integer(25); aThing.toString(); Is it the toString of the Object or
Object aThing = new Integer(25); According to my text book, the expression (Integer) aThing
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references
object[] objArray = new object[]{blah, 4, whatever}; foreach(var value in objArray) vs. foreach(object value
object ReassignTest extends App { class X(var i : Int) def x = new
Let me preface this question with first stating that I'm new to GUI interfaces:
I have some Scala code roughly analogous to this: object Foo { val thingA
I tried to Solution suggested here , but it didn't work in my case.
Object.preventExtensions and Object.seal prevent unknown properties from being added to an object, but those

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.