abstract classes and interfaces

Posted by iRock on Mon, 03 Jan 2022 21:59:39 +0100

abstract class

Significance of abstract classes: Abstract them to improve development efficiency

Abstract: Constraints

  • You can't use the abstract class new, you can only rely on subclasses to implement it: constraints!
  • Ordinary methods can be written in abstract classes
  • Abstract methods must be in abstract classes
package com.Demo05;
//Abstract abstract class
public abstract class Action {

    //Constraint ~ someone helped us achieve it
    //Abstract abstract abstract method has only method name and no method implementation
    public abstract void doSomething();
}
package com.Demo05;

//All methods of an abstract class and subclasses that inherit it must implement its methods, unless the subclass is also an abstract class
public class A extends Action{

    @Override
    public void doSomething() {

    }
}

Attention

  • Abstract method, only method name, no method implementation
  • All methods of an abstract class and subclasses that inherit it must implement its methods, unless the subclass is also an abstract class

Interface

Interface oriented programming

  • Constraints!!
  • Define some methods to be implemented by different people. For example, 10 people implement an interface. There are 10 different implementations
  • The default methods in the interface are public abstract. You can omit "public abstract" when defining abstract methods
  • All properties in the interface are public static final (constants are not defined in general interfaces). Initialization and assignment must be performed when defining constants!! "Public static final" can be omitted when defining constants
  • The interface cannot be instantiated. There is no constructor in the interface
  • implements can implement multiple interfaces
  • Methods in interfaces must be overridden

Code: (see more comments)

package com.Demo06;

//The keyword interface defined by the interface requires an implementation class
public interface UserService {

    //"public static final" can be omitted when defining constants
    public static final int AGE = 99;
    String NAME = "Shi GANGLONG";


    //All defined methods in the interface are abstract. "public abstract" can be omitted when defining abstract methods
    public abstract void run();// public abstract is grayed out to prove that the system will automatically define it without writing
    void add();
    void delete();
    void update();
    void query();
}

package com.Demo06;

//Class can implement the implements interface
//If you implement the class of the interface, you need to rewrite the methods in the interface
//Using interface to realize multi inheritance
public class UserServiceImpl implements UserService,TimeService{

    @Override
    public void run() {

    }

    @Override
    public void add() {

    }
    @Override
    public void delete() {

    }
    @Override
    public void update() {

    }
    @Override
    public void query() {

    }

    @Override
    public void timer() {

    }
}
package com.Demo06;

public interface TimeService {
    void timer();

}

Attention

  • The keyword interface defined by the interface requires an implementation class
  • All defined methods in the interface are abstract
  • Class can implement the implements interface; If you implement the class of the interface, you need to rewrite the methods in the interface; Using interface to realize multi inheritance

It is summarized in the following book, including abstract classes and interfaces

abstract class

  • Abstract methods must be decorated with the abstract keyword, and there is no need to implement the method body when defining the method
  • When a class contains abstract methods, the class must be abstract

Basic syntax of abstract class and abstract method definitions:

//Define abstract classes
[Modifier ] abstract class Class name {
    //Define abstract methods
    [Modifier ] abstract Method return value type method name ([parameter list]);
    //Other methods or properties
}

be careful:

  • An abstract class must be defined to contain abstract methods. An abstract class may not contain any abstract methods
  • Abstract classes cannot be instantiated (because abstract classes may contain abstract methods, which have no method body and cannot be called)
  • For the abstract method defined in the abstract class, you need to create a subclass and implement the abstract method of the abstract class in the subclass
  • To define an abstract method, you only need to add the abstract keyword to the ordinary method, and there are no braces and method body

Test code:

package com.Demo05;

//Abstract class Action
public abstract class Action{
    //Define abstract methods
    public abstract void shout();
}

//Define Dog class to inherit abstract class Action
class Dog extends Action {
    //Implement abstract method shot ()

