-
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.
-
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
-
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.
-
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
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
Replies: 1
Last Post: 05-20-2002, 10:02 AM
-
Replies: 6
Last Post: 04-10-2002, 05:22 AM
-
By Patrick Ireland in forum .NET
Replies: 0
Last Post: 04-26-2001, 10:01 PM
-
By Norm in forum VB Classic
Replies: 4
Last Post: 01-21-2001, 09: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks