Package Protected Constructor

Package Protected Constructor

Question:
I need to extend class A which resides in a different package, butclass A has only one constructor which is package protected. I get a compile error in class B, which extends class A, saying that theclass A constructor is not visible from class B. I know this iscorrect, but how do I extend and initialize class A and access itspackage protected fields?

Answer:
If you have written the source code to class A, then you are incomplete control and can resolve the problem. If class A was intendedto be subclassed outside of its package, then you have run into adesign problem. Any class that is intended to be extended by classesoutside of its package should declare most of its members eitherpublic or protected. Members which are to be publicly accessible, butnot polymorphic should be declared final. Members which comprise theexternal interface exported by the class should be public. Memberswhich are not part of the external class interface, but which areneeded by subclasses should be declared protected. Only if you wantto limit subclassing or use to a particular package should a constructor orother members be declared package local. Additionally, in some cases,you may want classes in a package to liberally access each other’spackage local member variables for performance reasons. Finally,member functions and variables which are only to be accessed by theclass should be declared private.

It is a common practice in C++ and Java to declare a constructorprotected if you only want subclasses to be able to use the class. InJava, you may declare a class constructor package local in order tolimit its use to a particular package. If you are finding you need tosubclass such a class outside of its package, you should redeclare itsconstructor protected. If you need to instantiate such a classoutside of its package, you should redeclare its constructor public.It also may be that your class B should be placedin the same package as class A if the original design decision tolimit access to class A to within the package was sound.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes