Similarities and differences between Java abstract classes and interfaces

I abstract class

1. Concept and description of abstract class

In the inheritance hierarchy, each new subclass makes the class more explicit and concrete. If you trace from a subclass to a parent class, the class becomes more generic and ambiguous. The design of a class should ensure that the parent class contains the common characteristics of its children. Sometimes, a parent class is designed so abstract that it has no concrete instance. Such classes are called abstract classes.

Abstract classes are designed for code reuse. When different classes have some of the same behavior (recorded as behavior set A), And some behaviors are implemented in the same way (the non true subset of a, denoted as b), allows these classes to be derived from an abstract class. B is implemented in this abstract class to avoid allowing all subclasses to implement B, which achieves the purpose of code reuse. The part of a minus B is left to each subclass to implement itself. It is precisely because A-B is not implemented here that abstract classes are not allowed to be instantiated (otherwise, it cannot be executed when calling a-b).

Definition format of abstract method: access modifier Abstract return type method name (parameter list);

For example: public abstract void getarea();

2. Instance learning of abstract classes

Example:

package com.wz.abstractdemo;

abstract class A{//Define an abstract class
	
	public void fun(){//Common method
		System.out.println("Method with method body");
	}
	
	public abstract void print();//Abstract method, no method body, decorated with abstract keyword
	
}
//Single inheritance
class B extends A{//Class B is a subclass of an abstract class and a common class

	@Override
	public void print() {//Mandatory override
		System.out.println("Hello World !");
	}
	
}
public class TestDemo {

	public static void main(String[] args) {
		A a = new B();//Upward transformation
		
		a.print();//Methods overridden by subclasses
	}
}

After operation:

Hello World !

It is now clear that:
(1) Abstract classes inherit subclasses with explicit method override requirements, while ordinary classes can selectively decide whether to override;
(2) Abstract classes actually have more abstract methods than ordinary classes, and other components are exactly the same as ordinary classes;
(3) Ordinary class objects can be instantiated directly, but objects of abstract classes can only be obtained after upward transformation.

Although a subclass of a class can inherit any ordinary class, from the actual requirements of development, ordinary classes try not to inherit another ordinary class, but to inherit abstract classes.

II Interface class

1. Concept and description of interface class

An interface is a class like structure used to define common operations for objects. An interface is similar to an abstract class in many ways, but its purpose is to indicate the common behavior of objects of related or unrelated classes. For example, using an appropriate interface, you can indicate that these objects are comparable, edible, or clonable. To distinguish between interfaces and classes, Java uses the following syntax to define interfaces:

modifier interface InterfaceName{
 /**Constant declarations*/
 /**Abatract method signatures */
}

The following is an example of an interface:

public interface Edible{
  /** Describe how to eat */
    public abstract String howToEat();
}

In Java, interface is regarded as a special class. Like regular classes, each interface is compiled into a separate bytecode file. Using interfaces is more or less like using abstract classes. For example, you can use an interface as the data type of a reference variable or the result of type conversion. Like an abstract class, you cannot create an instance of an interface using the new operator.

The design purpose of the interface is to restrict the behavior of the class (more accurately, it is a "yes" constraint, because the interface cannot specify what behavior the class cannot have), that is, it provides a mechanism to force different classes to have the same behavior. It only restricts the existence of the behavior, but does not limit how to implement the behavior.

2. Instance learning of interface class

Example:

package com.wz.interfacedemo;

interface A{//Define an interface A

    public static final String MSG = "hello";//Global constant

    public abstract void print();//Abstract method
}

interface B{//Define an interface B

    public abstract void get();
}

class X implements A,B{//Class X implements two interfaces A and B

    @Override
    public void print() {
        System.out.println("Interface A Abstract method of print()");
    }

    @Override
    public void get() {
        System.out.println("Interface B Abstract method of get()");
    }

}

public class TestDemo {

    public static void main(String[] args){

        X x = new X();//Instantiate subclass objects
        A a = x;//Upward transformation
        B b = x;//Upward transformation

        a.print();
        b.get();
    }

}

After operation:

Interface A Abstract method of print()
Interface B Abstract method of get()

For interfaces, there are only abstract methods and global constants. Therefore, public abstract or public static final can be omitted for simplicity. Moreover, there is only one access right in the interface: public, that is, even if public is not written when defining interface methods and global constants, the final access right is also public. Note that it is not default.

III Similarities and differences between abstract classes and interfaces

1. Abstract classes should be inherited by subclasses, and interfaces should be implemented by classes

2. The interface can only make method declaration, and the abstract class can make method declaration or method implementation.

3. The variables defined in the interface can only be public static constants, and the variables in the abstract class are ordinary variables.

4. Interface is the result of design, and abstract class is the result of reconstruction.

5. Abstract classes and interfaces are used to abstract concrete objects, but the interface has the highest abstraction level.

6. Abstract classes can have concrete methods and properties, and interfaces can only have abstract methods and immutable constants.

7. An abstract class can only inherit one abstract parent class, while an interface can inherit multiple interfaces; A subclass can only inherit one abstract class, but can implement multiple interfaces (in Java, the main function of the interface is to solve the limitation of single inheritance)

 

Keywords: Java Back-end

Added by Cobby on Wed, 15 Dec 2021 11:31:45 +0200