1. Create an abstract class to verify that it can instantiate an object.
Package COM.LZW; Public Abstract class UseCase3 { abstractvoid doit (); Public Static void Main (String args[]) { new UseCase3 ();} }
2. Try creating a parent class, creating two methods in the parent class, overriding the second method in the subclass, creating an object for the subclass, transforming it upward to the base class, and calling the method.
PackageCOM.LZW; Public classUseCase2 { Public voiddosomething () {System.out.println ("Parent class. DoSomething ()"); } Public voiddoanything () {System.out.println ("Parent class. Doanything ()"); } Public Static voidMain (String args[]) {UseCase2 u=NewSub (); U.dosomething (); U.doanything (); }}classSubextendsusecase2{ Public voiddoanything () {System.out.println ("Subclass. Doanything ()"); }}
3. Try to create a parent class and subclass, create a constructor method, and then add member variables and methods to the parent and child classes, and summarize the order in which the subclass objects are constructed.
Package Com.lzw;public Abstract class UseCase1 {abstract void testabstract (); UseCase1 () {//(1) First executes the parent constructor method System.out.println ("Before Testabstract ()"); Testabstract ();//If an abstract method is called, the method that the subclass overrides is called. This calls the Testabstract () method of the Atest class System.out.println ("After Testabstarcat ()");} public static void Main (String args[]) {new atest ();}} Class Atest extends Usecase1{private int i=1;//(2) causes member variables to initialize void Testabstract () {System.out.println ("testabstract ()" + i);} Public atest () {//(3) Call Subclass construction Method System.out.println (i);}}
Learning notes interface, inheritance and multi-state exercises of--java core technology