Rich second generation of Java inheritance relationship and his father

First, you need to know some definitions of inheritance. Briefly, but be sure to remember:

  • In the concept of inheritance, there are subclasses and superclasses. The superclass is also called superclass or base class, and the subclass is also called derived class (don't change the word to be silly)
  • Inheritance is for subclass objects to have the same properties and methods (behaviors) as the parent class. Subclasses can also directly the non private (public / protect) properties and methods (behaviors) in the parent class
  • Only extensions can be inherited in a single way, but not in multiple ways (interface implements can be implemented in multiple ways)

As mentioned above, inheritance is for the properties and methods of the parent class, so let's focus on this next. In order to make the article more interesting, I will introduce two concepts of property and money making method in the following examples: property = property and money making method = behavior

Rich father and his rich second generation son

There is such a rich father

/**
 * @author : Wei Ge
 * @description: TODO A rich father with a way to make money
 * @date : 2021/4/21 14:05
 */
public class RichFather {

    //constructor 
    public RichFather() {
        System.out.println("I was born a rich father");
    }
    
    //Dad, I have 10 million in cash
    protected int money = 10000000;

    protected void makemoney(){
        money += 100000;
        System.out.println("This is Dad's way to make money");
    }
}

ok, the rich second generation is here

/**
 * @author : Wei Ge
 * @description: TODO one born with a silver spoon in one's mouth
 * @date : 2021/4/21 14:09
 */
public class RichSecondGeneration extends RichFather{

    //Parameterless constructor
    public RichSecondGeneration() {
        //super() will be added in the first sentence of the subclass's constructor by default, indicating that the constructor of the parent class will be called
        //After the parent class member variable is initialized, it can be used by the child class
        //super(); 
        System.out.println("The rich second generation was born");
        //super(); // It can only be in the first sentence of the code. It can't be put here
    }
    
    //Ten thousand yuan pocket money
    public int money = 10000;
    
    public void consume(int consume_money) {
        System.out.println("I spend money at night" + consume_money + "element");
        money -= consume_money;
        System.out.println("I have money left" + money);
        System.out.println("Dad's money is left" + super.money);
    }
}

There can only be one parent (only extensions can be inherited)

Who can kiss more than one father?

Who was born first (constructor execution order when creating objects)

 public static void main(String[] args) {
     //The rich second generation was born
     RichSecondGeneration richSecondGeneration = new RichSecondGeneration();
 }

Dad wasn't born, son. How did you get out?

I was born a rich father
The rich second generation was born

But if dad was born with money in his mouth (metaphorically, I want to laugh myself), the constructor of rich father class becomes:

//constructor 
public RichFather(int money) {
    System.out.println("Rich dad, I was born with" + money);
}

Then when the rich second generation is born, it is necessary to declare that his father is not a normal way of birth super (nonparametric constructor). Who is born with money

//constructor 
public RichSecondGeneration() {
    super(50);//If the parent class does not have a parameterless constructor, the subclass must have a parameter super declaration when initializing the object
    System.out.println("The rich second generation was born");
}

I was born a rich father with 50 in my mouth
The rich second generation was born

In fact, the above analogy is not very appropriate. There are cases where the parameterless constructor and the parameterless constructor of the parent class exist at the same time. In this case, the super declaration is not required, that is, as long as the parameterless constructor of the parent class exists, the super declaration is not required

Whose money was spent (duplicate name of member variable)

Both father and the second generation of rich sons have money. If they spend money, who will they spend? Measure a wave

//test method 
public static void main(String[] args) {
     RichSecondGeneration richSecondGeneration = new RichSecondGeneration();
     //Rich second generation consumption
     richSecondGeneration.consume(4000);
 }

result:

I spent 10000 yuan on the night show
I have 6000 left
Dad has 10000000 left

this money is spent by the rich second generation. Why, because you didn't take your father's money, son

public void consume() {
    System.out.println("I spend too much at night" + consume_money + ",I can only steal dad's money and spend it");
    super.money -= consume_money;
    System.out.println("I have money left" + money);
    System.out.println("Dad's money is left" + super.money);
}

I spent more than 4000 yuan at night, so I had to steal my father's money to spend it
I have 10000 left
Dad has 9996000 left

How to get the money when it's hidden (how to get the private member variable of the parent class)

His father is wrong. The little boy is too timid. He can't. He has to hide the money

//Dad, I have 10 million in cash, but now the money is going to be hidden
private int money = 10000000;//Set protect to private

At this time, the son wants to steal, but he can't steal. What to do? He thinks of using his mother (the public method of the parent class), so he adds two methods to the RichFather class:

//In dad's world, it's natural for mom to take money
public void consumeMoneyByMonther(int consumeMoney) {
    this.money -= consumeMoney;
    System.out.println("Mom took it" + consumeMoney);
}

//Let mom see how much money dad has left
public int getMoney() {
    return this.money;
}

Turtle son's means are upgraded, and the consumption mode () is updated again:

public void consume(int consume_money) {
    //Upgrade the means and get the money through my mother
    System.out.println("I spend too much at night" + consume_money + ",What if Dad hides his money?");
    consumeMoneyByMonther(consume_money);
    System.out.println("I have money left" + money);
    System.out.println("Dad's money is left" + super.getMoney());
}

I spend more than 4000 yuan at night, and my father's money is hidden. What should I do?
Mom took 4000
I have 10000 left
Dad has 9996000 left

Forget it, I'd better teach my son to make money (member method inheritance)

Dad thinks it's not a way to go on like this. He can only let his son make money (), otherwise he will eat nothing:

//The rich second generation reformed and began to make money
richSecondGeneration.makemoney();
System.out.println("I have money left" + richSecondGeneration.money);
System.out.println("Dad's money is left" + richSecondGeneration.getMoney());

This is Dad's way to make money
I have 10000 left
Dad has 10.1 million left

So after a long time, my son found that he was cheated. Making money () is making money for my father. No, I want to own my father's way of making money (rewrite the parent class method @ Override) and add a new method in the rich second generation RichSecondGeneration class

@Override
public void makemoney (){
    System.out.println("This is the way for the rich second generation to make money");
    this.money += 5000;//I don't earn much every month
}

This is the way for the rich second generation to make money
I have 15000 left
Dad has 10000000 left

And the rich second generation is promising, and can help dad manage and make money by the way

@Override
public void makemoney (){
    System.out.println("This is the way for the rich second generation to make money");
    this.money += 5000;//I don't earn much every month
    super.makemoney();//By the way, help dad manage and make money
}

This is the way for the rich second generation to make money
This is Dad's way to make money
I have 15000 left
Dad has 10.1 million left

Unexpectedly, it can make money in a fancy way (* * overload). A makemoney method is added. The difference is that parameters need to be passed in

public void makemoney(String cannel){
    System.out.println("This is the way for the rich second generation to make money");
    System.out.println("stay" + "Make money through channels");
    this.money += 1000;//Money earned from other channels every month
}
//Fancy money
richSecondGeneration.makemoney("Pinduoduo");

This is the way for the rich second generation to make money
Make money through many channels

The father is kind and the son is filial, and everyone is happy

In addition, there are some norms that are not very metaphorical, which are directly discussed here:

  • When the subclass method overrides the parent method, you must ensure that the subclass Permission > = the parent permission
  • When overriding the method, the return value type of the subclass must be < = the return value type of the parent class

Keywords: Java Class Polymorphism

Added by thewooleymammoth on Sat, 19 Feb 2022 08:37:44 +0200