Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super classes and sub classes

 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric McIntyre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post a simple code example? I'm not really clear on what you'd like to accomplish.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have set your fields as private, eg
Then you have public get and set methods for a b c?
Then you have more variables in your subclass?
Then you have default constructors?
. . . and parameter constructors?

[You might be able to dispense with the no-parameter constructor in the subclass, as long as you don't plan to extend it to SubSubclass .]
Then you want to set a and c and leave b unchanged. How about this?

I have no idea whether it works, but try it out and tell us what happens.
CR
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, exactly. I'll try it out.
 
kenji mapes
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Almost exactly as you portrayed it Cambell How you could infer what I wanted to do from my shallow explanantion is bizzaro.
[ November 02, 2005: Message edited by: kenji mapes ]
 
Eric McIntyre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you just want to make "blue" either a constant, or pull it into a method. Either

or

The advantage of using the constant is that it's very simple to implement; the advantage of the protected method is that it's more extensible (in case a subclass wants a different default color).

As a side note on design, you've got a lot of duplicate code in there. The subclass constructor might be better off delegating to the super, then validating the color after the fact. Also, if your setters need the same data validation, move that logic out of the constructor and delegate to the setters.

Eric
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We try our hardest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic