"Thinkinginjava" 9, inheritance and cleanup

Source: Internet
Author: User

/*** Book: Thinking in Java * Features: Inheritance and cleanup * file: frog.java* time: April 1, 2015 19:38:22* Author: cutter_point*/package lesson8polymorphism ; import static Net.mindview.util.print.*;class characteristic{private String s; Characteristic (String s) {this.s = S;print ("Creating characteristic" + s);} protected void Dispose () {print ("disposing characteristic" + s);}} Class Description{private string S;description (string s) {THIS.S = S;print ("Creating Description" + s);} protected void Dispose () {print ("disposing Description" + s);}} Class Livingcreature{private characteristic p = new characteristic ("Is Alive");p rivate Description t = new Descripti On ("Basic Living creature"); Livingcreature () {print ("livingcreature () + constructor");} protected void Dispose () {print ("livingcreature dispose"); T.dispose ();p. Dispose ();}} Classes inherit class Animal extends livingcreature{private characteristic p = new characteristic ("has Heart");p rivate Description T =new Description ("Animal not vegetable"); Animal () {print ("Animal ()");} protected void diSpose () {print ("Animal dispose"); T.dispose ();p. Dispose (); Super.dispose ();}} Class Amphibian extends animal{private characteristic p = new characteristic ("Can live in Water");p rivate Description t = New Description ("Both Water and Land"); Amphibian () {print ("amphibian ()");} protected void Dispose () {print ("amphibian Dispose"); T.dispose ();p. Dispose (); Super.dispose ();}} public class Frog extends amphibian{private characteristic p = new characteristic ("Croaks");p rivate Description t = new De Scription ("Eats bugs");p ublic Frog () {print ("Frog ()");} protected void Dispose () {print ("Frog dispose"); T.dispose ();p. Dispose (); Super.dispose ();} public static void Main (String args[]) {Frog Frog = new Frog ();p rint ("Bye bye!"); Frog.dispose ();p rint ("All are Over");}}




Endpoint
* 1, the first is to call the parent class constructor
* 2. After the root parent class, first instantiate the data member (typically the implementation of the class's composition)
* 3, after the data member instantiation, and then the constructor function inside the class capacity

Output:

Creating characteristic is Alive obj1
Creating Description Basic Living creature obj1
Livingcreature () + constructor obj1
Creating characteristic has heart obj1
Creating Description Animal not vegetable obj1
Animal () obj1
Creating characteristic Can live in water obj1
Creating Description Both Water and land obj1
Amphibian () obj1
Creating characteristic croaks obj1
Creating Description Eats Bugs Obj1
Frog () obj1
Bye bye! Obj1
Frog Dispose Obj1
Disposing Description Eats Bugs obj1
Disposing characteristic croaks obj1
Amphibian Dispose Obj1
Disposing Description Both water and land obj1
Disposing characteristic Can live in water obj1
Animal Dispose Obj1
Disposing Description Animal not vegetable obj1
Disposing characteristic has heart obj1
Livingcreature Dispose Obj1
Disposing Description Basic Living creature obj1
Disposing characteristic is alive obj1
All are over obj1





"Thinkinginjava" 9, inheritance and cleanup

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.