Class Visibility of Protected Members between Packages

Hi,
I'm developing a system that consists of package A, which in turn contains packages B and C. My database access class is in a package by itself (package A.B) and I want anyone who uses it to provide a username and password. In order to do this, my system has a class called Config.java which resides in package A. When Config.java is instantiated, it will read the contents of some configuration xml file on the system and set protected variables for the username and password.
My problem is the following:
1. Config.java is not visible to any other classes in other packages unless it is public
2. If Config.java is made public, then anyone can inherit from it and have access to its protected members.
3. The other classes (in package A.C) must have access to the username and password in order to supply it to the database access class.
How can I get around this?
Thanks,
Ama

What is your concern, what someone will hack your
database by deriving
from Config?
Is that a reasonable fear -- couldn't they just look
in the XML file you mention?Well, hopefully that xml wont be so accessible, I'm thinking it will be necessary in case the user wants to modify any database settings through the application configuration screens.
I guess what my question is really, is where would a large application that needs to access a database usually store the username and password information? Surely it cant be hardcoded in the application somewhere? What if the user wants to change the connection info?

Similar Messages

  • Accessing protected members

    Hi,
    Can one of you please clarify the following doubt.
    CLASS A
    package com;
    public class A {
         protected String name = "JAVA";
         protected void m1(){
              System.out.println("m1");     
    CLASS B
    package com.abc;
    import com.A;
    public class B extends A {
         public static void main( String [] args ) {
              B b = new B();
              b.mt();
         public void mt() {
              System.out.println("name --> " + name);
              m1();
    CLASS C
    package com.abc.xyz;
    import com.abc.B;
    public class C extends B {
         public static void main( String [] args ) {
              C c = new C();
              c.mttt();
         public void mttt() {
              System.out.println("name --> " + name);
              m1();
    ====================================
    Now in one of the books I read that a protected member of parent class (say A) is accesible thru inheritance in a subclass in different package (say B). Perfect!
    But also says that another class (say C) inheriting the above subclass either in a the same or different package cannot access the protected members of the parent class (A).
    Surprisingly above code compiles and runs perfectly on my machine. Checked the JDK version and is 1.3 but the books is for 1.4
    Can some one please clarify which is correct.
    Thanks in Advance.

    I was expecting a compilation error (in class C as it cannot access the protected members of class A) according to the book I read. But I observed that it is compiling and running perfectly.
    Following the code again for clarity:
    CLASS A
    package com;
    public class A {
         protected String name = "JAVA";
         protected void m1(){
              System.out.println("m1");
    CLASS B
    package com.abc;
    import com.A;
    public class B extends A {
         public static void main( String [] args ) {
              B b = new B();
              b.mt();
         public void mt() {
              System.out.println("name --> " + name);
              m1();
    CLASS C
    package com.abc.xyz;
    import com.abc.B;
    public class C extends B {
         public static void main( String [] args ) {
              C c = new C();
              c.mttt();
         public void mttt() {
              System.out.println("name --> " + name);
              m1();
    }Message was edited by:
    talk2key

  • What is the diffrence between package javax.sql and java.sql

    Is javax designed for J2EE?
    And when to use package javax?

    Hi,
    What is the diffrence between package javax.sql and java.sql?The JDBC 2.0 & above API is comprised of two packages:
    1.The java.sql package and
    2.The javax.sql package.
    java.sql provides features mostly related to client
    side database functionalities where as the javax.sql
    package, which adds server-side capabilities.
    You automatically get both packages when you download the JavaTM 2 Platform, Standard Edition, Version 1.4 (J2SETM) or the JavaTM 2, Platform Enterprise Edition, Version 1.3 (J2EETM).
    For further information on this please visit our website at http://java.sun.com/j2se/1.3/docs/guide/jdbc/index.html
    Hope this helps.
    Good Luck.
    Gayam.Srinivasa Reddy
    Developer Technical Support
    Sun Micro Systems
    http://www.sun.com/developers/support/

  • How to import a class, which is not in a package?

    Hi all,
    I have the following problem:
    A class A is not in a package. In another class B, which is in a package (let's say package X) I want to use the class A. Without importing the class A, I get the error "Cannot resolve symbol".
    Using the import statement import A; gives the error ". expected".
    How can I import that class?
    Best wishes to all ...
    Heiko

    > I have the following problem:
    A class A is not in a package. In another class B,
    which is in a package (let's say package X) I want to
    use the class A. Without importing the class A, I get
    the error "Cannot resolve symbol".
    I think not putting a class in a package is a bad practice. Avoid that.

  • Unable to access classes in third party jar files packaged with my ejb.jar

    I have packaged some third party jar files in my xxx_ejb.jar file.  The third party jar file contains classes that are accessed by some custom classes which are in turn accessed by my bean classes.  I get the following error when my custom classes attempt to use any of the classes in one of the third party jar files:
    java.lang.NoClassDefFoundError: com/Ostermiller/util/CSVParser
    Shouldn't I be able to access classes in jar files that are packaged with my xxx_ejb.jar file?  I see the xxx_ejb.jar file as a resource of the application classloader that contains it but I don't see the third party jar files.

    As per the Java EE specification, additional JAR files should be packaged on the EAR level. Only WAR files' WEB-INF/lib folder is scanned for JAR files to be added to the classloader.

  • Ant tools: converter java card -  class file  not belonging to the package!

    Hi
    I have another problem with converter. Could you help me please again if you have the time?
    I have created a directory when I save the class files generated by javac. After That , I use convert in this way:
    <convert
    JCA="true"
    EXP="true"
    CAP="true"
    packagename="testUn/classes/testUn"
    packageaid="0x01:0x01:0x01:0x01:0x01:0x01:0x01:0x01:0x02:0x00"
    majorminorversion="1.0"
    verbose = "true">
    <AppletNameAID
    appletname="testUn.classes.testUn.PorteAppUn"
    aid="0x01:0x01:0x01:0x01:0x01:0x01:0x01:0x01:0x02:0x00:0x00"/>
    <classpath refid="classpath"/>
    </convert>
    Here is the answer of the computer:
    [echo] Generation of the CAP,JCA and EXP files...
    [convert] Java Card Ant Tasks, Version 1.0
    [convert] Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
    [convert] Note: Usage of Java Card Ant Tasks is optional. Java Card Ant Tasks are not supported by Sun Microsystems, Inc.
    [convert] Java Card 2.2.2 Class File Converter, Version 1.3
    [convert] Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
    [convert] parsing /home/ehoudart/app-manager/VersionZero/src/testUn/classes/testUn/PorteAppUn.class
    [convert] converting testUn.PorteAppUn
    [*convert] error: class PorteAppUn does not belong to package testUn.classes.testUn.*
    [convert] conversion completed with 1 errors and 0 warnings.
    I do not understand why this unique applet class file does not belong to its own package. Is someone an idea?
    Thanks very much in advance for your answer.

    Here is a complete example build.xml (I think I have posted this before).
    <?xml version="1.0" encoding="UTF-8" ?>
    <project default="convert" name="javacard-starter">
      <!-- Build specific properties -->
      <property name="target.classes" location="${basedir}/target/classes" />
      <property name="target.javacard" location="${basedir}/target/javacard" />
      <property name="source.java" location="${basedir}/src/main/java" />
      <!-- Must point to the folder containing the JAR files from the JCDK -->
      <property name="javacard.libs" location="${basedir}/lib" />
      <!-- Must point to the folder containing the JAR file from the JCDK ant task -->
      <property name="javacard.ant-task" location="${basedir}/lib" />
      <!-- Must point to the folder containing the API export files from the JCDK -->
      <property name="javacard.export" location="${basedir}/src/main/export" />
      <property name="verbose" value="true" />
      <property name="noverify" value="false" />
      <!-- Path for JC tasks -->
      <path id="classpath">
        <fileset dir="${javacard.ant-task}">
          <include name="*.jar" />
        </fileset>
        <fileset dir="${javacard.libs}">
          <include name="*.jar" />
        </fileset>
      </path>
      <!-- set the export path to the Java Card export files -->
      <path id="export" description="set the export file path">
        <fileset dir="${javacard.export}">
          <include name="**/*.exp" />
        </fileset>
        <pathelement path="${javacard.export}" />
        <pathelement path="${target.classes}" />
        <pathelement path="${target.javacard}" />
      </path>
      <!-- Definitions for tasks for Java Card tools -->
      <taskdef name="capgen" classname="com.sun.javacard.ant.tasks.CapgenTask" classpathref="classpath" />
      <taskdef name="deploycap" classname="com.sun.javacard.ant.tasks.DeployCapTask" classpathref="classpath" />
      <taskdef name="convert" classname="com.sun.javacard.ant.tasks.ConverterTask" classpathref="classpath" />
      <taskdef name="verifyexport" classname="com.sun.javacard.ant.tasks.VerifyExpTask" classpathref="classpath" />
      <taskdef name="verifycap" classname="com.sun.javacard.ant.tasks.VerifyCapTask" classpathref="classpath" />
      <taskdef name="verifyrevision" classname="com.sun.javacard.ant.tasks.VerifyRevTask" classpathref="classpath" />
      <typedef name="appletnameaid" classname="com.sun.javacard.ant.types.AppletNameAID" classpathref="classpath" />
      <typedef name="jcainputfile" classname="com.sun.javacard.ant.types.JCAInputFile" classpathref="classpath" />
      <target name="init">
        <mkdir dir="${target.classes}" />
        <mkdir dir="${target.javacard}" />
      </target>
      <target name="clean">
        <delete dir="${target.classes}" />
        <delete dir="${target.javacard}" />
        <delete dir="${javacard.eeprom}" />
      </target>
      <target name="compile" depends="init" description="Compile source code to class files">
        <!-- Compile the java code from ${src} to ${classes} -->
        <javac debug="yes" optimize="no" srcdir="${source.java}" destdir="${target.classes}" source="1.5" target="1.5">
          <classpath refid="classpath" />
        </javac>
      </target>
      <target name="convert" depends="compile" description="Convert class files to cap files">
              <convert packagename="chaining" packageaid="0xF0:0x00:0x00:0x01:0x00:0x01" majorminorversion="1.0" classdir="${target.classes}" outputdirectory="${target.classes}" jca="true" exp="true" cap="true" debug="true" verbose="${verbose}" noverify="${noverify}">
                   <appletnameaid aid="0xF0:0x00:0x00:0x01:0x00:0x01:0x00:0x01" appletname="chaining.ChainingApplet" />
                   <exportpath refid="export" />
                   <classpath refid="classpath" />
              </convert>
      </target>
      <target name="copy-debug">
        <mkdir dir="${target.debug}" />
        <echo>Copying cap files</echo>
        <copy flatten="true" todir="${target.debug}" verbose="true">
          <fileset dir="${target.classes}" includes="**/*.cap" />
          <fileset dir="${target.classes}" includes="**/*.exp" />
          <fileset dir="${target.classes}" includes="**/*.jca" />
        </copy>
      </target>
      <target name="all" depends="clean, convert" />
    </project>

  • Calling a object of class from other class's function with in a package

    Hello Sir,
    I have a package.package have two classes.I want to use object of one class in function of another class of this same package.
    Like that:
    one.java
    package co;
    public class one
    private String aa="Vijay";  //something like
    }main.java:
    package co;
    import java.util.Stack;
    public class main extends Stack
    public void show(one obj)
    push(obj);
    public static void main(String args[])
    main oo=new main();
    }when I compile main class, Its not compile.
    Its give error.can not resolve symbol:
    symbol: class one
    location: class co.main
    public void show(one obj)
                              ^Please help How that compile "Calling a object of class from other class's function with in a package" beacuse I want to use this funda in an application

    kumar.vijaydahiya wrote:
    .It is set in environment variable.path=C:\bea\jdk141_02\bin;.,C:\oraclexe\app\oracle\product\10.2.0\server\bin;. command is:
    c:\Core\co\javac one.javaIts compiled already.
    c:\Core\co\javac main.javaBut it give error.
    Both java classes in co package.Okay, open a command prompt and execute these two commands:
    // to compile both classes:
    javac -cp c:\Core c:\Core\co\*.java
    // to run your main-class:
    java -cp c:\Core co.main

  • How to call class file from jsp without creating packages

    i m using tomcat 5.5
    i stored my class file into WEB-INF\Class directory
    how to call that class file. from my jsp page.
    i got some error.
    i used package concept,that works fine.

    i m using tomcat 5.5
    i stored my class file into WEB-INF\Class directory
    how to call that class file. from my jsp page.
    i got some error.
    i used package concept,that works fine.Then use packages.
    As of Java1.4, you can no longer import classes from the default package (no package declared) into classes that are packaged.
    All Tomcat classes are in packages, including the compiled JSP.
    Therefore: Your classes need to be in a package inorder to be imported and used by the JSP.
    Why the problem? If you know it works with packages, why try not to use them?

  • Class.forname - Unable to load class if two jars with same package name

    class.forname - Unable to load class if two jars contains same package name exists in classpath.
    JVM is stopping search for class one it finds the package name in the jar file that is ahead in classpath,
    JVM is not continuing search in the remaining jar files.
    For this , I put the jar file that contained class ahead of of ther jar file containing same package name, then it worked.
    Is this a JVM bug ? (JDK 1.5)

    I reported as bug to sun. I got auto email, looks they have not assigned any Bug ID so far.

  • HELP share methods between packages

    Hello...
    I have 2 packages, the first (named "Library") is inttended to use as a library of miscelaneuos functions, all of them, inside one (and the only one) public Class named "MiscFunctions"
    At the second package, i got a clase named "A" that import the first package, like "import Library.MiscFunctions;", and a method named "M" that calls the method i need (named "IsMember") from the Class "MiscFunctions" at the package "Library"
    the problem is that when i compile, it doesnt recognizes the function "IsMember", it says that is not defined
    can somebody help me pleasee !!
    i will be grateful !...
    i' alooone with java...

    CALLER.JAVA
    package Example;
    import library.Funcs;
    import java.util.ArrayList;
    public class Caller {
    public static void main(java.lang.String[] args) {
         ArrayList dontcare=new ArrayList();
         if (fIsMember(dontcare, "doesnt work")) {
              /* the error is this:
    The method fIsMember invoked for type Example.Caller with arguments
    (java.util.ArrayLIst, java.lang.String) is not defined.
    FUNCS.JAVA
    package library;
    import java.util.ArrayList;
    public class Funcs {
    public boolean fIsMember(ArrayList x, String s) {
         return true;

  • How to reduce protected accessor to package accessor in child class

    I have made one abstract class ( say Parent), with 2 abstract methods (am1() & am2()) , with protected accessors. A class extends parent class (say Child). Now Child class need to keep accessors type of protected method as protected, as we cannot reduce the scope. Now as per my design, I need to create many subsclass (say SubClass) of Child class. But I don't want subclass of Child class to have access of method am1() & am2(). SubClass is declared in different package. But as I cannot reduce the accessor type from protected to package, I am not able to restrict the access of am1() & am2() to Child class and SubClass should not able to access am1() & am2().
    Do we have some way to design above scenario?
    Thanks in advance
    Praveen

    You can't reduce visibility.
    A subclass is supposed to be able to do everything the superclass can. If you'd restrict visibility of certain methods or fields, you break that contract.

  • Why we cannot reduce protected accessor to package accessor in child class?

    Hi,
    I have made one abstract class ( say Parent), with 2 abstract methods (am1() & am2()) , with protected accessors. A class extends parent class (say Child). Now Child class need to keep accessors type of protected method as protected, as we cannot reduce the scope. Now as per my design, I need to create many subsclass (say SubClass) of Child class. But I don't want subclass of Child class to have access of method am1() & am2(). SubClass is declared in different package. But as I cannot reduce the accessor type from protected to package, I am not able to restrict the access of am1() & am2() to Child class and SubClass should not able to access am1() & am2().
    Do we have some way to design above scenario?

    Double Post:
    http://forum.java.sun.com/thread.jspa?threadID=634969
    Triple post actually.
    http://forum.java.sun.com/thread.jspa?threadID=634985
    http://forum.java.sun.com/thread.jspa?threadID=634969

  • Visibility of protected

    Hi,
    I was just thinking - why members marked with protected level visibility
    allow other classes in the same package for accessing it ?
    I would have thought that protected is strictly for inheritance purposes (doesn't
    it work that way in C++ ??? )
    Cheers,
    Adrian

    Hmmm,
    I see what you mean. Perhaps I still can't get rid of C++ out of my mind :/
    I must remember that the whole language has been designed on packages basis
    and it's not just for namespace purposes .....
    Is there any explanation on the web anyway ? I'd like to read some more info
    about the design. I haven't seen any book dealing with java's design as there
    are at least few on C++. Maybe I didn't look well ...
    Thanks anyway !
    Adrian

  • Protected members not protected?

    I thought a protected member could only be accessed by the implementing class or an extension of it. Yet I have been able to set these variables inside another class. Is their something wrong with my JVM???
    1.4.1_05
    public class ComponentModel extends ContainerModel implements IComponentModel{
        private static final long serialVersionUID = 1;   
        public static final ModelType TYPE = ModelType.getType("LogicComponentModel");
        protected transient PropertyChangeListener pcl = new LCMPropertyChangeListener();
        //transients are assigned in contructors and clone and serialization methods
        //for consistency.
        protected transient Point m_offset;
        protected transient Rectangle m_sizeRect;
    public final class WADComponentModel implements IComponentModel {
        public IBaseModel getCopy(){
            ComponentModel lcm = (ComponentModel)new ModelFactory(ComponentModel.TYPE).getNewObject();
            for(Iterator it = getChildren().iterator();it.hasNext();){
                IBaseModel model = (IBaseModel)it.next();
                model.setComponent(lcm);
                model.addPropertyChangeListener(lcm.pcl); //???
            lcm.m_offset = new Point(m_offset); //???
            lcm.m_sizeRect = new Rectangle(m_sizeRect);  //???
            return lcm;
    }why am I able to set m_offset and m_sizeRect directly like this? Why am I able to access lcm.pcl directly???
    Surely I have forgotten some simply Java principle. Or my Eclipse environment is somehow hosed.

    As an above poster mentioned, if you scroll up it is described more clearly. I will post it though.
    A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
    If the member or constructor is declared public, then access is permitted. All members of interfaces are implicitly public.
    Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:
    Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
    Access is correct as described in ?6.6.2.

  • How do I make the controls on JPanel instantiated from other class visible?

    I am using Netbeans GUI editor to create a GUI. It's really terrific....but... I have aJFrame form containing a JPanel. I also have a "panel" class that extends JPanel. I would like to insantiate an object of teh "panel" class and use it as the JPanel in the JFrame. Sounds simple enough.
    When I do this, I see the JPanel from my "panel" class gets instantiated and used in the JFrame (yay), but the button and text label do not show up.
    The JFrame is in the class/file NewJFrame.java and the JPanel is in the class/file TestJPanel.java. I'm really about to pull my hair out. ;-) Any help would be greatly appreciated...I'm sure it must be something simple I have forgotten to do.
    Here is code for NewJFrame.java
    package my.gui;
    public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
    initComponents();
    @SuppressWarnings("unchecked")
    private void initComponents() {
    jPanel1 = new my.gui.TestJPanel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jPanel1.revalidate();
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(0, 400, Short.MAX_VALUE)
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(0, 300, Short.MAX_VALUE)
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    pack();
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new NewJFrame().setVisible(true);
    private javax.swing.JPanel jPanel1;
    And the code for TestJPanel.java
    package my.gui;
    import java.awt.Color;
    public class TestJPanel extends javax.swing.JPanel {
    public TestJPanel() {
    initComponents();
    this.setBackground(Color.white);
    @SuppressWarnings("unchecked")
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jLabel1.setText("jLabel1");
    jButton1.setText("jButton1");
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jLabel1)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
    .add(jButton1)
    .addContainerGap(273, Short.MAX_VALUE))
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(103, 103, 103)
    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    .add(jLabel1)
    .add(jButton1))
    .addContainerGap(174, Short.MAX_VALUE))
    public javax.swing.JButton jButton1;
    public javax.swing.JLabel jLabel1;
    }

    If you really want to learn Swing, I advise you to put the NetBeans away, and dive into the Sun Swing tutorials where you'll learn to code your Swing by hand. One of the main things that you'll learn -- and likely the thing that is tripping your current code up -- is how to use layout managers in a way that will allow you to flexibly place JPanels and other components where you want them, where they'll resize if need be, and where they'll be visible when you want them to be.
    Much luck.

Maybe you are looking for

  • Is it still possible to get the LATEST VERSION of ppro cs4?

    my ppro version is 4.0.0, some months ago i see here that ppro lates version is 4.4.2 I want to download that but today i can not find it anywhere... anyone know where to download it at adobe.com site?  thanks

  • Ivory plugin missing in AU Manager in Logic X 10.0.2

    I don't see Ivory in Logic X Works fione in Logic 9.1.8 Any thoughts? Thanks! TD

  • Transport of package links in BPC NW 10.0

    Hello Experts, I have observed something strange that not all package links seem to be transporting to target system. Though i have collected 6 package links through RSA1 granular transport methodology ... i see only 2 have been  imported into Q. I c

  • Update mail entries in Directory server 5.2

    Hi All, we have a request of updating mail addresses.. that to only after @. our server have around 650000 entries. One solution we are thinking of is exporting the database... changing the entries and importing again Can any one help me with any sim

  • Should I or shouldn't I - Upgrade to newer versions of iTunes?

    I've been unable to upgrade to some of the newer updates to iTunes (& iPod) due to error messages. I believe the last version I have is 6.0 (late last year's). The question I have is..If I can't upgrade to the newer versions am I missing something vi