Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Dynamic Polymorphism

Dynamic Polymorphism

Scheduled Pinned Locked Moved Java
helpsalesjsonoopquestion
10 Posts 3 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sudhanshu_India
    wrote on last edited by
    #1

    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
    
    L 1 Reply Last reply
    0
    • S Sudhanshu_India

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What is the question?

      S 1 Reply Last reply
      0
      • L Lost User

        What is the question?

        S Offline
        S Offline
        Sudhanshu_India
        wrote on last edited by
        #3

        I have to print old and new address. where my code is wrong?

        L 1 Reply Last reply
        0
        • S Sudhanshu_India

          I have to print old and new address. where my code is wrong?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • L Lost User

            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.

            S Offline
            S Offline
            Sudhanshu_India
            wrote on last edited by
            #5

            I have edited. please see where i am doing wrong?

            L 1 Reply Last reply
            0
            • S Sudhanshu_India

              I have edited. please see where i am doing wrong?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 the println method the system tries to print it. But since it has no idea how to print an Address object it just prints the class name and its memory address. You need to add a toString method to your Address class that returns the address formatted as a string.

              S 1 Reply Last reply
              0
              • L Lost User

                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 the println method the system tries to print it. But since it has no idea how to print an Address object it just prints the class name and its memory address. You need to add a toString method to your Address class that returns the address formatted as a string.

                S Offline
                S Offline
                Sudhanshu_India
                wrote on last edited by
                #7

                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?

                L 1 Reply Last reply
                0
                • S Sudhanshu_India

                  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?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You should always add a toString method to your classes. All classes are ultimately based on the Object class, which has a very simple toString 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.

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    You should always add a toString method to your classes. All classes are ultimately based on the Object class, which has a very simple toString 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.

                    S Offline
                    S Offline
                    Sudhanshu_India
                    wrote on last edited by
                    #9

                    got it. thanx:thumbsup: :)

                    E 1 Reply Last reply
                    0
                    • S Sudhanshu_India

                      got it. thanx:thumbsup: :)

                      E Offline
                      E Offline
                      englebart
                      wrote on last edited by
                      #10

                      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() …
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups