多态 
1、多态的体现 
   父类的引用指向了自己的子类对象。 
   父类的引用也可以接收自己的子类对象。 
2、多态的前提 
   必须是类与类之间有关系,要么继承、要么实现。 
   通常还有一个前提:存在覆盖。 
3、多态的好处 
   多态的出现大大的提高了程序的扩展性。   
4、多态的弊端 
   提高了扩展性,但只能使用父类的引用访问父类中的成员。 
 
前期预先调用功能,后期定义子类去实现功能,并把子类作为参数传递进来,从而实现后期扩展性。 
 
abstract class Animal 
{ 
        public abstract void eat(); 
} 
class Cat extends Animal 
{ 
        public void eat() 
        { 
                System.out.println("吃鱼"); 
        } 
        public void catchMouse() 
        { 
                System.out.println("抓老鼠"); 
        } 
} 
class Dog extends Animal 
{ 
           public void eat() 
            { 
                   System.out.println("吃骨头"); 
            } 
           public void kanJia() 
           { 
           System.out.println("看家"); 
       } 
} 
class Pig extends Animal 
{ 
         public void eat() 
         { 
                 System.out.println("饲料"); 
         } 
         public void gongDi() 
         { 
                 System.out.println("拱地"); 
         } 
} 
 
class DuoTaiDemo 
{ 
        public static void main(String[] args)  
          { 
                  Animal a = new Cat();//类型提升。 向上转型。 
                  a.eat(); 
 
                  //如果想要调用猫的特有方法时,如何操作? 
                  //强制将父类的引用。转成子类类型。向下转型。 
                  Cat c = (Cat)a; 
                  c.catchMouse(); 
          } 
} 
 |   
        
 
    
    
    
     
 
 |