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

Copy Constructor

This Java code defines a Fruit class with private fields for price and name. It includes a constructor to set these fields, a copy constructor, and get methods to return the price and name. The main method creates two Fruit objects - one with a constructor and one with the copy constructor, and prints the name and price of each.

Uploaded by

Friel Mado
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Copy Constructor

This Java code defines a Fruit class with private fields for price and name. It includes a constructor to set these fields, a copy constructor, and get methods to return the price and name. The main method creates two Fruit objects - one with a constructor and one with the copy constructor, and prints the name and price of each.

Uploaded by

Friel Mado
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Fruit {

private double fprice;


private String fname;
//constructor to initialize roll number and name of the student
Fruit(double fPrice, String fName)
{
fprice = fPrice;
fname = fName;
}
//creating a copy constructor
Fruit(Fruit fruit)
{
System.out.println("\nAfter invoking the Copy Constructor:\n");
fprice = fruit.fprice;
fname = fruit.fname;
}
//creating a method that returns the price of the fruit
double showPrice()
{
return fprice;
}
//creating a method that returns the name of the fruit
String showName()
{
return fname;
}
//class to create student object and print roll number and name of the student
public static void main(String args[])
{
Fruit f1 = new Fruit(399, "Ruby Roman Grapes");
System.out.println("Name of the first fruit: "+ f1.showName());
System.out.println("Price of the first fruit: "+ f1.showPrice());
//passing the parameters to the copy constructor
Fruit f2 = new Fruit(f1);
System.out.println("Name of the second fruit: "+ f2.showName());
System.out.println("Price of the second fruit: "+ f2.showPrice());
}
}

You might also like