Dynamic Polymorphism
-
In the noted line i have problem.where i am doing error so that i am not getting old address and new address? Rest of the things are showing expected result. -----------------------------------
public void displayCustomerDetails(Address address) {
System.out.println("Displaying customer details \n***************************");
System.out.println("Customer Id : " + customerId); //can also use this.customerid
System.out.println("Customer Name : " + customerName); //can also use this.customerName
System.out.println("Contact Number :"+ contactNumber);
System.out.println("Customer Address : " +this.getAddress()); //Here I want to print old address:confused:System.out.println();
}public void updateDetails(Address address) { //Association System.out.println("Updating customer address..."); this.setAddress(address); System.out.println("New Customer Address :"+ " "+this.getAddress() ); //Here i want to print new address:confused: }
My Code -------------- Customer Class ----------------------
public class Customer {
private String customerId;
private String customerName;
private long contactNumber;
private Address address; //Aggregation
public Customer(String customerId, String customerName, long contactNumber, Address address) { // Constructors and other methods
this.customerId = customerId;
this.customerName = customerName;
this.contactNumber = contactNumber;
this.address = address;
}
public String getCustomerId() {
return customerId;
}
public String getCustonerId() {
return customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public long getContactNumber() {
return contactNumber;
}
public void setContactNumber(long contactNumber) {
this.contactNumber = contactNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}public void displayCustomerDetails(Address address) { System.out.println("Displaying customer details \\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.out.println("Customer Id : " + customerId); //can also use this.customerid System.out.println("Customer Name : " + customerName); //can also use this.customerName System.out.println("Contact Number :"+ contactNumber); System.out.println("Customer Address
-
In the noted line i have problem.where i am doing error so that i am not getting old address and new address? Rest of the things are showing expected result. -----------------------------------
public void displayCustomerDetails(Address address) {
System.out.println("Displaying customer details \n***************************");
System.out.println("Customer Id : " + customerId); //can also use this.customerid
System.out.println("Customer Name : " + customerName); //can also use this.customerName
System.out.println("Contact Number :"+ contactNumber);
System.out.println("Customer Address : " +this.getAddress()); //Here I want to print old address:confused:System.out.println();
}public void updateDetails(Address address) { //Association System.out.println("Updating customer address..."); this.setAddress(address); System.out.println("New Customer Address :"+ " "+this.getAddress() ); //Here i want to print new address:confused: }
My Code -------------- Customer Class ----------------------
public class Customer {
private String customerId;
private String customerName;
private long contactNumber;
private Address address; //Aggregation
public Customer(String customerId, String customerName, long contactNumber, Address address) { // Constructors and other methods
this.customerId = customerId;
this.customerName = customerName;
this.contactNumber = contactNumber;
this.address = address;
}
public String getCustomerId() {
return customerId;
}
public String getCustonerId() {
return customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public long getContactNumber() {
return contactNumber;
}
public void setContactNumber(long contactNumber) {
this.contactNumber = contactNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}public void displayCustomerDetails(Address address) { System.out.println("Displaying customer details \\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); System.out.println("Customer Id : " + customerId); //can also use this.customerid System.out.println("Customer Name : " + customerName); //can also use this.customerName System.out.println("Contact Number :"+ contactNumber); System.out.println("Customer Address
-
I have to print old and new address. where my code is wrong?
-
I have to print old and new address. where my code is wrong?
-
You need to provide more details of where you are trying to print these details and what happens. Please edit your original post and add the requested information.
I have edited. please see where i am doing wrong?
-
I have edited. please see where i am doing wrong?
In the following lines you have calls to
getAddress()
...System.out.println("Customer Address : " +this.getAddress());
System.out.println("New Customer Address :"+ " "+this.getAddress() );
... But that call returns the
Customer.Address
object which contains various properties. So when you pass that to theprintln
method the system tries to print it. But since it has no idea how to print anAddress
object it just prints the class name and its memory address. You need to add atoString
method to yourAddress
class that returns the address formatted as a string. -
In the following lines you have calls to
getAddress()
...System.out.println("Customer Address : " +this.getAddress());
System.out.println("New Customer Address :"+ " "+this.getAddress() );
... But that call returns the
Customer.Address
object which contains various properties. So when you pass that to theprintln
method the system tries to print it. But since it has no idea how to print anAddress
object it just prints the class name and its memory address. You need to add atoString
method to yourAddress
class that returns the address formatted as a string.so, we cannot print by just passing values to object of Address class. we must have to use setter method in Address class or toString method. right?
-
so, we cannot print by just passing values to object of Address class. we must have to use setter method in Address class or toString method. right?
You should always add a
toString
method to your classes. All classes are ultimately based on theObject
class, which has a very simpletoString
method (as you have seen). So unless you provide a custom version that is what you will get for your own classes. Remember, the Java system cannot guess what properties, or in what format, you want your classes to be printed. -
You should always add a
toString
method to your classes. All classes are ultimately based on theObject
class, which has a very simpletoString
method (as you have seen). So unless you provide a custom version that is what you will get for your own classes. Remember, the Java system cannot guess what properties, or in what format, you want your classes to be printed.got it. thanx:thumbsup: :)
-
got it. thanx:thumbsup: :)
public void displayCustomerDetails(Address address) {
System.out.println("Displaying customer details \n***************************");System.out.println("Customer Obj: " + this); // same as this.toString() System.out.println("Customer Id : " + customerId); //can also use this.customerid
If you add the line above you will see something like: Customer@4645 Another approach would be to add a printDetails() method to the Address class and then you could call
… getAddress().printDetails() …