Abstract classes are used as parameters and return values of methods

Abstract class animal:

public abstract class Animal { public abstract void eat();

}

Subclass cat:

Subclass dog:

Public class dog extends animal {@ override public void eat() {system. Out. Println ("dogs eat bones");}

}

Test class:

/** pass the abstract class type as the parameter of the method * / public class test {public static void main (string [] args) {/ / call operatoranimal and pass the subclass object cat C = new cat(); operatoranimal (c); operatoranimal (New dog());} / ** Method operatoranimal. The parameter is an abstract class * call the method and pass the object of animal type. The animal abstract class has no object * only the object of the subclass of animal (polymorphic) * / public static void operatoranimal (animal a) {/ / reference variable a and call method eat a.eat();}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>