Java-Handling Identical Classes

Handling Identical Classes

When we import multiple packages it is likely that two or more packages contain classes with identical names. Example :

We may import and use these packages like :

Since both the packages contain the class DataClass. compiler cannot understand which one to use and therefore generates an error. In such instance, we have to be more explicit about which one we intend to use. Example :

Program

sdsd


Package Mypack1 Source

package mypack1;

public class DataClass {
    
    public void display() {
        
        System.out.println("MyPack1");
    }
}

Package Mypack2 Source

package mypack2;

public class DataClass {
    
    public void display()
    {
        System.out.println("MyPack2");
    }
}

Program Source

import mypack1.*;
import mypack2.*;

public class Javaapp {
    
    public static void main(String[] args) {
        
        mypack1.DataClass da1 = new mypack1.DataClass();
        da1.display();
        mypack2.DataClass da2 = new mypack2.DataClass();
        da2.display();
    }
}

Leave a Comment