0% found this document useful (0 votes)
33 views

Copy Constructor: Dr. Zara Hamid

This document discusses constructors in Java. It defines three constructors for a Car class: 1) A constructor that accepts all state attributes as parameters. 2) A constructor that uses some parameters and default values for remaining attributes. 3) A default constructor that sets default values for all attributes. It then demonstrates creating Car objects using each constructor and a copy constructor.

Uploaded by

balti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Copy Constructor: Dr. Zara Hamid

This document discusses constructors in Java. It defines three constructors for a Car class: 1) A constructor that accepts all state attributes as parameters. 2) A constructor that uses some parameters and default values for remaining attributes. 3) A default constructor that sets default values for all attributes. It then demonstrates creating Car objects using each constructor and a copy constructor.

Uploaded by

balti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COPY CONSTRUCTOR

Dr. Zara Hamid


public class Car {
private int numLiters;
private int horsepower;
private int numDoors;
private int year;
private String color;
private String model;
private String make;
private String vin;
// a constructor that accepts all state attributes
public Car(String vin, String color, String make, String model,
int numLiters, int horsepower, int numDoors, int year) {
this.vin = vin;
this.model = model;
this.make = make;
this.color = color;
this.numLiters = numLiters;
this.horsepower = horsepower;
this.numDoors = numDoors;
setYear(year); }
// a constructor that uses parameters and default state
values
public Car(String vin, int year, String make, String model)
{ this.vin = vin;
this.make = make;
this.model = model;
setYear(year); numLiters = 2;
horsepower = 200; color = "blue"; numDoors = 2; }
// a default constructor
public Car() { vin = "1234567"; make = "Ford";
model = "Focus"; year = 2011; numLiters = 2;
horsepower = 200; color = "blue"; numDoors = 2;
}
public static void main(String args[ ]) {
Car defaultCar = new Car();
System.out.println("My Car: " + defaultCar);

Car chevy = new Car("9431a", 2000, "Chevy", "Cavalier");


System.out.println("Chevy Car: " + chevy);

Car dodge = new Car("8888", "orange", "Dodge", "Viper", 5,


400, 2, 1996);
System.out.println("Dodge Car: " + dodge);

Car copy= new Car (chevy)


}

You might also like