The private methods of a class in Java are visible to its subclasses.?
What implementation of Set would you use to get the objects in the order they were inserted?
Answer 1 :
There are 3 access modifiers in Object-Oriented Programming :
1. Private: Private methods and variables can only be used by the members which are present within the class and cannot be inherited.
2. Protected: Protected methods and variables can be used by members within the class and by the members of the subclass. Hence, Can be inherited.
3. Public: Methods and data members that are declared public can be accessed from anywhere.
So, to answer the question,
No, the private methods of a class in Java are not visible to its subclasses. To make it visible for the subclasses, the user needs to declare the method as 'Protected'.
Answer 2 : LinkedHashSet
The implementation needed for a Set is to add Hash Table and Linked List with predictable iteration order. A normal HashSet iterator is very random but with the help of a linked list, it solves the problem. The linked list defines the order of the iteration i.e. the order in which the elements were inserted to the set.
Please upvote if my answer helped. Thanks and stay safe
Get Answers For Free
Most questions answered within 1 hours.