Consider the following case of inheritance:
public class ExtendingHashSet<E> extends HashSet<E> {
private int counter = 0;
public ExtendingHashSet() {
}
@Override
public boolean add(E e) {
counter++;
return super.add(e);
}
@Override
public boolean addAll(Collection<? extends E> c) {
counter += c.size();
return super.addAll(c);
}
public int getCounter() {
return counter;
}
}
Created instance:
ExtendingHashSet<String> s = new ExtendingHashSet<String>();
s.addAll(Arrays.asList("one", "two", "three"));
Question: What value would s.getCounter() method return at this point and why?
Looking forward for your answers dear readers
GD Star Rating
loading...
loading...
Related posts:
- Brainteaser: Hidden Iterators
… While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated.... - Brainteaser: Broken Comparator
Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why? import java.util.*; public class... - Brainteaser: Overridable methods
Consider the following case of inheritance: public class Parent { public Parent() { getValue(); } public void getValue() { } } public class... - How to Set SecurityManager and Java Security Policy Programmatically
In this example I want to show how to use SecurityManager to prevent unauthorized access to private members of a Java class, for... - Hack any Java class using reflection attack
Have you ever thought how secure your application is? Well reflection attack can demonstrate how vulnerable Java classes are. In this post, I...
