[HD Pg 238, Sec. 9.1.5 - information-hiding]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by OCAJO1 »

Side Question,

Code: Select all

class Account{
    int accountNumber;
    protected double balance;
    
    public void setAccountNumber(int value){
        this.accountNumber = value;
    }
    
    public int getAccountNumber(){
        return accountNumber;
    }
    
    public void setBalance(double value){
        this.balance = value;
        
    }
    
    public double getBalance(){
        return balance;
    }
}

public class DumpAccountInfo{
        public static void printAccounts(Account[] accts){ // line 2
               for(Account acct : accts){
                    System.out.println(acct.getAccountNumber()+" "+acct.getBalance());
                }
        }
}
Both classes are in the same package. The compiler issued a warning on // line 2, stating that exporting non-public type through public API. When I removed the public access from line 2, the warning went away.

Question,

Does this warning mean that since Account class' access is default, assigning parameter(s) of type Account in a public method somehow could allow access beyond default setting to the Account class?

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by admin »

Stop using IDE. Use the command line.
Or you might want to ask the reason for this on the particular IDE forum.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by OCAJO1 »

I did what you suggested, and asked them. This is what they said,

NetBeans warns you about this, because most likely it is an unintentional error - why do you want to make some class' field values accessible to all without publishing their type, effectively preventing others from using them?

If you want to make them accessible only to other classes within the same package (or subclasses in other packages), declare them as package private (or protected), not public.

In your particular case: Class Account has package private access, its fields have package private and protected access. Then why would you give printAccounts method which is used to print contents of these fields, has a parameter and an enhanced for-loop variable of type Account, and is in the same package as Account - a public access?

But if it is an intentional declaration due to a reason not visible in the code segment provided - then ignore the warning and leave it as is.


I guess one might say that they're warnings are designed to alert the programmer to possible inconsistencies in the code!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by admin »

:thumbup: Thanks for posting the discussion here.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by OCAJO1 »

I created the following test segment to see the relationship between Account and DumpAccountInfo classes.

Code: Select all

package accounts;


class Account{
    
    int accountNumber;
    protected double balance;
    
    public void setAccountNumber(int value){
        this.accountNumber = value;
    }
    
    public int getAccountNumber(){
        return accountNumber;
    }
    
    public void setBalance(double value){
        this.balance = value;
        
    }
    
    public double getBalance(){
        return balance;
    }
}

public class accounts {

    public static void main(String[] args) {
        
       // Account a = new Account(); //This should have been inside the loop.
          
        Account aa[] = new Account[3]; 
        int i = 0;
        
        while (i < 3){
            Account a = new Account();
            a.setAccountNumber(i+1000);
            a.setBalance((i+1)*200.0);
            aa[i] = a;
            System.out.println(a.getAccountNumber()+" "+a.getBalance());
            i++;        
        }
        
        DumpAccountInfo.printAccounts(aa);
    }
    
}

Code: Select all

package accounts;

public class DumpAccountInfo {
    
    static void printAccounts(Account[] accts){
        
        for(Account acct : accts)
            System.out.println(acct.getAccountNumber()+" "+acct.getBalance());
    }
}

The print statement within the while loop prints all three sets of Account objects. However the call to the printAccounts method only prints the last set of Account objects three times!

Note & Questions: I figured out what the problem was, and moved the instantiation of Account inside the while loop.

1. Just to get it straight in my mind, moving the instantiation inside the loop - did it act the same way as initializing the array with three different objects like this, aa[] = {new a.x(), new a.y(), new a.z()}?

2.However, if it were necessary to keep referring to the same Account object, how will the code change (ArrayList declaration for aa and printAccounts method's parameter - perhaps)?

Thanks
Last edited by OCAJO1 on Fri Mar 08, 2019 2:27 pm, edited 4 times in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by admin »

Not sure what you mean. Please elaborate what you are trying to get at.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by OCAJO1 »

If you're asking about question 1, is just that, a clarification question. Checking if I'm right or wrong.

As for question 2, I am asking is there a way to code so the same Account object being overwritten each time the loop iterates, avoiding the call to Account's constructor on every iteration of the loop? In other words, Account a = new Account(); being outside the loop, and yet each new iteration update gets added to the aa array.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by admin »

Couldn't understand either of your questions. What is this - aa[] = {new a.x(), new a.y(), new a.z()}? What do you mean by "act the same way"??

If you want to add different account objects to the array, then you can't avoid creating new Account objects (and therefore executing Account constructors). You need to actually write and execute the code that you are trying to compare. You can also print out the contents of the arraylist in both the situations to see what is happening.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by OCAJO1 »

1. I guess it was very bad way of saying that end result of adding each object with a different value to the array in each iteration, was similar to initializing the array with three different objects to begin with.

2. Looking at what I was after in the code, I would say whether is being stored in an array or arraylist, was not the issue. The issue was verifying that assigning new values to an object without executing the constructor, is not possible. Unlike the good old local variables that can be used and reused in place :)

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by DazedTurtle »

Based on the topic name, I assume this mistake is a result of rearranging sections, since "Hiding" is in section 12.6.1 for me.
I will discuss the rules of casting later
Casting was already covered in 12.5.2.

(There's also a bit in the previous paragraph acting like it's a new concept, but Kindle Cloud Reader does not allow copy&paste, and I don't feel like transcribing it.)

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 238, Sec. 9.1.5 - information-hiding]

Post by admin »

You are right. In the OCP 11 version of the book, this statement should be changed.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 27 guests