Feed on
 Posts
 Comments
Java Beans dot Asia

Just a few simple tutorials …

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&lt;? extends E&gt; 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...

Related posts:

  1. 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....
  2. 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...
  3. Brainteaser: Overridable methods
    Consider the following case of inheritance: public class Parent { public Parent() { getValue(); } public void getValue() { } } public class...
  4. 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...
  5. 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...