Last time I wrote for you the “inheritance”, inheritance is one of the three object-oriented features is a necessary knowledge of Java learning, if you do not understand thoroughly, please open the following article to watch oh!! Today we’ll talk about the “inheritance” of three object-oriented features.

【 Encapsulation is one of the three main features of Java orientation】

Learning tutorials recommended:

  • 1. Beijing Gaoqi Java300 Set (Java strongly recommended)

    Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners
  • 2.JavaSE Basics – Starting from scratch (recommended)

    Java from beginner to master _/ Java Basics/Arrays /OOP/ Collections /IO streams _
  • 3.Java common class basic practice

    Java common class actual combat basic tutorial _Java eight common class core foundation _Java common class basic /Java wrapper class /String class
  • 4. Basic Mathematics knowledge for Java [Data Structure and Algorithm] (recommended)

    Data structure and algorithm _Java data structure and algorithm foundation to advanced /Java basic introduction to advanced /Java data structure analysis /Java data structure FAQ _ bilibili_bilibili
  • 5.Java object-oriented programming _OOP basic in-depth explanation

    OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP
  • 6.GOF23 kinds of design patterns – detailed explanation of design patterns in 23 courses

    Java GOF23 kinds of design patterns from singleton to memo mode 23 kinds of patterns in detail

inheritance

Inheritance is one of the three characteristics of object-oriented programming, which makes it easier for us to realize the extension of existing classes and realize the modeling of the real world. Hey hey we should eat it today

1. Inheritance and its role

Inheritance makes it easier to extend classes. For example, if we define a human, defining the Boy class simply extends the human. Reuse code without reinvent the wheel.

The extends literally means “to extend.” A subclass is an extension of a parent class. Inheritance is everywhere in the real world. Such as:

In the picture above, mammals inherit from animals. It means that mammals have all the characteristics of animals;

In our programming, if we define a new Student class and find that a Person class already contains the attributes and methods we need, the Student class simply inherits the Person class to own the attributes and methods of the Person class.

Implement inheritance with code as follows:

public class Animal {//extends Object private String color; // color private int age; // age public Animal() {super(); } public Animal(String color, int age) { //alt+insert this.color = color; this.age = age; } public void eat(){ System.out.println("eating .........." ); } public void sleep(){ System.out.println("sleeping............" ); } public void introduce(){ System.out.println(color+" "+age); } } public class Dog extends Animal{ private String nickName; } public Dog(String color,int age){} public Dog(String color,int age,String nickName){// this.color = color; // this.age = age; super(color,age); this.nickName = nickName; } public void guard(){ System.out.println("Dog guarding......" ); } } public class Cat extends Animal { private int eysSight; Public Cat() {super(); } public Cat(String color, int age, int eysSight) {super(color,age); this.eysSight = eysSight; } public void grabMouse(){ System.out.println("Cat grab mouse.........." ); }} public class Test {public static void main(String[] args) {Dog Dog = new Dog(" black ",3," rich "); dog.eat(); // The method inherited from the parent class dog.sleep(); // The method inherited from the parent class dog.introduce(); // The method dog.guard() inherited from the parent class; // subclass-specific method Cat Cat = new Cat(" suit ",3,5); //alt+enter cat.eat(); cat.sleep(); cat.introduce(); cat.grabMouse(); }}Copy the code

Use of “inheritance” :

  1. Superclasses are also called superclasses and base classes. Subclass: derived class, etc.

  2. Java has single inheritance, not multiple inheritance like C++. Multiple inheritance causes confusion, making inheritance chains too complex and systems difficult to maintain.

  3. A subclass inherits its parent class and gets all the attributes and methods of the parent class (except the constructor), but does not necessarily have direct access to them (for example, attributes and methods that are private to the parent class).

  4. If extends is not called when you define a class, its parent is java.lang.object.

.

2. Method rewrite

The parent method, rule (), does not satisfy the needs of the subclass. Object toString() does not satisfy Animal or Dog. This can be done by method override, or method override

Let’s do this in code:

public class Animal {//extends Object protected String color; // color private int age; // age public Animal() {super(); } public Animal(String color, int age) { this.color = color; this.age = age; } public void introduce(){ System.out.println(color+" "+age); } @Override public String toString() { //return super.toString(); return "Animal[color="+color+",age="+age+"]"; } } public class Dog extends Animal{ private String nickName; Public Dog(String color, int age){} public Dog(String color, int age){} public Dog(String color, int age, String nickName){ // this.color = color; // this.age = age; super(color,age); this.nickName = nickName; } public void introduce(){ //this.introduce(); //super.introduce(); System.out.println(color+" "+this.getAge()+" "+nickName); } public void guard(){ //this.guard(); //super.guard(); System.out.println("Dog guarding......" ); } @Override public String toString() { //return super.toString()+" "+nickName; return "Dog[name="+color+",age="+getAge()+ ",nickName="+this.nickName+"]"; }}Copy the code

If you do not understand the partner: I will always show the total score, to a clearer understanding

Total: method overloading and method overwriting (overwriting) are two important concepts in object-oriented, in fact, there is no relationship between these two concepts, but after all are about methods, after all, easy to cause confusion. I’ve made some generalizations about this, and I feel like I can distinguish the two concepts pretty well. I intend to explain from two aspects: general distinction and detailed distinction.

Points: overall difference: the most important difference is that the problem solved is different, that is, the effect is different.

Overloading: the phrase”

Provides multiple implementations of a behavior within a class and improves readability

Rewrite: override

Between a subclass and a parent class, the parent class method cannot meet the requirements of the subclass, and the subclass can meet the requirements through method rewriting

Details: THE declaration of a method from left to right includes permission modifiers, method return values, method names, argument lists, exception types thrown, and so on. Here are the differences:

Total: overload Instances: constructor overload, println() overload

Overriding instances: toString(), equals(), hashCode(), and so on of the Object class can be overridden by subclasses

I don’t know if that makes sense to you

.

Hey, hey, if you are interested, go and try it

The above is all the content of this chapter, I will update the follow-up oh, like the partner support oh ~

Thanks for watching ~