I was learning about packages in java. I created 2 packages, "main" and "main2". Both the packages have a class which has the same name. And in both the classes I also have a method with same name. Currently I am working with the package "main".
See the below code:
"main" package
package main;
import main2.*;
public class learning {
public static void main(String args[]) {
okay obj = new okay();
}
}
class okay{
void hello(){
System.out.println("Hello");
}
}
"main2" package
package main2;
public class okay{
public static void main(String args[]) {
}
public void hello() {
System.out.println("Hello1");
}
}
Here I am currently working with package "main". Here I am also importing the package "main2". Now I want to create an object of "okay" class of the "main2" package. Since the "main" package has already a class called okay and the same method name It is not calling the hello method of package main2.
package main;
import main2.*;
public class learning {
public static void main(String args[]) {
okay obj = new okay();
// Creating an object of okay class in package main1.
okay obj1 = new okay();
// calling the hello method of okay class in package main1.
obj1.hello();
}
}
class okay{
void hello(){
System.out.println("Hello");
}
}
But it is only calling the hello method of package main. How do I call the hello method of package main1. Is there any methods for it. Or is it impossible to call the method of same name. Pls reply. I tried my best to explain the question.