Knowee
Questions
Features
Study Tools

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

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?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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The error in this code is that you cannot invoke methods of the derived class with an object of the base class type. In Java, you can't call a method on an Object reference that the Object class doesn't have. In this case, the Object class doesn't have a getMyInt() method. You would need to cast the Object to a Container before calling 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?

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?

Select the correct answerWhat will be the output of the following Java program? final class base     {         int a;    }        class derived extends base    {        int b;        System.out.println(b + " " + a);      }        class inheritance     {        public static void main(String args[])        {            derived obj = new derived();            obj.display();             }   }OptionsRuntime Error3 32 2Compilation Error

class Base { public void Print() { System.out.println("Base"); }} class Derived extends Base { public void Print() { System.out.println("Derived"); }} class InhDriver { public static void printMsg(Base ob) { ob.Print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); printMsg(x); printMsg(y); printMsg(z); }}

You have a class Student:public class Student { public int studentId; public String studentName; public String studentGrade; public Student(int studentId, String studentName, String studentGrade){ this.studentId = studentId; this.studentName = studentName; this.studentGrade = studentGrade; }}You have a child class CollegeStudent:public class CollegeStudent extends Student {}What is the error in this inheritance setup?Instruction: Choose the option that best answers the question. Answer ChoicesYou have to specify a default, no argument constructor in the derived class so that the derived class can be instantiatedYou have to create child classes marked with the special modifier “derived” so the inheritance hierarchy itself is wrongYou have to specify a parameterized constructor in the derived class so that the derived class can be instantiatedThere has to either be a default constructor defined in Student or a constructor defined in CollegeStudent

1/2

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.