Learning notes interface, inheritance and multi-state exercises of--java core technology

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.