Java: Check LinkedList<T> Classes

  • Context: Java 
  • Thread starter Thread starter Live4eva_2
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The discussion centers on the visibility issue encountered in a Java implementation of a generic LinkedList class. The user reports an error stating that "Node current" is not accessible within the LinkedList class. The root cause is identified as the protected access modifier of the current member in the Iterator class, which restricts access to the Iterator class and its subclasses, excluding the outer LinkedList class. The user confirms that the Iterator is instantiated within the LinkedList constructor, ruling out instantiation issues.

PREREQUISITES
  • Understanding of Java access modifiers (public, protected, private)
  • Familiarity with generic classes in Java
  • Knowledge of inner classes and their visibility rules in Java
  • Basic concepts of data structures, specifically linked lists
NEXT STEPS
  • Review Java access modifiers and their implications on class visibility
  • Learn about inner classes and their interactions with outer classes in Java
  • Explore Java generics in depth, focusing on their usage in data structures
  • Implement a complete LinkedList class with an Iterator to practice encapsulation and access control
USEFUL FOR

Java developers, computer science students, and software engineers interested in understanding class visibility and data structure implementation in Java.

Live4eva_2
Messages
28
Reaction score
0
Could someone check the following 3 classes for me please?

Java:
public class LinkedList<T>
{
    public Node<T> first;
    public Node<T> last;
    protected class Node<T>
    {
        public Node<T> next;
        public Node<T> last;

    }
    public class Iterator<T>
    {
        protected Node<T> current;
        protected Node<T> previous;

    }

}
I've only included the classes and variables in order to illustrate the visibility.
when I complile I'm getting the msg that "Node<T> current" isn't available in LinkedList<T> class...why not !?Am I only allowed one inner class?Also i ensured that my iterator was constructed within the LinkedList constructor so i don't think that it could be a problem with instantiating the iterator object.
 
Last edited by a moderator:
Technology news on Phys.org
The current member of Iterator<T> is protected, so it is accessible only to other members of that class or subclasses of that class. It is not accessible to outer scopes, which would include the LinkedList class.

I'm reasonably sure about what I've said, but I haven't really written any Java code for many years. If anybody has an opposing view, please jump in.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
13K
Replies
3
Views
4K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
23K
  • · Replies 1 ·
Replies
1
Views
2K