Class relationships -- inheritance, inclusion, dependency

Posted by Satabi2 on Fri, 22 Oct 2021 05:56:19 +0200

Relationship between classes
A is a B generalization (inheritance Implementation)
Inheritance through the extends keyword (single inheritance)
Implementation through the implements keyword (multiple implementation interfaces)
Note: the differences between method rewriting and overloading are Object class and method memory structure
A has-a B contains (combined aggregation Association)
Place an object of one class in another class as an attribute
A use-a B need-a
The method of one class uses the new method inside the object method of another class to pass parameters

Class relationship design: high cohesion and low coupling inheritance (Implementation) > composition > aggregation > Association > dependency

Inheritance: 1. The subclass inherits from the parent class through the keyword extends
2. You can call the properties and methods of the parent class for your own use. You can also add properties and methods yourself

  • Constructor is called by default from parent class to child class

  • Block is executed by default before calling the constructor
    3. If the inherited method does not meet the requirements, it can be rewritten (the next section describes the difference between method overloading and rewriting in detail)
    4.Object is the parent of any reference class (directly or indirectly)
    object is the final class and has no parent class
    There are nine object methods in total (next section)

    *5. Use of this and super
    this and super refer to pronouns instead of objects
    this replaces the object when the method is currently executed, not necessarily the object of the current class
    super replaces the parent object of the object when the method is currently executed
    Can call general properties and general methods
    It can be placed anywhere in a class member (attribute method building block)
    Note: when calling general methods, you can call each other back and forth (easy to write and compile) and execution may cause problems (StackOverflowError)
    Constructor can be called (placed on the first line of constructor)
    The way in which this and super call another class in the construction method can not appear in the first line at the same time.
    Constructors cannot call each other back and forth (compilation is difficult)


public class Car {
    public String brand;//Automobile brand
    public String type;//model
    public String color;//colour

    public Wheel wheel;//There is a wheel in the car -- > including the relationship
    //Construction method
    public Car(){ }
    public Car(String brand,String type,String color,Wheel wheel){
        this.brand= brand;
        this.type = type;
        this.color = color;
        this.wheel = wheel;
    public void showCar(){
        System.o ut.println("This is a car"+brand+"brand"+type+"model"+color+"a car");
        System.out.println("The car is carrying"+wheel.brand+"Card"+wheel.size+"size"+wheel.color+"Colored wheels");
        wheel.trun();//The method of the car wheel called by a certain object must be that the car wheel object call can be placed anywhere

public class Wheel {
    public String brand;//brand
    public int size;//size
    public String color;//colour

    //Construction method
    public Wheel() { }
    public Wheel(String brand,int size,String color){
        this.brand = brand;
        this.size = size;
        this.color = color;

    public void trun(){//turn
        System.out.println("The wheels of the car can rotate");

rely on

Java programs are embodied in the following forms:
An object of another class is used in the method of one class
The first is to pass parameters in a method
The second can be created directly in the method

package rely;

public class Pig {//Describe the of pigs
    private String name;//name
    private int weight = 20;//weight

    //Construction method
    public Pig(){ }
    public Pig(String name){;

    //Describe a method to indicate that a pig is killed
    public void beKilled(){
        System.out.println("It's terrible to be killed");

    //Describe a way to make pigs grow meat
    //    Every month is twice as high as the previous month
    //   The return value represents the weight of the growing pig
    public void growUp(int month){
        for(int i=1;i<=month;i++){
    //Describe a method to inform pigs of their weight
    public int getWeight(){
        return this.weight;

    public String getName(){

package rely;

public class Butcher {//Describe the butcher
    //Attribute name has knife
    //Describing a butcher's method of killing pigs requires providing a condition for a pig
    public void killPig(Pig pig){
        System.out.println("The butcher executed the method of killing pigs");

        String pigName = pig.getName();
        int pigWeight =pig.getWeight();
        System.out.println(pigName+"Your weight is:"+pigWeight);

``package rely;

public class Farmer {//farmer
    //Farmers raise pigs -- >
    //  Parameter -- > months return value -- > is a pig
    public Pig feedPig(int month){//Raising pigs
        Pig pig = new Pig("floret");//Dependency - > pig objects are used in the butcher's method -- creating objects is not only used in the main function
        pig.growUp(month);// 20-->640
        return pig;

`package rely
public class Test {
    public static void main(String[] args){
        //Create farmer object
        Farmer farmer = new Farmer();
        //A farmer does one thing -- > raising pigs
        Pig pig = farmer.feedPig(5);
        //Create butcher object
        Butcher butcher = new Butcher();
        //Butchers do things -- > kill pigs


Topics: Java Back-end