    @Override
    public void shout() {
        System.out.println("Woof, woof.....");
    }
}

//Define test class
class Application {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.shout();
    }
}

Interface

When defining an interface, you cannot use the class keyword, but the interface keyword

Syntax format of interface definition:

[Modifier ] interface Interface name [extends Parent interface 1,...] {
    [public] [static] [final] Constant type constant name = constant value;
    [public] [abstract] Method return value type method name ([parameter list]);
    [public] default Method return value type method name ([parameter list]){
        //Method body
    }
    [public] static Method return value type method name ([parameter list]){
        //Method body
    }
}

Note:

  • extends parent interface 1,..., which means that multiple parent interfaces can be inherited at the same time when defining an interface
  • The initial value must be assigned when defining constants. The default method and static method can have method bodies

Syntax for defining interface implementation:

[Modifier ] class Class name [extends Parent class name] [implements Interface 1,...]{
    //
}
  • Interfaces can contain three types of methods: abstract methods, default methods, and static methods
    • Static methods can be implemented through "interface name. Method name"
package com.Demo05;

//Define Action interface
public interface Action{
    //Define abstract methods
    public abstract void shout();

    //Define global constants
    int ID = 1;

    //Define default method
    default void getType(String type){
        System.out.println("The animal belongs to"+type);
    }

    //Define static methods
    static int getId(){
        return Action.ID;
    }
}



//The Dog class implements the Action interface
class Dog implements Action {
    //Implement abstract method shot ()

    @Override
    public void shout() {
        System.out.println("Woof, woof.....");
    }

}

//Define test class
class Application {
    public static void main(String[] args) {
        System.out.println(Action.getId());  //Call the static method action through the interface name getId()
        Dog dog = new Dog();
        System.out.println(dog.ID);      //Get global variables in the interface
        dog.shout();          //Call the shot() method of dog
        dog.getType("Canidae");   //The instantiated object of the Dog class is implemented through the interface, and the default method of the interface is called
    }
}

Interfaces and interfaces can also be inheritance relationships

  • Also use the extends keyword to implement
  • The LandAction interface inherits the Action interface. When the Dog class implements the LandAction interface, it needs to implement two abstract methods
package com.Demo05;

//Define Action interface
public interface Action{
    //Define abstract methods
    public abstract void shout();

    //Define global constants
    int ID = 1;

    //Define default method
    default void getType(String type){
        System.out.println("The animal belongs to"+type);
    }

    //Define static methods
    static int getId(){
        return Action.ID;
    }
}
//Defines the LandAcion interface and inherits the Action interface
interface LandAction extends Action{
    //Call abstract method
    void run();
}


//The Dog class implements the LandAction interface
class Dog implements LandAction {
    //Implement abstract method shot ()
    @Override
    public void shout() {
        System.out.println("Woof, woof.....");
    }
    //Implement abstract method run()


    @Override
    public void run() {
        System.out.println("The dog is running");
    }
}

//Define test class
class Application {
    public static void main(String[] args) {
        System.out.println(Action.getId());  //Call the static method action through the interface name getId()
        Dog dog = new Dog();
        System.out.println(dog.ID);      //Get global variables in the interface
        dog.shout();          //Call the shot() method of dog
        dog.getType("Canidae");   //The instantiated object of the Dog class is implemented through the interface, and the default method of the interface is called
        dog.run();   //Call the dog object to implement the run() method
    }
}

Interface summary:

  • The methods in the interface are abstract and do not contain the method body. To call an abstract method, the implementation method must be called through the implementation class of the interface
  • When a class implements an interface, if the class is an abstract class, it only needs to implement some abstract methods in the interface, otherwise it needs to implement all abstract methods in the interface
  • A class can implement multiple interfaces at the same time through the implements keyword
  • Interfaces can be inherited through the extends keyword. An interface can inherit multiple interfaces at the same time

Topics: Java interface abstract class