public class Vehicle { private String regNumber; private String make; private int yearMade; private double value; public Vehicle(String regNumberIn, String makeIn, int yearMadeIn, double valueIn) { regNumber = regNumberIn; make = makeIn; yearMade = yearMadeIn; value = valueIn; } public void setValue(double valueIn) { value = valueIn; } public String getRegNumber() { return regNumber; } public String getMake() { return make; } public int getYear() { return yearMade; } public double getValue() { return value; } public int calculateAge(int currentYearIn) { return currentYearIn - yearMade; } }
public class SecondHandVehicle { private int numberOFOwners; public SecondHandVehicle (String Registration, String Make, int Manufacture, double value, int numberOfOwners) { } }
public class TestSHV { public static void main(String[] args) { SecondHandVehicle myCar = new SecondHandVehicle(); } }
SecondHandVehicle myCar = new SecondHandVehicle();
1.
A new object is created in memory
2.
The constructor for the object's class is called
public SecondHandVehicle (String Registration, String Make, int Manufacture, double value, int numberOfOwners)
{
}
•
Registration: jhf72935eu
•
Make: Peugeot 208
•
Year of manufacture: 2001
•
Value: 10
•
Number of owners: 5
SecondHandVehicle myCar = new SecondHandVehicle("jhf72935eu", "Peugeot 208", 2001, 10.0, 5);
myCar.SecondHandVehicle();
System.out.println("Registration: " + myCar.*all the variables I defined in the constructor*;
// No relationship here..!
public class SecondHandVehicle
// OK! SecondHandVehicle now has an IS-A relationship to Vehicle using inheritance.
public class SecondHandVehicle extends Vehicle
// Nope! This won't work now...
public SecondHandVehicle(String Registration, String Make, int Manufacture, double value, int numberOfOwners) {
}
// OK! The constructor is using the 'super' keyword to defer to the Vehicle constructor
public SecondHandVehicle(String Registration, String Make, int Manufacture, double value, int numberOfOwners) {
super(Registration, Make, Manufacture, value);
}
public String getRegNumber() {
return regNumber;
}
public String getMake() {
return make;
}
public int getYear() {
return yearMade;
}
public double getValue() {
return value;
}
String myCarMake = myCar.getMake();
double myCarValue = myCar.getValue();
System.out.printf("myCar is a %s with registration %s, manufactured in %d, worth £%.2f ",
myCar.getMake(), myCar.getRegNumber(), myCar.getYear(), myCar.getValue());
Last reply 1 minute ago
LSE Economic history departmentPosted 1 minute ago
student finance and nhs fund for 2nd healthcare degree?Last reply 1 minute ago
Official London School of Economics and Political Science 2024 Applicant ThreadLast reply 2 minutes ago
When can I start to apply for vacation schemes and trainee contracts for law?Last reply 2 minutes ago
British Council English Language Assistants 2024/2025Last reply 2 minutes ago
Official UCL Offer Holders Thread for 2024 entryLast reply 3 minutes ago
Official University of Edinburgh Applicant Thread for 2024Last reply 6 minutes ago
lloyds bank 2024 apprenticeshipLast reply 6 minutes ago
Which Falmouth Uni Accomodation do you recommend for a Penryn student)Last reply 8 minutes ago
Official: Queen's University Belfast A100 2024 Entry ApplicantsLast reply 11 minutes ago
Amazon Project management apprenticeship 2024Last reply 12 minutes ago
Official Cambridge Postgraduate Applicants 2024 ThreadLast reply 12 minutes ago
LSE anthropology and law 2024Last reply 13 minutes ago
Sidney Sussex College Cambridge 2024 offer-holdersLast reply 16 minutes ago
Official University College London Applicant Thread for 2024Last reply 16 minutes ago
AQA A Level History Paper 2 (7042/2A-2T) - 7th June 2024 [Exam Chat]Last reply 3 hours ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 1 day ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 3 hours ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 1 day ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 1 week ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 4 weeks ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]