Unit 2 (Prog 1) : Test Inheritance of class in JAVA

By | June 1, 2013
Inheritance Test for OCJP preparation…
 * equals() method is defined under
 * java.lang.Object class which is inherited by all class having main method
 * thus every class is instance of java.lang.Object class
 * Whenever you create a class, you automatically inherit all of class Object’s methods.
public class ch21TestClassInheritance {
          public static void main(String a[]){
                    ch21TestClassInheritance a1 = new ch21TestClassInheritance();
                    ch21TestClassInheritance a2 = new ch21TestClassInheritance();
// a1 and a2 are two distinct objects of same class ch21TestClassInheritance
//condition true, both are distinct
                              System.out.println(“Equal method works !!!”);
                              System.out.println(a1.hashCode() +” ” + a2.hashCode());
//here on a1 and a2 both are instance of same class and both are equal objects
                   if(a1.equals(a2)) {
//condition true, both are equal
                             System.out.println(“Oups its dont !!!”);
                             System.out.println(a1.hashCode() +” ” + a2.hashCode());
                   if(a1 instanceof Object){
                             System.out.println(“a1 is subclass of java.lang.Object”);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.