1, when using the abstract keyword to decorate a class, this class is called abstract class; When you use abstract to modify a method, the method is called an abstract method.
2. Classes containing abstract methods must be declared abstract classes, abstract classes must be inherited, and abstract methods must be rewritten.
3. Abstract classes cannot be instantiated.
4, abstract method only need to declare, and do not need to implement.
Public classtestpolymorphic{ Public Static voidMain (String args[]) {//Animal Animal = new Animal ("Animal"); Abstract classes cannot be instantiatedCat cat =NewCat ("Cat","Blue"); Dog Dog=NewDog ("Dog","Black"); //Lily L1 = new Lily ("L1", animal);Lily L2 =NewLily ("L2", dog); //L1.myanimalenjoy ();L2.myanimalenjoy (); }}classlily{PrivateString name; PrivateAnimal Animal; PublicLily (String name, Animal Animal) { This. Name =name; This. Animal =Animal; } Public voidMyanimalenjoy () {Animal.enjoy (); }}Abstract classanimal{//declaring abstract Classes PrivateString name; PublicAnimal (String name) { This. Name =name; } /*Public void Enjoy () {System.out.println ("Scream ~ ~ ~"); }*/ Public Abstract voidEnjoy ();//declaring abstract methods}classDog extends animal{PrivateString Forlorcolor; PublicDog (String name,string forlorcolor) {super (name); This. Forlorcolor =Forlorcolor; } Public voidenjoy () {System. out. println ("Dog Barking ~ ~ ~"); }}classCat extends animal{PrivateString Eyescolor; PublicCat (String name,string eyescolor) {super (name); This. Eyescolor =Eyescolor; } Public voidenjoy () {System. out. println ("Cat Scream ~ ~ ~"); }}
Operation Result:
Summary: Abstract classes are generally used in polymorphism.
The abstract class of Java