A Test Class to Demonstrate Method Hiding - Java Object Oriented Design

Java examples for Object Oriented Design:Inheritance

Description

A Test Class to Demonstrate Method Hiding

Demo Code

class MySuper {//from   ww  w . j  av a 2  s.c  o  m
  public static void print() {
    System.out.println("Inside MHidingSuper.print()");       
  }
}
 class MySub extends MySuper {
  public static void print() {
    System.out.println("Inside MHidingSub.print()");  
  }
}
public class Main {
  public static void main(String[] args) {
    MySuper mhSuper = new MySub();
    MySub mhSub = new MySub();

    System.out.println("#1");

    MySuper.print();
    mhSuper.print();

    System.out.println("#2");

    MySub.print();
    mhSub.print();
    ((MySuper) mhSub).print();

    System.out.println("#3");

    mhSuper = mhSub;
    mhSuper.print();
    ((MySub) mhSuper).print();
  }
}

Result


Related Tutorials