OOP 101
-
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); }
-
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); }
I hope you told him to change his.
-
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); }
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
-
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); }
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!
-
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); }
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