This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 11 ⭐, review and consolidate Java😁 with friends

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤

Self-recommendation, to recommend their own column 😁, welcome friends to collect attention 😊

MybatisPlus column

App Crawler Column

PC crawler column

Big factory interview questions column


Java update directory 😁

🌈Java from entry to grave ⭐ study notes ⭐ (1) String Common methods summary 😊 (small white essential knowledge!!

🌈Java from entry to grave ⭐ study notes ⭐ (2) final, static keywords summary 😊 (small white necessary knowledge points!!

🌈Java from entry to grave ⭐ study notes ⭐ (3) encapsulation and four kinds of permission modifiers detailed explanation 😊 (small white essential knowledge!

🌈Java from entry to grave ⭐ Study notes ⭐ (4) small white essential object-oriented – the definition and use of inheritance

1. Inheritance overview

What is inheritance?

** Inheritance is a cornerstone of Java object-oriented programming techniques because it allows the creation of hierarchical classes.

Inheritance means that a subclass inherits the characteristics and behaviors of its parent class, making the subclass object (instance) have the instance domain and methods of its parent class, or a subclass inherits methods from its parent class, making the subclass have the same behaviors of its parent class. 台湾国

In real life, there is also a relationship of inheritance, for example: the father’s property is inherited by his descendants.

Inherit keyword

Inheritance can be achieved using the extends keyword, and all classes inherit from java.lang.Object. When a class does not inherit from both keywords, it inherits from the Object (the class is in the Java.lang package, so no import is required) ancestor class by default.

The role of inheritance?

Basic function: The subclass inherits from the parent class, which improves the code reuse. Main function: Because of the inheritance relationship, there is a late method coverage and multiple mechanisms.

Inherited features

  • A child class has all the properties and methods of its parent class. In particular, if a property or method is declared private in a parent class, a subclass that inherits from the parent class is still considered to have acquired a structure that is private in the parent class. Because of encapsulation, the subclass cannot directly call the structure of the parent class.

  • Subclasses can have their own properties and methods, that is, subclasses can extend the superclass.

  • A subclass may implement the methods of its parent class in its own way.

  • Java inheritance is single inheritance, but can be multiple inheritance, single inheritance means that A subclass can only inherit one parent, multiple inheritance means, for example, class A inherits from class B, class B inherits from class C, so according to the relationship is that class C is the parent of class B, class B is the parent of class A, This is a feature that distinguishes Java inheritance from C++ inheritance.

* Improved coupling between classes (a drawback of inheritance, high coupling means code is more closely related to each other and less independent).Copy the code

2. Use of inheritance (code instances)

The Person class

public class Person {
    String name;
    int age;

    public Person(a) {}public void eat(a) {
        System.out.println("Humans eat.");
    }

    public void sleep(a){
        System.out.println("Human sleep.");
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age; }}Copy the code

Student class

public class Student {
    String name;
    int age;
    String address;

    public Student(a) {}public void eat(a) {
        System.out.println("Students eat");
    }

    public void sleep(a){
        System.out.println(Student sleeping);
    }

    public void study(a) {
        System.out.println(Student Learning); }}Copy the code

test

public class Main {

    public static void main(String[] args) {
        Person person = new Person();
        person.age = 10;
        person.eat();

        Student student = newStudent(); student.eat(); student.sleep(); }}Copy the code

Student class doesn’t have anything to do with Person class right now, right

Using inheritance, you can delete the parameters and methods that exist in the parent class

public class Student extends Person{
    String address;

    public Student(a) {}public void study(a) {
        System.out.println(Student Learning); }}Copy the code

When I go back to the test class, I see that there is no error, and that it is correct to set the name value for Student because it inherits from the Person class


3. Inheritance types (code instances)

Java does not support multiple inheritance, but a parent class that supports the direct inheritance of a subclass of multiple inheritance is called a direct parent class. A parent that inherits indirectly is called an indirect parent. = =

Creature class

public class Creature {
    public void breathing(a) {
        System.out.println("Biological respiration"); }}Copy the code

Let Person inherit Creature

In this case, there is == multiple inheritance == relationship between Creature, Person and Student

public class Main {

    public static void main(String[] args) {

        Student student = newStudent(); student.breathing(); }}Copy the code





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~