I have a java code where i am trying to print a statement by passing an object of a class to a function . Following is the code :
import java.io.*;
class Abc
{
public static void print(Object o)
{
o.name();
}
public static void main(String args[])
{
Vendor obj=new Vendor();
print(obj);
}
}
class Vendor
{
public void name()
{
System.out.println("The name of the customer is chandeep");
}
}
The error I am getting is, cannot find symbol name() in class Abc. I understand that’s an appropriate error but how do i solve it!?
It doesn’t work, because
Objectclass has no such method asname(), and you are trying to invoke it on a reference of typeObject.Just change the method signature to take
Vendorreference as parameter: –Since your method
name()is inVendorclass, and you are passing a reference ofVendor, so you don’t need to useObjectas formal parameter in your method.