Java foundation (understanding of abstract classes and interfaces)

1. Characteristics of abstract classes and interfaces.

Abstract class: Define the common function and inherit the abstract class, then all the abstract methods in the abstract class must be implemented.

  • Both abstract classes and abstract methods need to be modified by abstract. Abstract methods must be defined in abstract classes
  • Abstract classes cannot directly create objects (reason: calling abstract methods makes no sense)
  • Only after covering all the abstract methods in the abstract class, its subclasses can create objects. Otherwise, the subclass must also be an abstract class.

Interface: for scalability. (Who needs this function, give it to anyone)

  • Contains special classes for function declarations.
  • Definition format: public interface interface name {} Use format: class A implements interface name {}
  • Member variables can be defined in the interface, but the variables can only be final variables and are static. Default modifier: public static final.
  • Methods can be defined in the interface, but only abstract methods.
  • There is no constructor in the interface, and it is not possible to create pairs of objects.
  • Subclasses must override all abstract methods in the interface before they can be instantiated.

2. Details

Abstract class

  • The abstract class must be the parent class
  • Abstract methods can not be defined in abstract classes (meaning: static methods can be directly used by subclasses)
  • Keywords cannot be used with private and final. (Final declared classes are not allowed to have subclasses, private ones are not allowed to be overwritten)

interface

  • Multiple inheritance can be used between multiple interfaces for multiple inheritance
  • Class can implement multiple interfaces

Code example:

package day0526;

/**
 * 1、类可以实现多个接口
 * 必须实现接口的所有抽象方法
 * 注:可以实现部分方法,但该类必须是抽象类
 * 2、接口可以实现多继承
 */
//多实现
public class demo1 implements Inter1,Inter {
    public void show(){

    }
}
//多继承
interface Inter extends Inter1,Inter2{
    //定义变量默认修饰符public static final
    int a=10;
    //定义方法默认修饰符public abstract
    void show();
    //接口中不允许有构造方法
}
interface Inter1{

}
interface Inter2{

}

The choice of the two

  • Choose the interface first and use abstract classes as little as possible
  • You need to define the behavior of the subclass, and you need to provide the common function for the subclass before choosing the abstract class.

3. Case:

To achieve this, we first analyze it.

Then draw a picture to analyze the relationship between them, the connection between each object, and their properties and methods.

After analysis, use code to achieve.

// Define the interface

interface Learnenglish{

public abstract void learnspeaking();

}

// Public abstract class (person)

abstract class Person1{

private String name;

private int age;

public Person1(String name,int age) {

this.name=name;

this.age=age;

}

public void Print() {

System.out.println(name+"---"+age);

}

public abstract void eat() ;

public abstract void sleep();

}

// Abstract class (athlete)

abstract class Athlete extends Person1{

public Athlete(String name,int age) {

super(name,age);

}

public void eat() {

System.out.println ("Eat healthy food.");

}

public void sleep() {

System.out.println ("Early to bed and early to rise");

}

public abstract void learn();

}

// Abstract class (coach)

abstract class Coach extends Person1{

public Coach(String name,int age) {

super(name,age);

}

public void eat() {

System.out.println ("Reasonable Meal, Keep Your Body");

}

public void sleep() {

System.out.println ("Law of rest");

}

public abstract void teach();

}

// Specific class (athlete)

class PingPong extends Athlete implements Learnenglish{

public PingPong(String name,int age) {

super(name,age);

}

public void learn() {

System.out.println ("Learn Table Tennis");

}

public void learnspeaking() {

System.out.println ("Learn English!");

}

}

class Basketball extends Athlete{

public Basketball(String name,int age) {

super(name,age);

}

public void learn() {

System.out.println ("Learning Basketball");

}

}

// Specific class (coach)

class PingCoach extends Coach implements Learnenglish{

public PingCoach(String name,int age) {

super(name,age);

}

public void teach() {

System.out.println ("Ping Pong Teaching");

}

public void learnspeaking() {

System.out.println ("Learn English!");

}

}

class BasketCoach extends Coach{

public BasketCoach(String name,int age) {

super(name,age);

}

public void teach() {

System.out.println ("Basketball Teaching");

}

}

// Test class

public class Myproject1 {

      public static void main(String[]args) {

      BasketCoach a=new BasketCoach ("yyy",20);

      a.Print();

      a.eat();

      a.sleep();

      a.teach();

      }

}

A test class for a basketball coach:

operation result:

A test class for table tennis players:

operation result:

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/90545145