inheritance and polymorphism java -


I have my base class, the customer's address, in it there are 3 sub-sections account 1, account 2 and acount3. When I go through a loop, to check everybody's every type and then properly assigning it, I finally get empty data fields.

Is such a thing possible?

  public static and readFile (string filename, customer [] review) throws IOException {scanner scan = new scanner (new file (filename)); * * * Read separate records separately * / Customer first = new customer (); Account 1 first 1 = new account 1 (); Account 2 First 2 = New Account 2 (); Account 3 First 3 = New Account 3 (); String [] a = scan NXTIN () Partition ("="); First.set_account_id (Integer.parseInt (a [1] .trim ())); A = Scan. NXTIN (). Partition ("="); First.set_name (a [1] .toUpperCase () trim ().); A = Scan. NXTIN (). Partition ("="); First.set_address (a [1] .trim ()); A = Scan. NXTIN (). Partition ("="); First.set_accType (a [1] .trim ()); If (first .get_accType (). Equals ("saving")) {first = first1; } And if (first .get_accType (.) Equal ("checking")) {first = first2; } And if (first .get_accType (). Equals ("Fixed") {first = first3; A = Scan. NXTIN (). Partition ("="); First3.set_intRate (Double.parseDouble (one [1] .trim ())); }   

This code gives me empty data fields for my objects.

We first need to distinguish between one object and one variable. A variable type can catch the reference of any object type of customer type from any type of customer or customer. Assignment sets the variable to refer to a different object. Assignment can not change the object at all, with this argument, the assignment is not in the loss of data.

Now, you asked if an object of a base class can be assigned to a variable that is used to capture the derivative type sub-class obj = new base class () < / Code>. This can not be done because the object of the base class is not an object of a derivative type. Another way around this is possible, for example, 'baseclass ozz = new subclass' is an object of the base class in the form of a derivative type

However, if you have a variable Which refers to the reference type of the base type and the object of a derivative type is assigned to this variable, then you assign the object to the object using the type of castings derived Can be:

  baseclass obj = new sub-class (); Subclass subObj = (all class) obj;   

In your example, you do the following:

  1. You create client objects and specify references for variables
  2. You can change the status of the client object referenced in the first variable
  3. You replace the reference in the first variable in the context of an account 1 object

    The results of this series will not get results, which I have collected, setName and SetAdd methods are called in account 1 object. Basically, the assignment first = first 1 will reset the condition again when the first variable (the internal state of the first object and first 1 remains uncontrolled) will be seen.

    Whatever you want to do, it should be done as follows:

      Private static string next property (scanner scanner) {return scanner.nextLine (). Split ("="). Trim ()} public static read read (...) {... last int accountId = Integer.parseInt (nextProperty (scan)); Last String AccountName = Next Property (Scan); Last String Account Acadired = Next Property (Scan); Last String Account Type = Next Property (Scan); Client account = null; If (accountType.equals ("saving")) {account = new account1 (); } And if (accountType.equals ("check")) {account = new account2 (); } And if (accountType.equals ("fixed")) {account = new account3 (); (Account3) account) .set_intRate (Double.parseDouble (nextProperty (scan)); } Other {Delete new invalid level ("encountered unexpected account type"); } Account.set_account_id (accountId); Account.set_name (account's); Account.set_address (accountAddress); Account.set_accType (ACCOUNTTYPE); ...}    

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -