class A{ public int getVal(){ return(5); } } class B extends A{ public int getVal(){ return(10); } } public class override { public static void main(String[] args) { B b = new B(); A a= (A)b;//把 b 强 制转换成A的类型 int x=a.getVal(); System.out.println(x); } } Overload实例 package com.guonan; //Demostrate method voerloading. class OverloadDemo { void test(){ System.out.println("NO parameters"); } void test(int a){ System.out.println("a:"+a); }//end of Overload test for one integer parameter. void test(int a, int b){ System.out.println("a and b:"+a+" "+b); } double test(double a){ System.out.println("double a:"+a); return a*a; } } class Overload{ public static void main(String[] args) { OverloadDemo ob = new OverloadDemo(); double result; ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25):"+result); } }