Accessing super class  private variables from derived class

posted November 01, 2005 08:20 PM Profile for kenji mapes Email kenji mapes Send New Private Message Edit/Delete Post Reply With Quote Assume I have a default and a param constructor in both a subclass and a super class. The members are private.
So after validation logic in the sub class param. constructor, I want to access an instance variable of the super class's default constructor to set the subclass's matching variable to the default in the super class.
Is there anyway I can do this. Of course, I have inherited setters and getters.
Thanks.

posted November 01, 2005 08:20 PM Profile for
kenji mapes Email kenji mapes Send New Private
Message Edit/Delete Post Reply With QuoteI suppose this is the result of an attempted crossposting from another forum. :)

Similar Messages

  • Query: Accessing super class

    hi friends ,
    i have a small doubt on accessing super class using super keyword.
    does the use of super as super(super(super(i))); work properly
    and what would be the result.
    kindly guide me..........
    jay

    What do you expect that to do? Is that supposed to be a call to a super constructor? (and then super-super, and then super-super-super).
    In any case, you can only call things from the immediate superclass (the parent). If you think you need to call the superclass' superclass (the grandparent), you likely have a bad design.
    What are you trying to do?

  • Accessing a private variable from a public method of the same class

    can anyone please tell me how to access a private variable, declared in a private method from a public method of the same class?
    here is the code, i'm trying to get the variable int[][][] grids.
    public static int[][] generateS(boolean[][] constraints)
      private static int[][][] sudokuGrids()
        int[][][] grids; // array of arrays!
        grids = new int[][][]
        {

    Are you sure that you want to have everything static here? You're possibly throwing away all the object-oriented goodness that java has to offer.
    Anyway, it seems to me that you can't get to that variable because it is buried within a method -- think scoping rules. I think that if you want to get at that variable your program design may be under the weather and may benefit from a significant refactoring in an OOP-manner.
    If you need more specific help, then ask away, but give us more information please about what you are trying to accomplish here and how you want to do this. Good luck.
    Pete
    Edited by: petes1234 on Nov 16, 2007 7:51 PM

  • Access super class private variables

    class A
    private int i,j;
    A()
    i=j=20;
    public void show()
    System.out.println(i+" "+j);
    class B extends A
    int k;
    B()
    super();
    k=20;
    public static void main(String args[])
    B b=new B();
    b.show();
    In the above program, when a instance of class A is created, the memory is allocated for 2 private integer variables(i and j in this case) 8 byte of memory gets allocated and the referance is returned to the object.
    ex:
    A a=new A();
    a contains the address of memory allocated for 2 integer variables.
    In this code there is another class called B which contains only one variable of it's own.when a object is creted for class B only 4 byte of memory is allocated and the reference is returned.
    ex:
    B b=new B();
    b contains address of 4 byte of memory allocated.
    my doubt is when the B's constructor is called,it inturn calls super(),
    where the variables are intialized. variable (i,j) does not exist in memory and how are they getting initialized.are they created at runtime and getting initialized.Though B has no explicit control over these variables it can able to access their values.How is that possible??

    Can you please stop creating multiple threads with the same question. There is already a discussion in your other thread. Please don't waste our time.
    http://forum.java.sun.com/thread.jspa?messageID=4063146#4063146
    Kaj

  • Accessing Super Class of Super Class

    Consider the following classes
    Class A {
    print() {
    System.out.println("Class A Called");
    Class B extends A {
    print() {
    System.out.println("Class B Called");
    Class C extends B {
    print() {
    System.out.println("Class C Called");
    How can I call the print method of class A while working in class C ? Please state all possible solutions.
    Can it somehow be done using super ?

    RZP wrote:
    Consider the following classes
    Class A {
    print() {
    System.out.println("Class A Called");
    Class B extends A {
    print() {
    System.out.println("Class B Called");
    Class C extends B {
    print() {
    System.out.println("Class C Called");
    How can I call the print method of class A while working in class C ? Please state all possible solutions.
    Can it somehow be done using super ?Here is one possible solution.
    I really don't understand the ramifications of reflection... and am not familiar with OOP design... but reflection seems to enable a programmer to avoid OOP design principals as well as type-checking. While I like reflection, most of my readings suggest to rarely use it.
    public class Main {
      public static void main(String[] args) {
        try {
          new C(789).print();
        } catch(Exception e) { e.printStackTrace();  }
          static class A {
            protected A a;
            protected int id;
            public A(A a, Integer id) {
              this.a = a;
              this.id = id;
            public A(int id) { this.id = id; }
            public void print() {
              System.out.println("Class A called... from a " + a.getClass().getCanonicalName() + " using the original instance with id = " + a.id);
        static class B extends A {
          public B(A a, Integer id) { super(a, id);  }
          public B(int id) { super(id); }
          public void print() { System.out.println("Class B called...");   }
        static class C extends B {
            public C(A a, Integer id) { super(a, id); }
            public C(int id) { super(id); }
            public void print() {
                try {
                    System.out.println("Class C called...");
                    Object obj = getClass().getSuperclass().getSuperclass().getConstructor(A.class, Integer.class).newInstance(this, -111);
                    obj.getClass().getMethod("print", null).invoke(obj, null);
                } catch (Exception e) {
                    e.printStackTrace();
    }Anyway, I think that enables one to invoke overridden methods in super classes, regardless of how "high-up" they are in the class heirarchy (without having to use a new object to do it). Use that idea in an interview at your own risk but just not in interviews for the same position I am trying to get...
    Edited by: dpxqb on Apr 17, 2010 4:43 AM

  • Access a instance level variable from activity in a subflow

    I have a flow such that it calls a number of subflows chained together.
    I have defined a instance variable to store a flag when a perticuler activity has been executed.
    in one of the subflows, there is an activity which i need it to update the parent level instance variable when it has done its task.
    but, i'm not sure how to get access to the parent level instance variables from an activity with in a subflow.
    I have PAPI code polling on the instance wating for this variable to change.
    any ideas?

    in a clip on the main timeline:
    var mc = this.parent;
    in a clip on the main timeline that was added with a loader:
    var mc = this.parent.parent;
    the first parent would be the loader
    the second parent is the loader's parent, or, the main timeline

  • Accessing Captivate 5.5 variables from embedded SWFs

    Hi,
    as we all know the interface for accessing Captivate variables from embedded SWFs tends to change between Captivate versions. Shortly after it's release we upgraded to version 5.5, and I'm not succeeding in accessing the Captivate variables in my Flash/AS3 code the same way it was done in version 5.
    How should I access the Captivate 5.5 variables (rdinfoCurrentSlide et al) from my SWFs?

    Hi,
    You can use system variable : cpInfoCurrentSlide to show the slide number.
    So you can use text captions to display the value of the variable, the format for displaying the value stored in the variable is $$<system variable>$$. So for slide number use : $$cpInfoCurrentSlide$$, type this in the text caption and then check it in the preview.
    Thanks

  • Is there a way to reference a private variable from one class in another?

    My first class starts off by declaring variables like so:
    class tStudent {
      // declare student name, id, grades 1 & 2, and gpa
      private String fname, lname, g1, g2;
      private int id;
      private double gpa;
      // define a constructor for a new student
      tStudent () {fname=lname=g1=g2=null; id=-1; gpa=0.0;}
      // define methods for manipulating the data members.
      // readStudent: reads information for just one student
    public void read (Scanner input) {
          fname = input.next();
          lname = input.next();
          id = input.nextInt();
          g1 = input.next();
          g2 = input.next();
    }And the second class:// tStudentList: for a list of students
    class tStudentList {
      private int nStudents;
      private tStudent[] list;
      // constructor for creating student list
      tStudentList() {
          list = new tStudent[36];
          for (int i=0; i < 36; i++) list=new tStudent();
    // read the individual students into the student list
    public void read(Scanner scan) {
    nStudents=0;
    while (scan.hasNext()) {list[nStudents++].read(scan);}
    // display the list of all students - fname, lname, id, g1, g2 and gpa
    // with an appropriate header so the output matches my sample //output
    public void print() {
    Is there a way to reference the variables in the first class to use in the second? Specifically in the last section of code where I am going to print the list.

    Not without resorting to reflection hackery. If the fields are private (and are supposed to be), then that means "don't allow access to these to outsiders of this class" by design.
    So if you really meant them to be accessible, then don't use private, or provide public accessors for them.

  • Composition in java : how to access super-class from sub-class

    I have 3 classes related by composition
    class A{
    B b = new B();
    class B{
    List<C> cList = new ArrayList<C>();
    in class C{
    getSample(){
         //Try to access class A     
    how can I access A from class C ?
    Any help is appreciated!

    well, 'Id' is known only to class A, and class C has a getSample() which is called in class A.
    class A{
    B b = new B();
    getID(){
    return id;
    b.getC().getSample();
    class B{
    List<C> cList = new ArrayList<C>();
    class C{
    getSample(){
    if() {
    //pick up value from properties file
    // format the value and parameter substitute it with 'Id' from A
    The design does seem a little weird, but I guess this is the only way to do this considering the other constraints we have ....
    TIA

  • How to access super class's super class

    Hello every one.
    I have a question.
    if I have a class like:
    public class A {
    void methoda() {
    void methodb() {
    and a class B extends A and override A's methoda
    public class B extends A {
    void methoda() {
    and a class C extends B which want use A's methoda.
    public class C extends B {
    // and here I want to use class A 's methoda,how can I do?
    thanks a lot

    Try the following. When run it eill print out:
    [In class C] A:B:C
    Now calling A's method: [In class A] A and i is 500Which is what you want right?
    public class TestSuperSuper {
        public static void main (String[] args) throws Exception {
            C c = new C();
            c.setName("A", "B", "C");
            c.setI(500);
            c.methodA();
        static class A {
            String aName;
            int i;
            public void methodA(){
                System.out.println("[In class A] " + aName + " and i is " + i);
            public void setName(String aName){
                this.aName = aName;
            public void setI(int i){
                this.i = i;
        static class B extends A {
            String bName;
            public void methodA(){
                System.out.println("[In class B] " + aName + ":" + bName);
            public void setName(String aName, String bName){
                this.aName = aName;
                this.bName = bName;
        static class C extends B {
            String cName;
            A wrappedA;
            public C(){
                wrappedA = new A();
            public void methodA(){
                reflectOnA();
                System.out.println("[In class C] " + aName + ":" + bName + ":" + cName);
                System.out.print("Now calling A's method: ");
                wrappedA.methodA();
            public void setName(String aName, String bName, String cName){
                this.aName = aName;
                this.bName = bName;
                this.cName = cName;
            private void reflectOnA(){
                try {
                Field[] aFields = wrappedA.getClass().getDeclaredFields();
                for (int i = 0; i < aFields.length; i++){
                    Class fldType = aFields.getType();
    if (fldType.isPrimitive()){
    if (fldType.getName().equals("int")){
    aFields[i].setInt(wrappedA, aFields[i].getInt(this));
    else {
    // now do the other primitive type tests here
    else {
    aFields[i].set(wrappedA, aFields[i].get(this));
    catch (Exception e){
    System.err.println(e);
    The main thing here is that in class C you have an instance variable called wrappedA which is an instance of A and the method called reflectOnA(). Basically, what this method does is it will set all the fields in wrappedA to the same values that this object has so that it would look like that wrappedA is somewhat the same as this object. You will call this method whenever you want to call your superclass' superclass' method.
    Hope this helps.

  • Accessing Super Class methods

    Hi,
    If I have a Subclass that overrides a method in a superclass, is it possible to call that method from the superclass?
    Thanks

    Hi thanks for the reply, but that works for static methods, I'm trying to figure out non-static methods. Below, I want to call the method a() from the superclass using an instance of the subclass.
    class superclassA
    void a()
    System.out.println("in method a");
    class subclassA extends superclassA
    void a()
    System.out.println("in method b");
    public class overtest
    public static void main(String [] args)
    subclassA sub = new subclassA();
    superclassA.a();//non-static method a cannot be referenced from a static context
    Thanks!

  • Access private variables from javascript

    Hi.
    How can i access my objects and varibles, in my java code from my javascript.

    Try displaying the value before you assign it.
    If you wanna display the value of a text field called textCallClass, this is the javascript code:
    alert(document.getElementById("form1:textCallClass").value);
    And if you wanna display a field called uniqueNum from a page called
    SchemeHolders, this is the javacsript code:
    alert(#{SchemeHolders.uniqueNum});
    You have ro have a public function called getUniqueNum in SchemeHolders.java. And ensure the case is exactly like mine is.
    That does work fine in my programs, if you have any other problems, post the error message.

  • How to access an object's variable from native code

    i am passing an object in a native method.i have to change the value of the object's variable.how do i access the variable of object and assign it new value from c++ code.when i try to access it i get a message that the variable must have a class/sruct/union type.
    pl. help.

    I'll tell You if You send me the structure of this object in Java. And a name of field what are You thinking about.
    Maciek

  • How to access TestStand Event and variables from external application?

    Hi all
    The test system was built by TestStand, now there is a new requirement to filter the test report without change the sequence file. The task must be done without any impact to previous software system, so I decide to write a tool by VC++.
    My idea is to deal with the reports after every UUT loop, but I need to monitor the UUT loop status by VC++, if an unit is tested, pass or fail, raise a event and pass to VC++ application. In a word, I would like to access TestStand internal event and variables.
    Anyone has ideal on this case? ActiveX or something.
    thanks .
    Rexxar
    *The best Chinese farmer*

    paulbin,
    While sharing variables via COM or DCOM is certaintly possible, I don't think you need to go down that route.  I think that there is probably an easier way to limit your report.  In your Configuration menu, under the Report Options item, there is a field at the top that will allow you to filter what steps go into your report.
    This will not affect any sequence file you may have already created, all it does is change a few options in the ReportOptions.ini file in the <TestStand>\cfg directory.
    This is a much simpler option than trying to write a program to interface with a running TestStand engine.
    Josh W.
    Certified TestStand Architect
    Formerly blue

  • How to get variable from another class?

    I have 2 classes. In first I have int variable. In second class I need to get this variable value. How I can make it?
    import javax.microedition.lcdui.*;
    import java.io.*;
    import java.util.*;
    public class ChooseLessons extends Form implements CommandListener, ItemStateListener
         ChoiceGroup lessons;     // Choice Group of preferences
         Dictionary     dictionary;
         int volumeSize;
         ChooseLessons(Dictionary dictionary)
              int volumeSize = 15;
         public void commandAction(Command c, Displayable s)
              if (c == Dictionary.BEGIN_CMD) {
                   new TeachForm(dictionary, this);
    import javax.microedition.lcdui.*;
    import java.util.*;
    public class TeachForm extends Form implements CommandListener     
         Dictionary               dictionary;
         ChooseLessons          lessons;
         TeachForm(Dictionary dictionary, ChooseLessons lessons) {
              super(Locale.WORD);
              this.dictionary = dictionary;
              this.lessons = lessons;
              lessons.volumeSize(); // HERE I NEED VARIABLE VALUE FROM PREVIOUS CLASS
    }Edited by: Djanym on Mar 16, 2009 4:43 PM

    This is a classic problem that coders run into when trying to get their head around object-oriented programing. Since you have a class that should be modeled after a real world object, as far as that object is concerned, no one else needs to know the details of it - without asking nicely. This is where you should set up some getters and setters, which are methods that allow fields in a class to reveal themselves or allow their states to be changed in a orderly fashion.
    There are a number of fields that never need to be known outside of the class. Then there are some fields you would like to let people know about, but don't want them to have the ability to change them. In the example below, there are to getter methods allow return of the necessary fields. If you made these public, there is a possibility that someone utilizing this field may change it outside of its intended use, or access them without them being ready for public consumption.
    Class test {
    //These private variables are only visible from the class
    private int grade1 = 0;
    private int grade2 = 0;
    private int grade3 = 0;
    private float average = 0;
    private int gradeboost = 0;
    //This method sets the gradeboost field to one desired by the instructor
    void setboost(int boost) {
    gradeboost = boost;
    //These methods accept test scores and compute the average for three test
    //Notice that the calculated average may not be the true average of the three test scores
    //because of the possibility of gradeboost calculation being greater than 1
    void test1(int score) {
             grade1 = score;
             average = (grade1 + grade2 + grade3 + gradeboost)/3;
    void test2(int score) {
             grade2 = score;
             average = (grade1 + grade2 + grade3 + gradeboost)/3;
    void test3(int score) {
             grade3 = score;
             average = (grade1 + grade2 + grade3 + gradeboost)/3;
    //This is a getter method, which provides read access to the private variable average
    //If someone just had public access to the grades and wanted to take their own average
    //They would miss how the gradeboost field affects the final outcome.
    float getAverage() {
        return average;
    //Here is a getter method, which accepts an argument to determine which test score to return
    //Notice that this isn't the true testscore, but it has been modified by the gradeboost field.
    //If the user had public access to the true testscore, it wouldn't take into account the gradeboost calculation!!
    //This is how a getter can control exactly what a user has access to.
    float get testScore(int test) {
    float testresult = 0;
    if (test = 1) {
           testresult = (grade1+ gradeboost) / 3;
    if (test = 2) {
           testresult = (grade2+ gradeboost) / 3;
    if (test = 3) {
           testresult = (grade3+ gradeboost) / 3;
    return testresult;
    }

Maybe you are looking for

  • Hard drive/Finder trouble

    I'm having trouble with my hard drives, it's happened several times during the last couple of days that the Finder becomes unresponsive, as if there's something it's unable to read. I can't get it to relaunch, and as the menu bar also disappears I ha

  • Computer reboots during install solaris 8 ,i solve it

    i install solaris 8 on DELL DIMENSION and machine reboots when screen show like :(using b kadb -v) pci-device: ide@1,ata1 ata1 is /pci@0,0,pci-ide@f/ide@1 sd0 at ata1: target 0 lun 0 sd0 is /pci@0,0/pci-ide@f/ide@1/sd@0,0 cannot mount root on /pci@0,

  • HT201413 error 21 linked to battery failure

    Hi I have been having a problem with My Iphone 4. It started 2 days ago when it had 10% battery left, I was timing a sports game and when i went to check the time 5 minutes later and the phone had shut down. I thought it was down to the fact it had n

  • Condition type for pricing came from PC but has not included the additive cost record

    hello all; we have the pricing base for the sales order with the estimated cost from pc; our costing process has a posting record for additive cost is not included on the total cost is sent to the condition type EK02. question: It is possible to incl

  • Can't login to server, from Server.app

    I have a very odd problem. After an clean install of Lion Server 10.7.2 i can't access my server from server.app or Server admin? I have tryied this solution but no luck http://support.apple.com/kb/TS3960?locale=en_US The servermgrd IS loaded? Anybod