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. Other Discussions
  3. The Weird and The Wonderful
  4. OOP 101

OOP 101

Scheduled Pinned Locked Moved The Weird and The Wonderful
java
5 Posts 5 Posters 6 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.
  • L Offline
    L Offline
    Leisuresuit Larry
    wrote on last edited by
    #1

    Java code: public class Account { ... public String type; public String acctNumber; public String balance; ... } public class CreditCardAccount extends Account { ... } public Account getAccount(String id) { ... } public void transferMoney(Account account, double amount) { ... if () { CreditCardAccount cca = (CreditCardAccount)account; ... } ... } Looks okay so far, but I got a ClassCastException when I did: Account account = getAccount("12345"); ... transferMoney(account, amount); It turns out, transferMoney *never* returns a CreditCardAccount object, but instead returns an Account object, with the type member set to "CreditCardAccount". The engineer who wrote this told me that I should change my code to: Account account = getAccount("12345"); if (account.type.equals("CreditCardAccount")) { CreditCardAccount cca = new CreditCardAccount(); cca.type = account.type; cca.acctNumber = account.acctNumber; cca.balance = account.balance; ... transferMoney(cca, amount); }

    P A N L 4 Replies Last reply
    0
    • L Leisuresuit Larry

      Java code: public class Account { ... public String type; public String acctNumber; public String balance; ... } public class CreditCardAccount extends Account { ... } public Account getAccount(String id) { ... } public void transferMoney(Account account, double amount) { ... if () { CreditCardAccount cca = (CreditCardAccount)account; ... } ... } Looks okay so far, but I got a ClassCastException when I did: Account account = getAccount("12345"); ... transferMoney(account, amount); It turns out, transferMoney *never* returns a CreditCardAccount object, but instead returns an Account object, with the type member set to "CreditCardAccount". The engineer who wrote this told me that I should change my code to: Account account = getAccount("12345"); if (account.type.equals("CreditCardAccount")) { CreditCardAccount cca = new CreditCardAccount(); cca.type = account.type; cca.acctNumber = account.acctNumber; cca.balance = account.balance; ... transferMoney(cca, amount); }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I hope you told him to change his.

      1 Reply Last reply
      0
      • L Leisuresuit Larry

        Java code: public class Account { ... public String type; public String acctNumber; public String balance; ... } public class CreditCardAccount extends Account { ... } public Account getAccount(String id) { ... } public void transferMoney(Account account, double amount) { ... if () { CreditCardAccount cca = (CreditCardAccount)account; ... } ... } Looks okay so far, but I got a ClassCastException when I did: Account account = getAccount("12345"); ... transferMoney(account, amount); It turns out, transferMoney *never* returns a CreditCardAccount object, but instead returns an Account object, with the type member set to "CreditCardAccount". The engineer who wrote this told me that I should change my code to: Account account = getAccount("12345"); if (account.type.equals("CreditCardAccount")) { CreditCardAccount cca = new CreditCardAccount(); cca.type = account.type; cca.acctNumber = account.acctNumber; cca.balance = account.balance; ... transferMoney(cca, amount); }

        A Offline
        A Offline
        Andy Brummer
        wrote on last edited by
        #3

        Leisuresuit Larry wrote:

        The engineer who wrote this told me that I should change my code to

        I think you meant: The "engineer" who wrote this told me that I should change my code to That's wrong on so many different levels. I hope you aren't dealing with a system where this kind of thing is rampant. If so, run, run far away.


        This blanket smells like ham

        1 Reply Last reply
        0
        • L Leisuresuit Larry

          Java code: public class Account { ... public String type; public String acctNumber; public String balance; ... } public class CreditCardAccount extends Account { ... } public Account getAccount(String id) { ... } public void transferMoney(Account account, double amount) { ... if () { CreditCardAccount cca = (CreditCardAccount)account; ... } ... } Looks okay so far, but I got a ClassCastException when I did: Account account = getAccount("12345"); ... transferMoney(account, amount); It turns out, transferMoney *never* returns a CreditCardAccount object, but instead returns an Account object, with the type member set to "CreditCardAccount". The engineer who wrote this told me that I should change my code to: Account account = getAccount("12345"); if (account.type.equals("CreditCardAccount")) { CreditCardAccount cca = new CreditCardAccount(); cca.type = account.type; cca.acctNumber = account.acctNumber; cca.balance = account.balance; ... transferMoney(cca, amount); }

          N Offline
          N Offline
          Nickolay Karnaukhov
          wrote on last edited by
          #4

          He don't deserve the "engineer"... He can be named "mess-professional"... But honestly - it's really really bad to work with such people who create mess themself and then argue when someone telling them that they're wrong...

          ------------------------------------------------------------ Want to be happy - do what you like!

          1 Reply Last reply
          0
          • L Leisuresuit Larry

            Java code: public class Account { ... public String type; public String acctNumber; public String balance; ... } public class CreditCardAccount extends Account { ... } public Account getAccount(String id) { ... } public void transferMoney(Account account, double amount) { ... if () { CreditCardAccount cca = (CreditCardAccount)account; ... } ... } Looks okay so far, but I got a ClassCastException when I did: Account account = getAccount("12345"); ... transferMoney(account, amount); It turns out, transferMoney *never* returns a CreditCardAccount object, but instead returns an Account object, with the type member set to "CreditCardAccount". The engineer who wrote this told me that I should change my code to: Account account = getAccount("12345"); if (account.type.equals("CreditCardAccount")) { CreditCardAccount cca = new CreditCardAccount(); cca.type = account.type; cca.acctNumber = account.acctNumber; cca.balance = account.balance; ... transferMoney(cca, amount); }

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            He's obviously object-disoriented.

            ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown God is the only being who, to rule, does not need to exist. -- Charles Baudelaire

            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