The following declarations in Java you should assume that ea
The following declarations in Java (you should assume that each class would be placed in a different file):
Answer the following yes/no questions about the above code and for each answer explain why you answered as you did:
1- Is it legal to access the value variable in statement 1?
2- Is it legal to access the name variable in statement 2?
3- Is it legal to access the header variable in statement 3?
4- Is it legal to access the sentinelNode variable in statement 4?
5- Is it legal to access the value variable in statements 3&4 (even if you answered no to either statement 3 or 4, assume that you had answered yes and consider whether based on a \"yes\" answer, if value would be accessable)? Hint the answer and reason is the same in both cases.
package LinkedList; class ListNode f protected int value; String name; package LinkedList; public class List List Node header protected ListNode Sentinel Node; public List header new ListNode header value 10 1) 2) header me brad. sentinel Node new L package Linkedoueue; class Queue extends LinkedList.List public Queue() header value 20 3) sentinel Node. value 30Solution
Hi,
Please find the answers as below:
1- Is it legal to access the value variable in statement 1?
Ans: Yes its perfectly legal to access. Both List and ListNode are in same package and header.value is declared protected so it will not create any compile/run time error
2- Is it legal to access the name variable in statement 2?
Ans: Yes its perfectly legal again same reason as both class are in same package (List and ListNode)
3- Is it legal to access the header variable in statement 3?
Ans: NO its illegal to access header variable in statement 3. header variable is declared inside class List which is in LinkedList package. Also header variable is having access modifier as default so it cannot be accessed outisde package
4- Is it legal to access the sentinelNode variable in statement 4?
Ans: No its illegal to access Sentinel Node in statement 4. Sentinel Node is decalred inside class List and is of type ListNode. Both these classes List and ListNode lie in same package LinkedList, however they are being accessed outisde package and are having default access modifier. Hence it will give compile time error
5- Is it legal to access the value variable in statements 3&4 (even if you answered no to either statement 3 or 4, assume that you had answered yes and consider whether based on a \"yes\" answer, if value would be accessable)?
Ans: No, The concept here is of usage of access modifiers. Both the List and List Node are declared inside package LinkedList and are having default access modifiers. So they cannot be used outside the packeg in different package. If they are to be used outside then they need to defined with public access modifiers.
