Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gaining access to package-access members by creating the same package name

I have a question about a sneaky way to gain access to package-access members that occurred to me. Specifically, I want to extend a class - let's call it com.acme.Foo - to add some functionality. This is pure addition: all current methods of Foo would be supported just by delegating to the superclass's method. However, there is no accessible constructor of Foo, so I can't extend the class and my implementation won't pass the "isA" test for being a Foo. There is no interface that expresses Foo-ness that I can use instead of inheritance.

This is where the sneaky thought occurred to me: Foo has a package-access constructor, so why not just create a package com.acme in my source folder and create an InheritableFoo class in that package:

package com.acme;

public class InheritableFoo extends Foo {
    public InheritableFoo() {
        super();
    }
}

Now I can implement my extension to Foo by extending InheritableFoo, since that DOES have an accessible constructor.

This feels all wrong, and somehow unethical: I'm not respecting the decision the original programmer made when he or she decided to not expose a constructor for Foo. But it could be the case that they simply adopted the "deny everything until there's a reason for it" approach. Either way, is there anything fundamentally wrong with this idea? Am I going to cause myself problems in the future if I go this route, and if so, why?

like image 446
Dave Mulligan Avatar asked Dec 18 '14 00:12

Dave Mulligan


People also ask

How do you use two different classes of same name which are present in different packages?

Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.

What are the benefits of using packages write down the steps in creating a package and using it in a java program with an example?

Advantage of Java Package 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision.

What is a package in Java explain accessibility rules for packages?

Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.

What is a package How do you create a package?

A package is a group of similar types of Classes, Interfaces, and sub-packages. We use Packages in order to avoid name conflicts. Syntax: To import a package. import package.name.*; Example: To import a package.


1 Answers

You are correct regarding the fact that you can "sneak" in a class of your own into another package and get access to package restricted elements.

This will however only as long as the author of com.acme.foo distribute his code as source or an unsealed package. Should the author of this package distribute it as a sealed jar (which is quite common I believe) your approach will no longer work.

From Sealing Packages within a JAR File

Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file.

like image 163
aioobe Avatar answered Oct 14 '22 14:10

aioobe