Questions tagged [package-private]

Package private is the default access-control modifier in Java. If a member of a class is not annotated with `private`, `protected`, or `public`, then the member is `package private` by default. When a member is `package-private`, it can only be accessed by the parent class, and other classes in the same package.

Consider the following classes:

package my.stuff;

class Foo {
  private int a;
  int b;
}

package my.stuff;

public class Bar {
  private int c;
  private int d;
}

package my.otherstuff;

public class Baz {
  private int c;
  private int d;
}

Class Foo is accessible from class Bar, but not from class Baz. This is because both Foo and Bar are in the same package (my.stuff). Also, the field Foo.b is accessible from Bar, but not from Baz for the same reasons.

48 questions
1
vote
0 answers

javadoc generate "extends Object" instead of the real class when the parent class is package private and comes from another module

My file structure. I have 2 directories next to each other. module1 and module2. module1: src\hu\package1\TestPackagePrivateClass.java: package hu.package1; abstract class TestPackagePrivateClass {…
dfritsi
  • 1,224
  • 3
  • 14
  • 24
1
vote
2 answers

How to create mock object of a class which is package private

I have a class. It has a companion object A with a factory method. class A private[somepackage](x: Int) { } object A { def createA(y: Int): A = { new A(y) } } Now I need to create the mock object of A in a scalatest file which is in a different…
user9920500
  • 606
  • 7
  • 21
1
vote
1 answer

Why private access for methods is more preferable than package-private?

Java developers always use private access level for methods which are not used outside of this class. There are known benefits of doing so but from other side we increase complexity of unit tests. In most of cases our code is not used by any other…
Aliaksei Yatsau
  • 749
  • 7
  • 12
1
vote
4 answers

What is good practice to accessing package private variables?

What is good practice to access my package-private variables from other classes in this same package? Package-private accessor String getColor() { return color; } Just accessing as field from object. String color = instanceOfClass.color; In…
Yosuo
  • 13
  • 4
1
vote
1 answer

Package-private class visible to some other packages (with same name) under a different source folder

When I was doing some testing with packages and package-private classes in Java, I noticed an interesting thing. The following is my projects source structure, the class MyTestClass.java in package com.test.pkg under source folder src is a…
k0der
  • 137
  • 1
  • 10
1
vote
1 answer

Android method with default (package) visibility called incorrectly with ART

I've a two classes in different packages like this. Base class: package com.example.artpackageprivate.base; public class Base { protected final Context mContext; public Base(final Context context) { mContext = context; } …
bio007
  • 893
  • 11
  • 20
1
vote
3 answers

Is there any way to avoid casting this type?

Consider the following class that defines and implements the Foo interface: public class MyClass { public Foo getFoo() { return new FooImpl(); } public void fooMethod(Foo foo) { final fooImpl = (FooImpl) foo; …
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
1
vote
0 answers

Bound mismatch with generics: The type is not a valid substitute for the bounded parameter

The error is "Bound mismatch: The type UserSettingsFragment is not a valid substitute for the bounded parameter of the type TabListener" and it occurs on the parametrizing of UserSettingsFragment. public class MainActivity extends Activity { …
0
votes
0 answers

How to make relation between entities from different packages and keep encapsulation?

I have problems with architecture of my project application. I wanted to avoid all classes being public (it is problem of layer packaging), so I created vertical packages as shown below on the screen-shot: packages screen-shot But I have got a…
0
votes
1 answer

Package-private member visibility in complex inheritance / package combinations - Qiuck General Rule to verify visibility

If Base and Derived classes are in different packages then package-private member from Base shall not be inherited and thus it shall be absent in Derived, that is such member shall be inaccessible both thru Base obj = new Derived() and Derived obj =…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
0
votes
0 answers

Adding an external class with the same package name and breaking encapsulation

I've run into this statement when I was reading a book on Kotlin: With Java, the encapsulation can be easily broken, because external code can define classes in the same packages used by your code and thus get access to your package-private…
stdout
  • 2,471
  • 2
  • 31
  • 40
0
votes
2 answers

Is it possible to extend a package private class defined in a 3rd party jar in Scala

Is it possible to extend a package private class in scala that is defined in a 3rd party jar. So for instance if a 3rd party jar that my code depends on has some class like so private [somepackage] A { } can I somehow subclass this class in my…
Abdul Rahman
  • 1,294
  • 22
  • 41
0
votes
1 answer

Why I cannot access a package-private class in another jar (NOT sealed)?

I've encounter a strange behaviour of Java classloader: Assuming that I submit an Apache Spark jar to a cluster, which contains an extension of HiveServer2: package org.apache.hive.service.server; public class MyOP2 extends…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
0 answers

How to block access to objects from outside the library?

I am developing a library has too many objects on android. So I must group the classes with package. I want to access A class from C but I want to prevent this objects accessible from Project X. So I can't use private-package (default) and "public"…
oyenigun
  • 587
  • 6
  • 15
0
votes
2 answers

How to make two different packages access each other's classes without allowing any other third package to access it in java?

I am making a project in netbeans and facing a problem similar to the one already asked on this site - How to share package private data between two packages in Java? , with a slight difference of let's say that application developer can see my…
Maxkiel
  • 88
  • 1
  • 9