Accessing super class private variables from derived class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Accessing super class private variables from derived class

  1. #1
    Join Date
    Nov 2005
    Posts
    2

    Accessing super class private variables from derived class

    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.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I want to access an instance variable of the super class's default constructor
    This is a bit foggy, as an instance variable doesn't belong to a
    constructor
    . Could you manage by duplicating the logic that generates
    the superclass default value(s) in the subclass's param constructor ?

    If this is wrong then please explain in more detail what you want to achieve.
    eschew obfuscation

  3. #3
    Join Date
    Nov 2005
    Posts
    2
    Here's part of the super class

    public class Car implements Cloneable
    {
    private int year;
    private int numDoors;
    private double carPrice;
    private String model;
    private String color;

    public Car()
    {
    year = 2002;
    numDoors = 4;
    color = "Blue";
    carPrice = 12500;
    model = "Nissan Altima";

    }

    public Car(int yr, int numD, String col, double carP, String mod)
    {
    // if_else to control year - sets to default year if invalid

    if((yr >= 1970) && (yr <= 2005))
    year = yr;

    else
    year = 2002;

    // if_else to control # car doors - sets to default car doors if invalid

    if((numD > 0) && (numD <= 5))
    numDoors = numD;

    else
    numDoors = 4;

    //no conditions for color

    color = col;

    // if_else to control car price - sets to default car prices if invalid

    if((carP <= 50000) && (carP > 0))
    carPrice = carP;

    else
    carPrice = 12500;

    //no conditions for model

    model = mod;

    }

    Now here's the sub

    public class FamilyCar extends Car
    {

    private int mileage;

    public FamilyCar()
    {
    super();
    mileage = 15000;

    }

    public FamilyCar(int yr, int numD, String col, double carP, String mod, int mil)
    {
    // if_else to control year - sets to default year if invalid

    if((yr >= 1970) && (yr <= 2005))
    year = yr;

    else
    year = 2005;

    // if_else to control # car doors - sets to default car doors if invalid

    if(numD == 4)
    numDoors = numD;

    else
    numDoors = 4;

    //color must be Blue, black, Maroon, or Grey

    if(col.equalsIgnoreCase("blue"))
    color = col;
    else if(col.equalsIgnoreCase("black"))
    color = col;
    else if(col.equalsIgnoreCase("maroon"))
    color = col;
    else if(col.equalsIgnoreCase("grey"))
    color = col;
    else
    color = "blue";

    // if_else to control car price - sets to default car prices if invalid

    if((carP <= 20000) && (carP > 0))
    carPrice = carP;

    else
    carPrice = 12500;

    //no conditions for model

    model = mod;

    //validation logic for mileage

    if((mil >= 1) && (mil <=200000 ))
    mileage = mil;
    else
    mileage = 15000;

    }

    It has an extra data field. If you look in the param constructor in the sub class, instead of explicitly assigning blue as a defualt in the if else validation logic, I'd like to be able to conditionally assign it to the super class's default, which of course is blue. This is why I'd like to access the super's data field

    I have to trim down some of the suplicate code and also try to leave all validation logic in the setters.

    Also, the sub class has one extra datafield, mileage.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You could declare a public (final ? ) static variable:

    public static String dlftColor="blue";

    in the superclass and access that in the sublass's default else clause
    (or anywhere else for that matter) like:
    else
    col=Car.dlftColor;
    eschew obfuscation

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  2. wait() notify() problem
    By Satish in forum Java
    Replies: 1
    Last Post: 05-20-2002, 11:02 AM
  3. Producer Consumer Thread
    By Chirag in forum Java
    Replies: 6
    Last Post: 04-10-2002, 06:22 AM
  4. Replies: 0
    Last Post: 04-26-2001, 11:01 PM
  5. Toolbar Button Style.
    By Norm in forum VB Classic
    Replies: 4
    Last Post: 01-21-2001, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center