Natwar Singh
Natwar Singh

Reputation: 2275

How to call other method with same name form other package in following context

How can I call a method of class B from Package.one when the same method is available in class B in Package.two? main() is in class RunAB in Package.two.

Package
 |-----one
 |      |---A.java (interface)
 |      |---B.java (implements A)
 |-----two
        |---B.java (implements A)
        |---RunAB.java

//Package.one dir

package Package.one;

public interface A {
    int area(); 
}

.

package Package.one;

public class B implements A {
    public int width;
    public int height;

    public int area() {
        System.out.println("i am form one");
        return width*height;
    }

    public B(int a, int b) {
        width = a;
        height = b; 
    }
}

//Package.two dir

package Package.two;

import Package.one.*;

class B implements A {
    public int width;
    public int height;

    public int area() {
        System.out.println("i am form two");
        return width*height;
    }

    public B(int a, int b) {
        width = a;
        height = b; 
    }
}

.

package Package.two;
import Package.one.*;


class RunAB {
    public static void main(String args[]) {
        B b = new B(10,12);
        System.out.println("area is " + b.area());

    }
}

///output ;

i am form two
area is 120

How does Java know that it needs to call the method from package two and not from Package one? Is there any role of interface A in this calling process (decision)?

Upvotes: 0

Views: 1640

Answers (3)

jlordo
jlordo

Reputation: 37813

Use the fully qualified name:

public static void main(String args[]) {
    Package.one.B b = new Package.one.B(10,12);
    System.out.println("area is " + b.area());
}

Adressing your second question, run this code:

public static void main(String args[]) {
    pack.one.B bFromOne = new pack.one.B(10,12);
    System.out.println(bFromOne.area());
    pack.two.B bFromTwo = new pack.two.B(10,12);
    System.out.println(bFromTwo.area());
}

You only have to use the fully qualified name, if that class is hidden through another import.


In future, please respect the Java naming conventions and have your packages start with a lowercase letter.

Upvotes: 4

user1839489
user1839489

Reputation:

Don't import it. When creating objects from a different package use the following format in your code

Package.one.A a = new Package.one.A();

Upvotes: 1

nanofarad
nanofarad

Reputation: 41271

Since package.one.B is not static we need to get an instance of package.one.B first. We use the fully-qualified classname:

package.one.B myBOne=new package.one.B(16,24);
System.out.println("area is " + myBOne.area());

If we were calling a static method, it would be the fully qualified name followed by the method name.

If package.one.B had a static method staticFoo( we'd use package.one.B.staticfoo()

When these classes implememt the interface they do so independently of one another and do not need to be aware that the other is also implementing it.

Upvotes: 1

Related Questions