Differences between abstract classes and interfaces and between abstract classes and interfaces

Posted by razta on Sat, 19 Feb 2022 04:18:41 +0100

Preface:

catalogue

abstract class

1. What is an abstract class

2. What data type is an abstract class?

 3. How are abstract classes defined?

 4. Purpose of abstract classes?

 5. Can a subclass of an abstract class be an abstract class?

 6. Why must an abstract class have a constructor

7. Abstract method

9. Abstract oriented programming!

 

 

abstract class

1. What is an abstract class


There are common features between classes. Abstract classes are formed by extracting these common features. Abstract classes can also extract common features to form abstract classes. Abstract class cannot be instantiated.  

                                                
2. What data type is an abstract class?


Abstract classes are reference data types.


 3. How are abstract classes defined?


Syntax:
Modifier list {abstract} class name {}
         for example:
abstract class} account {} ===== create bank account class


 4. Purpose of abstract classes?

Abstract classes cannot be instantiated and objects cannot be created. In our impression, a class is nothing more than instantiation to call methods and their properties in the class, or to be inherited by subclasses,
Now abstract classes cannot be instantiated, so abstract classes are used to be inherited by subclasses.


 5. Can a subclass of an abstract class be an abstract class?


Yes, subclasses of abstract classes can also be non abstract classes


 6. Why must an abstract class have a constructor

Although an abstract class cannot instantiate an object, it must have a construction method (for subclasses), because if the subclass is a non abstract class, the subclass can instantiate the object. When the subclass instantiates the object,
The parent class must be constructed first, because if there is no parent, there will be no child. There must be a super pointing to the attribute characteristics of the parent type in the memory space of the child class.


7. Abstract method

The non abstract class inherits the abstract class. The abstract method must be implemented | rewritten | overwritten: because the non abstract class inherits the abstract method of the abstract class, and the abstract method only appears in the abstract class. Because the subclass is a non abstract method, the parent class abstract method must be rewritten as a normal method. The implementation of abstract methods in non Abstract subclasses is called the implementation of abstract methods.

/*Subclasses are non abstract and abstract methods must be overridden*/
abstract class A{
    public abstract void move();//There are no abstract methods
}
class b extends A{
    public void move() {
        
    }
}
/*If the subclass is abstract, it doesn't matter. You can override the implementation without rewriting the abstract method.*/
abstract class a{ 
    public abstract void move();
}
abstract class c extends a{
    
}

                      1. Methods without implementation, that is, methods without method body
                      2. Abstract methods belong to abstract classes. Abstract classes do not necessarily have abstract methods. Abstract methods must exist in abstract classes (not in other classes). Abstract classes can have ordinary non abstract methods.

/*Abstract classes can have abstract methods or non abstract methods*/
abstract class A{
	public abstract void move();//Abstract method
	public void fly() {//Non abstract method
		
	}
}
class b extends A{
	public void move() {
		
	}
}
/*The following paragraph will report errors*/
class A{
    abstract void fly();//Abstract methods can only be in abstract classes
}

9. Abstract oriented programming!

Abstract oriented programming should be the implementation of classes in abstract classes, and then use abstract classes to call the methods implemented in non abstract classes. (if you can use polymorphism, use polymorphism. Try to upgrade the program function without changing too many codes)

/*We should face Abstract Programming instead of concrete programming, reduce program coupling and improve program expansion. The idea is in line with the principle of ocp*/
public class Abstractclasstest {
	public static void main(String[] args) {
		animal a = new bird();//Here, instead of creating an object, animal a defines an animal class A.
		a.move();
	}

}
abstract class animal{
	public abstract void move();
}
class bird extends animal{
	public void move() {
		System.out.println("Birds are flying");
	}
}

Interface

1. Basic syntax of interface

1. The interface is also a reference data type, and it is the same after compilation Class file. Interface is a special class, a completely abstract class. When defining, change its name to interface

2. The interface is completely abstract, and the abstract class is semi abstract.

3. How is the interface defined?

Modifier list {interface} interface name {}

4. Interfaces can be inherited. Interfaces support multiple inheritance. One interface inherits multiple interfaces

interface A{
	
}
interface B{
	
}
interface C extends A,B{
	
}

5. The interface only contains two parts, one is constant, the other is abstract method, and the other cannot have. Since there are abstract methods in the interface, you can save public abstract when defining constants for interface methods.

interface math{
	public static final double P = 3.123;//You can save public static final
	int sum(int a,int b);//public abstract is omitted
	int sub(int a,int b);
}

6. All interfaces are public modified

7. Class to class is called inheritance extensions, and class to interface is called implementation implements. When a class wants to implement an interface, it is necessary to implement / rewrite / overwrite the abstract methods in all interfaces in the class. If the implemented class is an abstract class, the abstract method in the interface can not be implemented, because the abstract method only appears in the abstract class. Because the subclass is a non abstract method, the parent class abstract method must be rewritten as an ordinary method.

interface math{
	public static final double P = 3.123;//You can save public static final
	int sum(int a,int b);//public abstract is omitted
	int sub(int a,int b);
}
class mathimp implements math{
	public int sum(int a,int b) {
		return a+b;
	}
	public int sub(int a,int b) {
		return a-b;
	}
}
abstract class mathimps implements math{}

 

 

Topics: Java