Assume we have the class Product as shown belowpackage com.model;public class Product{ //some code public void display() { System.out.println("Display method"); }}Observe the code given belowpublic class Main{ public static void main(String args[]) { Answer productObj.display(); }}Fill the appropriate code, so that productObj is a reference of Type Product and the above code displays the output as "Display method".
Question
Assume we have the class Product as shown belowpackage com.model;public class Product{ //some code public void display() { System.out.println("Display method"); }}Observe the code given belowpublic class Main{ public static void main(String args[]) { Answer productObj.display(); }}Fill the appropriate code, so that productObj is a reference of Type Product and the above code displays the output as "Display method".
Solution 1
In the given code, you need to create an object of the class Product and assign it to the reference variable productObj. Here is how you can do it:
public class Main{
public static void main(String args[])
{
Product productObj = new Product();
productObj.display();
}
}
In the above code, Product productObj = new Product(); creates an object of the class Product and assigns it to the reference variable productObj. Now, you can use this reference variable to call the display() method of the Product class. When you run this code, it will print "Display method" as output.
Solution 2
In the given code, you need to create an object of the class 'Product' and assign it to the reference 'productObj'. Here is how you can do it:
public class Main{
public static void main(String args[])
{
Product productObj = new Product();
productObj.display();
}
}
In the above code, Product productObj = new Product(); creates an object of the class 'Product' and assigns it to the reference 'productObj'. Now, 'productObj' can be used to call the 'display' method of the 'Product' class. When you run this code, it will print "Display method" as output.
Solution 3
In the given code, you need to create an object of the class Product and assign it to the reference productObj. Here is how you can do it:
public class Main{
public static void main(String args[])
{
Product productObj = new Product();
productObj.display();
}
}
In the above code, Product productObj = new Product(); creates a new object of the class Product and assigns it to the reference productObj. Now, productObj can be used to call the display() method of the Product class. When you run this code, it will print "Display method" to the console.
Solution 4
In the given code, you need to create an object of the class Product and assign it to the reference variable productObj. Here is how you can do it:
public class Main{
public static void main(String args[])
{
Product productObj = new Product();
productObj.display();
}
}
In the above code, Product productObj = new Product(); creates an object of the class Product and assigns it to the reference variable productObj. Now, you can use this reference variable to call the display() method of the Product class. When you run this code, it will print "Display method" as output.
Similar Questions
the correct answerWhat will be the output of the given Java program? abstract class java { int x; abstract void display(); } class method extends java { int y; void display() { System.out.println(y); } } class Abstract_demo { public static void main(String args[]) { method obj = new method(); obj.y = 1; obj.display(); } }Options0Runtime Error1Compilation Error
What will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options02Compilation Error1
public class Code { public static void main(String[] args) { method(null); } public static void method(Object o) { System.out.println("Object method"); } public static void method(String s) { System.out.println("String method"); }}
What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}
Assuming that all the necessary imports are done, what will be the output of the following Java code? class parent { int x; void display() { System.out.println(x); } } class child extends parent { int y; void display() { System.out.println(y); } } class method_overriding { public static void main(String args[]) { child obj = new child(); obj.x=4; obj.y=5; obj.display(); } }Options435Compilation Error
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.