Java-6.3 combination of aggregation and inheritance

Source: Internet
Author: User

Java-6.3 combination of aggregation and inheritance

In this section, we will discuss the combination of aggregation and inheritance.

We have already talked about the basic knowledge and points of attention before. Now we can directly go to the Code:

1. Example

 

package com.ray.ch05;public class PlaceSetting extends Custom {private DinnerPlate dinnerPlate;private Spoon spoon;private Fork fork;private Knife knife;public PlaceSetting(int i) {super(i);dinnerPlate = new DinnerPlate(i + 1);spoon = new Spoon(i + 1);fork = new Fork(i + 1);knife = new Knife(i + 1);System.out.println(done);}public static void main(String[] args) {new PlaceSetting(9);}}class Plate {public Plate(int i) {System.out.println(create Plate);}}class DinnerPlate extends Plate {public DinnerPlate(int i) {super(i);System.out.println(create DinnerPlate);}}class Utensil {public Utensil(int i) {System.out.println(create Utensil);}}class Spoon extends Utensil {public Spoon(int i) {super(i);System.out.println(create Spoon);}}class Fork extends Utensil {public Fork(int i) {super(i);System.out.println(create Fork);}}class Knife extends Utensil {public Knife(int i) {super(i);System.out.println(create Knife);}}class Custom {public Custom(int i) {System.out.println(create Custom);}}

Output:

 

Create m
Create Plate
Create DinnerPlate
Create Utensil
Create Spoon
Create Utensil
Create Fork
Create Utensil
Create Knife
Done

 

The above is a typical example of using aggregation and inheritance at the same time.

The above example shows the initialization and aggregation objects that we mentioned previously with parameter inheritance.

Note that the compiler will not initialize the object to new, but will only initialize the object to null.

 

2. Object cleanup and cleanup Sequence

The above code is followed. Then we add a dispose method to each class to check the object cleanup and cleanup sequence.

Code:

 

package com.ray.ch05;public class PlaceSetting extends Custom {private DinnerPlate dinnerPlate;private Spoon spoon;private Fork fork;private Knife knife;public PlaceSetting(int i) {super(i);dinnerPlate = new DinnerPlate(i + 1);spoon = new Spoon(i + 1);fork = new Fork(i + 1);knife = new Knife(i + 1);System.out.println(done);}@Overridepublic void dispose() {dinnerPlate.dispose();spoon.dispose();fork.dispose();knife.dispose();System.out.println(PlaceSetting dispose);super.dispose();}public static void main(String[] args) {PlaceSetting placeSetting = null;try {placeSetting = new PlaceSetting(9);} catch (Exception e) {} finally {placeSetting.dispose();}}}class Plate {public Plate(int i) {System.out.println(create Plate);}public void dispose() {System.out.println(Plate dispose);}}class DinnerPlate extends Plate {public DinnerPlate(int i) {super(i);System.out.println(create DinnerPlate);}@Overridepublic void dispose() {System.out.println(DinnerPlate dispose);super.dispose();}}class Utensil {public Utensil(int i) {System.out.println(create Utensil);}public void dispose() {System.out.println(Utensil dispose);}}class Spoon extends Utensil {public Spoon(int i) {super(i);System.out.println(create Spoon);}@Overridepublic void dispose() {System.out.println(Spoon dispose);super.dispose();}}class Fork extends Utensil {public Fork(int i) {super(i);System.out.println(create Fork);}@Overridepublic void dispose() {System.out.println(Fork dispose);super.dispose();}}class Knife extends Utensil {public Knife(int i) {super(i);System.out.println(create Knife);}@Overridepublic void dispose() {System.out.println(Knife dispose);super.dispose();}}class Custom {public Custom(int i) {System.out.println(create Custom);}public void dispose() {System.out.println(Custom dispose);}}

Output:

 

Create m
Create Plate
Create DinnerPlate
Create Utensil
Create Spoon
Create Utensil
Create Fork
Create Utensil
Create Knife
Done
DinnerPlate dispose
Plate dispose
Spoon dispose
Utensil dispose
Fork dispose
Utensil dispose
Knife dispose
Utensil dispose
PlaceSetting dispose
Custom dispose

From the output result, we can see that the generated object is still the first parent and then the child, but the cleaning is indeed the opposite, first child and then the parent.

 

Of course, in most cases, we do not need to manually clean up, but only need the Garbage Collector to automatically clean up.

However, you must note that when you must manually clean up objects, you must always pay attention to the various aggregation and inheritance relationships between subclass and subclass, subclass and parent class.

 

3. A method that is reloaded multiple times can be reloaded in the subclass.

 

package com.ray.ch05;public class DinnerPlate extends Plate {public DinnerPlate(int i) {super(i);System.out.println(create DinnerPlate);}@Overridepublic void dispose() {}public void dispose(double i) {}}class Plate {public Plate(int i) {System.out.println(create Plate);}public void dispose() {System.out.println(Plate dispose);}public int dispose(int i) {return i;}public float dispose(float i) {return i;}}

From the code above, we can see that dispose is reloaded multiple times and overwritten in different ways.

 

 

Summary: This chapter describes how to combine aggregation and inheritance, and describes problems that may occur during use.

 

 

 

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.