Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extend a Java class with one having the same name

Tags:

java

I'm working with a ridiculously large codebase and need to move a file from one package to another. However, I do not have the whole codebase locally, so I am not sure if I have found and updated every reference to the original file. In order to ensure that I don't break anything, I would like to leave the original file, and simply have it extend the new file that I created. Ideally, they'd both have the exact same name. Overtime, I plan to deprecate and remove the old file, but for now this seems like the most robust solution. However, I cannot figure out how to get it to work in Java.

Here is the new class:

package myproject.util.http;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class MyServlet extends HttpServlet
{

    public void service (HttpServletRequest  request,
                         HttpServletResponse response)
            throws ServletException, IOException
    {
        // implementation of service
    }

}

Here is the old class that extends the new:

package myproject.util.net;

import myproject.util.http.MyServlet;

public abstract class MyServlet extends myproject.util.http.MyServlet
{
// this class was deprecated, use myproject.util.http.MyServlet instead
}

Unfortunately, I get the error:

MyServlet is already defined in this compilation unit

Is this possible, or am I going to have to come up with a new name for the parent class.

like image 740
matt snider Avatar asked Mar 01 '13 00:03

matt snider


People also ask

Can you extend a class with the same name?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.

Can two classes have same name in Java?

Note that you can't import two classes with the same name in two different packages. The classes Integer and String belongs to the package java. lang but they don't need to be imported as the java.

Can two classes extend the same class Java?

However, Java doesn't support multiple inheritances. This means you can't extend two or more classes in a single class. When you need to extend two or more classes in Java, you need to refactor the classes as interfaces. This is because Java allows implementing multiple interfaces on a single class.

Can you only extend one class in Java?

So basically, extends keyword is used to extend the functionality of the parent class to the subclass. In Java, multiple inheritances are not allowed due to ambiguity. Therefore, a class can extend only one class to avoid ambiguity. Implements: In Java, the implements keyword is used to implement an interface.


2 Answers

You can do it but you need to remove the import statement.

Otherwise the compilation unit will import all the classes declared in your package to be used without the fully qualified name: this means that you wouldn't be able to distinguish between the two MyServlet, this is why it is illegal.

like image 187
Jack Avatar answered Oct 09 '22 00:10

Jack


Just remove import and have fully qualified name myproject.util.http.MyServlet

like image 25
mishadoff Avatar answered Oct 09 '22 00:10

mishadoff