Summary of JAVA inheritance, abstraction and interface

01 inheritance

1. Sample code

public class Account {
    
    

  public String username = "小程";
  private String password = "123";

  //Account的构造方法
  public Account(){
    
    
    System.out.println("Account的构造方法");
  }

  //Account的普通方法
  public void hello(){
    
    
    System.out.println("欢迎登陆");
  }
}
class MyAccount extends Account {
    
    

  //MyAccount的构造方法
  public MyAccount(){
    
    
    System.out.println("MyAccount的构造方法");
  }

  public static void main(String[] args) {
    
    
    MyAccount myAccount =  new MyAccount();
    myAccount.hello();
    System.out.println(myAccount.username);
    //System.out.println(myAccount.password); //报错:不能访问父类private成员变量
  }
}

2. Running results

Account的构造方法
MyAccount的构造方法
欢迎登陆
小程

3. Summary

1. The subclass has the non-private properties and methods of the parent class .
2. Subclasses can have their own properties 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 .
4. 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 it is class C according to the relationship It is the parent class of class B, and class B is the parent class of class A. This is a feature that distinguishes java inheritance from C++ inheritance.
5. Improve the coupling between classes (the disadvantage of inheritance, high coupling will cause the connection between codes).

02 Abstract

1. Sample code

//抽象类
public abstract class Fruits {
    
    
  String name= "水果";

  //Fruits的构造函数
  public Fruits() {
    
    
    System.out.println("Fruits的构造函数");
  }

  //普通方法
  public void buy(){
    
    
    System.out.println("买水果吃~~");
  }

  //抽象方法
  public abstract void taste();
}
//继承抽象类
public class Peach extends Fruits {
    
    
  //Test的构造函数
  public Peach() {
    
    
    System.out.println("Peach的构造函数");
  }

  //实现抽象方法
  @Override
  public void taste() {
    
    
    System.out.println("桃子尝起来是甜的");
  }

  public static void main(String[] args) {
    
    
    Peach peach =  new Peach();
    peach.buy();
    peach.taste();
    System.out.println(peach.name);
  }
}

2. Execution result

Fruits的构造函数
Peach的构造函数
买水果吃~~
桃子尝起来是甜的
水果

3. Summary

1. The abstract class cannot be instantiated . If it is instantiated, an error will be reported and the compilation will not pass. Only non-abstract subclasses of abstract classes can create objects .
2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes .
3. Construction methods and class methods (methods modified with static) cannot be declared as abstract methods.
5. The subclass of the abstract class must give the concrete implementation of the abstract method in the abstract class , unless the subclass is also an abstract class.
5. Because it cannot be instantiated, it can only be used by inheritance, and a subclass can only inherit one abstract class .

03 interface

1. Sample code

public class Watermelom extends Fruits implements SellNum, RemainNum {
    
    
  //Watermelom的构造函数
  public Watermelom() {
    
    
    System.out.println("Watermelom的构造函数");
  }

  //实现抽象方法
  @Override
  public void taste(){
    
    
    System.out.println("西瓜实现继承抽象类的抽象方法");
  }

  public static void main(String[] args) {
    
    
    Watermelom watermelom =  new Watermelom();
    watermelom.buy();
    watermelom.taste();
    watermelom.haveAmount();
    watermelom.selloutAmount();
    System.out.println(watermelom.name);
  }


  //implements必须实现接口
  @Override
  public void haveAmount() {
    
    
    System.out.println("实现了RemainNum接口的抽象方法haveAmount");

  }

  @Override
  public void selloutAmount() {
    
    
    System.out.println("实现了selloutAmount接口的抽象方法selloutAmount");

  }
}
public interface SellNum {
    
    
  //接口中变量会被默认指定为 public static final (并且只能是 public)。
  public static final int sellNumber =3;
  //或者这样写int sellnum = 1;一样的,会被默认指定。

  //接口中方法会被默认指定为 public abstract(只能是public abstract)。
  public abstract void selloutAmount();
  //或者这样写一样的void sellFruits();会被默认指定。
}

public interface RemainNum {
    
    
  // 接口中变量会被默认指定为 public static final (并且只能是 public)。
  public static final int remainNumber = 1;
  // 或者这样写int remainNumber =7;一样的,会被默认指定。

  // 接口中方法会被默认指定为 public abstract(只能是public abstract)。
  public abstract void haveAmount();
  // 或者这样写一样的void haveAmount();会被默认指定。
}

2. Execution result

Fruits的构造函数
Watermelom的构造函数
买水果吃~~
西瓜实现继承抽象类的抽象方法
实现了RemainNum接口的抽象方法haveAmount
实现了selloutAmount接口的抽象方法selloutAmount
水果

3. Summary

1. Interfaces cannot be used to instantiate objects .
2. The interface has no constructor .
3. All methods in the interface must be abstract methods .
4. Interfaces cannot contain member variables , except static and final variables.
5. The interface is not inherited by the class, but is implemented by the class , and multiple implementations are separated by commas.
6. The interface supports multiple inheritance .

4. Interface characteristics

1. Each method in the interface is also implicitly abstract , and the method in the interface will be implicitly designated as public abstract (only public
abstract, other modifiers will report an error).
2. The interface can contain variables , but the variables in the interface will be implicitly designated as public static final variables (and can only be public, and
a compilation error will be reported if modified with private).
3. The methods in the interface cannot be implemented in the interface , and the methods in the interface can only be implemented by the class that implements the interface.

04 The difference between abstract class and interface

1. The method in the abstract class can have a method body , that is, the specific function of the method can be realized, but the method in the interface cannot.
2. Member variables in an abstract class can be of various types , while member variables in an interface can only be of public static final type .
3. Interfaces cannot contain static code blocks and static methods (methods modified with static), while abstract classes can have static code blocks and static methods .
4. A class can only inherit one abstract class , but a class can implement multiple interfaces .

Guess you like

Origin blog.csdn.net/qq_43010602/article/details/122160494