Unit 2 (Prog 2) : Accessing Super Class method

By | June 2, 2013
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);
          }
}

Leave a Reply

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