Author Archives: KisHan SarsecHa

Unit 8 (Prog 2) : Test Inner Class 2nd

In this example you will find Anonymous Inner class concepts, Inner class concepts in java, and also some tricks about Interface.

/*
 * TestAnonymousInnerClass.java
 *
 * This Example includes…
 *          1. Method inside Interface with body.
 *          2. Inner Class inside Interface.
 *          3. Anonymous Inner class of abstract class.
 *          4. Anonymous Inner class of non-abstract class.
 *          5. Anonymous Inner class of Interface.
 * */

Unit 2 (Prog 5) : Constructors in java

Constructor Demo code…

/*
 * 1. Constructor can use any return type.
 * 2. Constructor name must match the name of class.
 * 3. constructor must not have a return type.
 * 4. If you see a method with return type having same name as class than it is just a method.
 * 5. if you’ve typed in a constructor with arguments, 
 *  you won’t have a no-arg constructor unless you type it in yourself!
 * 6. Interface do not have constructors.
 * 7. constructors are never inherited.
 */
class DemoClass{
          String demoString;
          DemoClass(String value){
                    this.demoString = value;
          }
}
class ChildDemo extends DemoClass{
          ChildDemo(){
          // super(); // Error here, no default constructor found in super class.
                    super(“from child”); // now compiler feels good.
          }
// more code…
}
// check out what happens after removing comments from following declaration. 
/*
class AnotherChild extends DemoClass{
* Compiler error like…
* Implicit super constructor DemoClass() is undefined for default constructor. 
* Must define an explicit constructor 
 
} */
public class ConstructorDemo {
           public static void main(String a[]) {
           // following code will not compile after removing comments 
          // because no suitable constructor found.
          // DemoClass dm1 = new DemoClass();
 
           // legal code
                    DemoClass dm2 = new DemoClass(“constructor found”);
          }
}
/*
 * Default constructor….
 * has same name and return type as class.
 * has no arguments.
 * includes no-arg call to super() constructor or call to this().
 * */

Unit 2 (Prog 4) : Test Overloading and Overriding

Overloading v/s Overriding methods…

class Operator {
          public int operation(int x, int y) {
                    System.out.println(“First method invoked from parent class.”);
                    return x + y;
          }
          public double operation(double x, double y) {
                    System.out.println(“Second method invoked from parent class.”);
                    return x + y;
          }
}
class ChildOperator extends Operator {
           public int operation(int x, int y) {
           // overriding method operation().
                     System.out.println(“First method invoked from child class.”);
                     return x + y;
           }
           public int operation () {
           // overloading method operation().
                     return 0;
           }
}
public class TestOverloading {
          public static void main(String a[]) {
                    Operator op = new Operator();
                    // here object type and reference type both are Operator.
                    double ans1 = op.operation(5, 5);
                    System.out.println(ans1);
                    int ans2 = (int) op.operation(5.0, 8.0);
                    System.out.println(ans2);
                    double ans3 = op.operation(5.0, 8.0);
                    System.out.println(ans3);
                   Operator opChild = new ChildOperator();
                   // Reference type is Operator and object type is ChildOperator.
                   opChild.operation(5, 5);
                  // supertype method will be invoked.
          }
}
/* The reference type will decide which method will be invoked in overloading at compile time…not object type. 
 * The Object type will decide which method will be invoked in overriding at run time…not reference type.
 * */
 

Unit 1 ( Prog 2) : Test Access Modifiers

Use of access modifiers and class properties…

/* 
 * Question : value of rightFirst will be always one third of leftFirst or not ?
 * NO because both are public, no need to use setLeft() method for that
 * those are directly accessible…:p
 * but it is true in case of class ValueSecond
 */
class ValueFirst {
          public int leftFirst;
          public int rightFirst;
          public void setLeftFirst(int leftNum) {
                    leftFirst = leftNum;
                    rightFirst = leftNum / 3;
          }
          public String printValues(){
                    return “left of ValueFirst Class : ” + leftFirst + “nright of ValueFirst Class : ” +  rightFirst ;
          }
         // remaining function code goes here.
}
class ValueSecond {
          private int leftSecond = 9;
          private int rightSecond = 3;
          public void setLeftFirst(int leftNum) {
                    leftSecond = leftNum;
                   rightSecond = leftNum / 3;
          }
          // remaining function code goes here.
          public String printValues(){
                   return “left of ValueSecond Class : ” + leftSecond + “nright of ValueSecond Class : ” +  rightSecond ;
          }
}
public class testAccessModifier {
          public static void main(String a[]) {
                    ValueFirst v1 = new ValueFirst();
                   // here it goes…
                   v1.leftFirst = 5;
                   v1.rightFirst = 10;
                   System.out.println(v1.printValues());
                   ValueSecond v2 = new ValueSecond();
                    // whoah…compilation error in next line after removing comments…:D
                    // v2.leftSecond = 5;
                   // v2.rightSecond = 10;
                    v2.setLeftFirst(9);
                    System.out.println(v2.printValues());
          }
}
/* Check access modifiers of the properties of function first if you find an easy question.
 * it might be twisted…:p 
 */

 

UNIT 1 (Concept 1) : Access Modifiers in java

Visibility of the properties  of class for different access modifiers 

——————————————————————————————————————–

1) From the same class

          Public , Protected, Private and Default

2) From any class in same package

          Public, Protected and Default

3) From a subclass in same package

          Public, Protected and Default

4) From subclass outside the same package

          Public and Protected (by inheritance)

5) From any non-subclass or class outside the package

          Public only

Unit 2 (Prog 3) : Test equals() method.

Testing of equals() method and overriding equals() method of Object class.

// Overriding equals() method will make an object identical
// to use a object as a key, one has to override equals() method of Object class
 
public class EqualsTest {
          public static void main(String[] args) {
                    TestClass one = new TestClass(8);
                    TestClass two = new TestClass(8);
                    if (one.equals(two)) {
                              System.out.println(“one and two are equal”);
                   }
          }
}
class TestClass {
          private int testValue;
          TestClass(int val) {
                    testValue = val;
          }
          public int getTestValue() {
                    return testValue;
          }
//overriding equals() method of Object class
         public boolean equals(Object o) {
                   if ((o instanceof TestClass) && (((TestClass) o).getTestValue() == this.testValue)) {
                             return true;
                  } else {
                             return false;
                  }
         }
}
/* 
 * equals method is public in java.lang.Object class. 
 * while overriding the equals() method it must be declared as public. 
 * visibility can not be reduced in overriding a method. 
 */

Unit 2 (Prog 2) : Accessing Super Class method

Accessing Super Class method :
class ParentShape {
          public void displayShape(Object o) {
                    System.out.println(“displaying parent shape from ” + o.toString() );
          }
// more code
}
class ChildOne extends ParentShape {
          public void childOneMethod() {
                    System.out.println(“this is method of childOne class”);
          }
}
class ChildTwo extends ParentShape {
          public void childTwoMethod() {
                    System.out.println(“this is method of childTwo class”);
          }
}
class accessSuper{
// parent class’ object as a parameter of method
          public static void doShapes(ParentShape shape) {
//invoking parent class method
                    shape.displayShape(shape);
          }
}
public class ch22AccessingSuperClass {
          public static void main(String a[]) {
                    ChildOne one = new ChildOne();
                    ChildTwo two = new ChildTwo();
//objects of child class invoking method of parent class
/*
* parameter of accessSuper.doShape() is super class’ object
* although it can be invoke by objects of child class
*/
                    accessSuper.doShapes(one);
                    accessSuper.doShapes(two);
          }
}