Create subclass parent class and apply up transformation p188_2

Source: Internet
Author: User

Parent Class Code:

1  Public classFather {2 3     4     PrivateString name;//name5     6     Private intAge//Age7     8     //the way to eat9      Public voideat () {Ten          OneSystem.out.println ("is eating! "); A     } -      -     //How to work the      Public voidWork () { -          -SYSTEM.OUT.PRINTLN ("Working! "); -     } +  -     //getter and Setter methods +      PublicString GetName () { A         returnname; at     } -  -      Public voidsetName (String name) { -          This. Name =name; -     } -  in      Public intGetage () { -         returnAge ; to     } +  -      Public voidSetage (intAge ) { the          This. Age =Age ; *     } $}

The code for the Child class:

1  Public classSonextendsfather{2 3     //subclass Inherits Parent class4     //overriding methods of work in the parent class5     //In addition to the method body must be identical to the method of the parent class when overriding?6      Public voidWork () {7         8System.out.println ("Learning, not working yet!"));9     }Ten}

Test class:

1  Public classTestson {2 3      Public Static voidMain (string[] args) {4         5Father s =NewSon ();6         7S.work ();//A method that executes a subclass8     }9     Ten      One      A      -}

Operation Result:

I'm learning, I don't have a job!

Create subclass parent class and apply up transformation p188_2

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.