Thursday 22 February 2018

method hiding & method overriding

up vote97down voteaccepted



public class Animal {
    public static void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public static void foo() {  // hides Animal.foo()
        System.out.println("Cat");
    }
}
Here, Cat.foo() is said to hide Animal.foo(). Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen:
Animal.foo(); // prints Animal
Cat.foo(); // prints Cat

Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;

a.foo(); // should not be done. Prints Animal
b.foo(); // should not be done. Prints Animal because the declared type of b is Animal
c.foo(); // should not be done. Prints Cat because the declared type of c is Cat
d.foo(); // should not be done. Prints Animal because the declared type of b is Animal
Calling static methods on instances rather than classes is a very bad practice, and should never be done.
Compare this with instance methods, which are polymorphic and are thus overridden. The method called depends on the concrete, runtime type of the object:
public class Animal {
    public void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public void foo() { // overrides Animal.foo()
        System.out.println("Cat");
    }
}
Then the following will happen:
Animal a = new Animal();
Animal b = new Cat();
Animal c = new Cat();
Animal d = null;

a.foo(); // prints Animal
b.foo(); // prints Cat
c.foo(); // prints Cat
d.foo(): // throws NullPointerException
---


First of all What is meant by method Hiding?
Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact it will be determined by the type of reference variable used to invoke the method
What is meant by method overriding? 
Method overriding means subclass had defined an instance method with the same signature and return type( including covariant type) as the instance method in superclass. In that case method of superclass is overridden(replaced) by the subclass. It signifies that: The version of method that is executed will be determined by the object that is used to invoke itIt will not be determined by the type of reference variable used to invoke the method.
Why can't static methods be overridden?
Because, static methods are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. 
How should static methods be accessed?
Static methods should be accessed in static way. i.e. by the name of class itself rather than using an instance.



https://stackoverflow.com/questions/16313649/what-is-method-hiding-in-java-even-the-javadoc-explanation-is-confusing

Tuesday 13 February 2018

Collections Java

https://beginnersbook.com/java-collections-tutorials/

https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/


-------------
HashSet


import java.util.HashSet;
import java.util.Iterator;


public class SetExample {


public static void main(String args[]){
HashSet<String> hset = new HashSet<String>();

hset.add("apple");
hset.add("oragne");

//System.out.println(hset);
hset.add("apple");
hset.add("banana");
//System.out.println(hset);



  Iterator<String> it =hset.iterator();
  while(it.hasNext()){
  System.out.println(it.next());
  }
 
}
}


------------------------------------

String array[]=new String[hset.size()];
hset.toArray(array);
for(String temp: array){
System.out.println(temp);
}


--------------------------------------------------------



Hash Map


 Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }




--------------------------------------------------------------------------------------


Hash Table



Hashtable<String, String> hashtable = 
              new Hashtable<String, String>();

Enumeration names = hashtable.keys();
   while(names.hasMoreElements()) {
      key = (String) names.nextElement();
      System.out.println("Key: " +key+ " & Value: " +
      hashtable.get(key));
   }