Java inheritance (the difference between implements and extends)

implements

The keyword implements is a class, a keyword used to implement an interface, which is used to implement the abstract method defined in the interface. To implement an interface, all methods in the interface must be implemented. Using the implements keyword can make Java have the characteristics of multiple inheritance in disguise . The scope of use is the case of class inheritance interfaces , and multiple interfaces can be inherited at the same time (interfaces and interfaces are separated by commas). 
Note: 
(1) Interfaces can be implemented multiple times (implements), abstract classes can only be inherited by a single (extends) 
(2) Interfaces only have definitions, abstract classes can have definitions and implementations 
(3) The field definition of the interface defaults to: public static final type, and its initial value must be given, Therefore, it cannot be redefined in the implementation class, nor can its value be changed
. (4) Abstract class fields are friendly by default, and their values ​​can be redefined or reassigned in subclasses.
(5) The methods in the interface are public and abstract by default

example:
 

// 定义两个接口Bread、Milk并(implements)单一实现
//定义Bread接口
public interface Bread{
  //定义一个方法吃面包(接口中没有具体的实现方法体)
  public void eatBread();
}

//Bread接口的实现类
public class BreadImpl implements Bread{
   //实现eatBread方法(接口的实现类中需要有具体的方法体)
   public void eatBread(){
     System.out.println("吃面包");
   }
}

public interface Milk{
  //定义一个方法喝牛奶
  public void drinkMilk();
}

//Milk接口的实现类
public class MilkImpl implements Milk{
   //实现drinkMilk方法(接口的实现类中需要有具体的方法体)
   public void drinkMilk(){
     System.out.println("喝牛奶");
   }
}

// 多重实现 依照上面定义的接口进行多重实现
public class Man implements Bread,Milk{
  //实现了Bread接口中eatBread方法
  public void eatBread(){
     System.out.println("吃面包");
   }

//实现了Milk接口中drinkMilk方法
  public void drinkMilk(){
     System.out.println("喝牛奶");
   }
}

The role of the interface:
        The interface is the specification of software programming, which can reduce the coupling. In other words, it can make a certain module or function reusable, so that it only needs to write the code of this function once and it will be ok. To be used in other places, all are implemented by interface calls

Advantages of the interface:

  • The most common advantage of "interface + implementation" is the separation of the implementation class and the interface. When replacing the implementation class, there is no need to replace the interface function.
  • This is also very helpful for unit testing.

extends

The keyword extends indicates the inheritance of the parent class, which can implement the parent class or call the parent class initialization. And it will override the variables or functions defined by the parent class. In Java, class inheritance is single inheritance, that is, a subclass can only have one parent class, so extends can only inherit one class.

// 类的继承格式
class 父类 {
    ...
}

class 子类 extends 父类 {
 ...
}

example:

// 开发动物类,其中动物分别为企鹅以及老鼠,要求如下:
// 企鹅:属性(姓名,id),方法(吃,睡,自我介绍) 
// 老鼠:属性(姓名,id),方法(吃,睡,自我介绍)
//企鹅类
public class Penguin { 
    private String name; 
    private int id; 

    public Penguin(String myName, int  myid) { 
        name = myName; 
        id = myid; 
    } 

    public void eat(){ 
        System.out.println(name+"正在吃"); 
    }

    public void sleep(){
        System.out.println(name+"正在睡");
    }

    public void introduction() { 
        System.out.println("大家好!我是" + id + "号" + name + "."); 
    } 
}

// 老鼠类
public class Mouse { 
    private String name; 
    private int id; 

    public Mouse(String myName, int  myid) { 
        name = myName; 
        id = myid; 
    } 

    public void eat(){ 
        System.out.println(name+"正在吃"); 
    }

    public void sleep(){
        System.out.println(name+"正在睡");
    }

    public void introduction() { 
        System.out.println("大家好!我是" + id + "号" + name + "."); 
    } 
}

From these two pieces of code, it can be seen that the code is repeated, resulting in a large amount of code and bloated, and low maintainability (maintainability is mainly due to the need to modify a lot of code when it needs to be modified later, which is prone to errors). So to fundamentally solve the problem of these two pieces of code, you need to inherit and extract the same parts of the two pieces of code to form a parent class:
 

// 公共父类
public class Animal { 
    private String name;  
    private int id; 

    public Animal(String myName, int myid) { 
        name = myName; 
        id = myid;
    } 

    public void eat(){ 
        System.out.println(name + "正在吃"); 
    }

    public void sleep(){
        System.out.println(name + "正在睡");
    }

    public void introduction() { 
        System.out.println("大家好!我是" + id + "号" + name + "."); 
    } 
}

This Animal class can be used as a parent class, and after the penguin class and mouse class inherit this class, they will have the properties and methods of the parent class, and there will be no duplicate code in the subclass, and the maintainability will be improved, and the code will be more concise , to improve code reusability (reusability is mainly because it can be used multiple times without writing the same code multiple times) Code after inheritance:
 

// 企鹅类
public class Penguin extends Animal { 

    public Penguin(String myName, int myid) { 
        super(myName, myid); 
    } 
}

// 老鼠类
public class Mouse extends Animal { 

    public Mouse(String myName, int myid) { 
        super(myName, myid); 
    } 
}

The keyword super 
is in the inheritance relationship. The member variables, local variables, and methods of the parent class are hidden and often overwritten by the subclass. At this time, the member variables, local variables, and methods of the parent class need to be referenced. 
super.variable name, super.method name, super.constructor of the parent class; 
keyword this
If the member variable of the class has the same name as the local variable, the member variable of the class will be hidden. If you want to use the member variable of the class, you need to use This quotes it. 
this. variable name, this. method name, this. parent class constructor;

Inherited features 
1. The subclass has the non-private properties and methods of the parent class. 
2. Subclasses can have their own attributes and methods, that is, subclasses can extend the parent class. 
3. The subclass can implement the method of the parent class in its own way. 
Java inheritance is single inheritance, but multiple inheritance is possible. Single inheritance means that a subclass can only inherit one parent class. Multiple inheritance means, for example, class A inherits class B, and class B inherits class C. So according to the relationship, class C is B The parent class of the class, class B is the parent class of class A, which is a feature that distinguishes java inheritance from C++ inheritance. 
4. Improve the coupling between classes (the disadvantage of inheritance, high coupling will cause the connection between codes

Guess you like

Origin blog.csdn.net/qq_38889101/article/details/124368125