Java Inheritance - Getting a Parameter from Parent Class
Posted
by
Aaron
on Stack Overflow
See other posts from Stack Overflow
or by Aaron
Published on 2012-11-08T04:54:49Z
Indexed on
2012/11/08
5:01 UTC
Read the original article
Hit count: 249
java
I'm trying to take one parameter from the parent class of Car and add it to my array (carsParked
), how can i do this?
Parent Class
public class Car
{
protected String regNo; //Car registration number
protected String owner; //Name of the owner
protected String carColor;
/** Creates a Car object
* @param rNo - registration number
* @param own - name of the owner
**/
public Car (String rNo, String own, String carColour)
{
regNo = rNo;
owner = own;
carColor = carColour;
}
/** @return The car registration number
**/
public String getRegNo()
{
return regNo;
}
/** @return A String representation of the car details
**/
public String getAsString()
{
return "Car: " + regNo + "\nColor: " + carColor;
}
public String getColor()
{
return carColor;
}
}
Child Class
public class Carpark extends Car
{
private String location; // Location of the Car Park
private int capacity; // Capacity of the Car Park - how many cars it can hold
private int carsIn; // Number of cars currently in the Car Park
private String[] carsParked;
/** Constructor for Carparks
* @param loc - the Location of the Carpark
* @param cap - the Capacity of the Carpark
*/
public Carpark (String locations, int room)
{
location = locations;
capacity = room;
}
/** Records entry of a car into the car park */
public void driveIn()
{
carsIn = carsIn + 1;
}
/** Records the departure of a car from the car park */
public void driveOut()
{
carsIn = carsIn - 1;
}
/** Returns a String representation of information about the carpark */
public String getAsString()
{
return location + "\nCapacity: " + capacity +
" Currently parked: " + carsIn +
"\n*************************\n";
}
}
© Stack Overflow or respective owner