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