You have a class Container:public class Container{ public int myInt = 3; public int getMyInt(){ return myInt; }}Then you execute the following code:Object myContainer = new Container();myContainer.getMyInt();What is the error in this code?
Question
You have a class Container:public class Container{ public int myInt = 3; public int getMyInt(){ return myInt; }}Then you execute the following code:Object myContainer = new Container();myContainer.getMyInt();What is the error in this code?
Solution
The error in this code is that you are trying to call the method getMyInt() on an object of type Object. In Java, the Object class does not have a getMyInt() method.
Here's the step by step explanation:
- You create a new instance of the
Containerclass withnew Container(). - You assign this instance to a variable
myContainerof typeObject. - You then try to call
myContainer.getMyInt().
The problem is that myContainer is of type Object, and the Object class does not have a getMyInt() method.
To fix this, you need to cast myContainer to Container before calling getMyInt(), like so:
((Container) myContainer).getMyInt();
This tells the compiler that myContainer is actually of type Container, and it's safe to call getMyInt().
Similar Questions
You have a class Container:public class Container{ public int myInt = 3; public int getMyInt(){ return myInt; }}Then you execute the following code:Object myContainer = new Container();myContainer.getMyInt();What is the error in this code?Instruction: Choose the option that best answers the question. Answer ChoicesYou cannot invoke methods of the derived class with an object of the base class typeThere are no errors in this code, so this will execute and produce the expected outputThe getMyInt() method is not publicly accessible outside of the Container classThe return type for the method specification is not the same as the actual return values
ou have a class Container that, like all other Java classes, derives from java.lang.Object:package com.skillsoft.test;public class Container { public int myInt = 3; public int getMyInt(){ return myInt; }}Then you execute the following snippet of code:Object myObject = new Container();System.out.println(myObject instanceof Container);What will be returned by this snippet of code?
You have a class Container:package com.skillsoft.test;public class Container { public Object myValue = new Object(); public void printMyValue(){ System.out.println(myValue); }}You have child classes BooleanContainer and IntegerContainer:public class BooleanContainer extends Container { public void printMyValue(){ System.out.println(myValue); }}public class IntegerContainer extends Container{ public void printMyValue(){ System.out.println(myValue); }}What can be improved about the setup of this inheritance hierarchy?Instruction: Choose the option that best answers the question. Answer ChoicesThere should be only one printMyValue() function in ContainerThe classes aren’t logically related to each other so there should be no inheritanceYou should ideally create child classes that are protectedYou should ideally create child classes that are private
class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}
You have a class Shape:public class Shape { protected String name; protected int area; protected int perimeter; public Shape(){ } public Shape(String name, int area, int perimeter){ this.name = name; this.area = area; this.perimeter = perimeter; } public int getArea(){ return area; } public int getPerimeter(){ return perimeter; }}You have a child class Circle:public class Circle extends Shape { public Circle(String name, int area, int perimeter){ super(name, area, perimeter); } public int getArea(){ return area; } @Override public int getCircumference(){ return perimeter; }}What is the error in the definition of Circle?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.