Interface and abstract class in java

In the book "Java Programming Thoughts", abstract classes are defined as "classes containing abstract methods", but if a class does not contain abstract methods, it is also an abstract class if it is only decorated with abstract.

An abstract class is to 继承exist. For a parent class, if one of its methods is present 父类中实现出来没有任何意义,必须根据子类的实际需求来进行不同的实现, then this method can be declared as an abstract method, and this class becomes an abstract class at this time.

Do abstract classes have to have abstract methods?

    abstract class Cat {
    
    			
       	public static void sayHi() {
    
    
      		  System. out. println("hi~");
        }
        public String test1() {
    
    
    		System. out. println("抽象类不一定非要有抽象方法");
    		return "hello world";
    	}
    }

In the above code, the abstract class does not have abstract methods but can run normally.

What is the difference between a normal class and an abstract class?

Abstract classes cannot be instantiated directly. Abstract classes contain methods that have no concrete implementation, so you cannot create objects with abstract classes. Ordinary classes cannot contain abstract methods, and ordinary classes can be instantiated directly.
Insert picture description here
抽象类中非抽象方法必须实现

Insert picture description here

  1. Abstract classes cannot be instantiated directly, they can only be used for inheritance
  2. After the subclass inherits the abstract class, it must implement all the abstract methods of the parent class. If the derived subclass of the abstract class does not implement all the abstract methods in it, then the derived subclass is still an abstract class and can only be used for inheritance, not instance. Change
  3. An abstract method is defined in a class, then the class must be defined as an abstract class, but an abstract class can have abstract methods or not
  4. 构造方法和静态方法不可以修饰为abstract!!! (Note: static and abstract are definitely not mutually exclusive. Although the two cannot modify a method at the same time, they can modify internal classes at the same time)
  5. The abstract method must be public or protected (because if it is private, it cannot be inherited by the subclass, and the subclass cannot implement the method), and the default is public by default.

What is the difference between abstract class and interface?

Interface refers to a method for people to call or function, it is 行为abstract. (Interface is a variant of abstract class, more abstract than abstract class), interface is decorated with interface key.

[修饰符] [abstract] interface InterfaceName  [extends 父接口1,父接口2]{
    
    	
}

Variables in the interface will be implicitly designated as public static final variables (and only public static final variables, modified with private will report compilation errors), that is 接口中的变量都为常量, generally not defined in the interface. Abstract classes can have their own member variables (like ordinary classes), and their values ​​can be redefined and assigned in subclasses .

Starting from jdk1.8 , the methods in the interface can no longer only have abstract methods (ordinary methods will be implicitly designated as public abstract methods), it can also have static methods and default methods. And static methods and default methods can have method bodies ! The implementation class only needs to implement the abstract methods in the interface.

public interface NewInterface {
    
    
    static void staticMethod() {
    
    
        System.out.println("staticMethod");
    }
 //使用default关键字修饰的方法
    default void defaultMethod() {
    
    
        System.out.println("defaultMethod");
    }
 
    public void getInfo();
}

For static methods, they are directly invoked by the interface name, and do not need to be invoked by objects of the interface implementation class. NewInterface.staticMethod();
For the default method, an instance object is needed to call it. new SimpleImpl().defaultMethod();

 //当一个类实现了多个接口之后,如果多个接口有着相同的default方法,即方法名和参数列表相同
//会无法识别到底是调用的哪个接口的default方法
public class SimpleImpl implements NewInterface {
    
    
    @Override
    public void getInfo() {
    
        
        System.out.println("INFO");
        defaultMethod();
    }    
 //必须要在实现类里面显式重写default的方法
 //在实现类中不需要继续出现default关键字也不能出现default关键字**。
    public void defaultMethod() {
    
    
        System.out.println("Impl default Method");
    }
}

The access permission required for the overridden default method must be public, because the default method can only be modified with public access qualifiers, except that there is no explicit access modifier. We know that in Java, we need to rewrite a method to access The qualifier must be greater than or equal to the access qualifier range specified by the parent class or interface, and the exception thrown at the method declaration must also be greater than the latter . So the access authority must be public.

Finally, when the default method and the method of the parent class inherited by the implementation class have the same name, the method of the parent class is called first

The difference between abstract class and interface

An abstract class is an abstraction of a kind of thing, that is, an abstraction of a class, and an interface is an abstraction of behavior. An abstract class abstracts the entire class as a whole, including attributes and behaviors, but an interface abstracts part of the class (behavior).

Implementation: Subclasses of abstract classes use extends to inherit; interfaces must use implements to implement interfaces.
Constructor: Abstract classes can have constructors; interfaces cannot.
Number of implementations: A class can implement many interfaces; but only one abstract class can be inherited.
Access modifier: The methods in the interface use public modification by default; the methods in the abstract class can be any access modifier.

Design concept: abstract class (is-a relationship) interface (like-a relationship)
inheritance is a relationship of "is it right", while interface implementation is a relationship of "is there or not". If a class inherits an abstract class, the subclass must be the type of abstract class, and the interface implementation is related to whether or not it has the relationship, such as whether a bird can fly (or whether it has the characteristic of flying) and can fly You can implement this interface, but you won’t implement this interface if you can’t fly.

The difference between interface and abstract class
Reference: Interface (jdk1.8) and abstract classes in java

Can abstract classes use final modification?
No, the definition of an abstract class is to let other classes inherit. If it is defined as final, the class cannot be inherited, which will cause conflicts with each other. Therefore, final cannot modify the abstract class. As shown in the figure below, the editor will also prompt an error message:
Insert picture description here

Guess you like

Origin blog.csdn.net/eluanshi12/article/details/96284250