Abstract class and final class

1. Abstract class

A class with the abstract keyword in its declaration is called an abstract class. An abstract class is located at a higher level of the class and cannot be instantiated, that is, an instance object of an abstract class cannot be created. A class without the abstract keyword modification is called a concrete class, and a concrete class can be instantiated.

In programming, abstract classes are often used to generalize and abstract certain classes, that is, abstract classes define properties and methods common to their subclasses, so as to avoid repeated definitions of subclasses. That is to say, an abstract class is mainly used to define the parent class of several functionally similar classes.

The purpose of defining an abstract class is to lay the groundwork for its subclasses, not as a template for creating objects.

The format of an abstract class declaration statement is as follows:

public abstract class abstract class name {
      class body;
}

Abstract classes have the following characteristics.

(1) You cannot create an instance of an abstract class with new.

(2) Like concrete classes, classes can have member variables and member methods, including constructors, but unlike concrete classes, abstract classes can define abstract methods.

(3) Abstract methods can only appear in abstract classes, but abstract classes can have no abstract methods.

(4) All abstract methods in an abstract class must be implemented in their non-abstract subclasses, otherwise the subclasses must also be declared as abstract classes.

2. Abstract methods

A method declared with the abstract keyword in a member method of a class is called an abstract method. Abstract methods are used to describe the functionality of the system or to standardize certain operations. A method that is not modified by the abstract keyword is called a concrete method, and a concrete method must have a method body.

The format of an abstract method is as follows:

Permission modifier abstract return value type method name (formal parameter list);

E.g:

abstract void eat(); //abstract method

Then declare the eat() method in the class as an abstract method. However, it should be noted that the constructor cannot be declared abstract; abstract and static cannot exist at the same time, that is, there cannot be an abstract static method.

In Java, any class that contains abstract methods must be declared abstract. Because abstract classes contain unimplemented methods, abstract classes cannot be used to define objects directly.

In programming, an abstract class is mainly used to define the parent class of several functionally similar classes.

package page106;
abstract class Animal{
	String str;
	
	Animal(String s){ //Define general method of abstract class
		str = s;
	}
	abstract void eat(); //Define abstract method
}
class Horse extends Animal{
	String str;
	
	Horse(String s){
		super(s); //Call the constructor of the parent class
	}
	void eat() { //Rewrite the abstract method of the parent class
		System.out.println("Horse eats grass!");
	}
}
class Dog extends Animal{
	String str;
	
	Dog(String s){
		super(s);
	}
	void eat() {
		System.out.println("Dogs eat bones!");
	}
}
public class Test {
	public static void main(String[] args) {
		Animal h=new Horse("马");
		Animal d=new Dog("狗");
		h.eat();
		d.eat();
	}
}

The output is:

Horses eat grass!

Dogs eat bones!

The above example illustrates:

<1> In programming, an abstract class must be the parent class of a certain class or certain classes.

<2> Subclasses of several abstract classes must implement some methods with the same name.

3. Final class and final method

A final class refers to a class that cannot be inherited, that is, a final class cannot be used to derive subclasses. In the Java language, if you do not want a class to be inherited, you can declare this class as final. Final classes are specified with the keyword final.

E.g:
public final class FinalClass{
    //...
}

Java stipulates that the methods in the final class are automatically final methods. The Math class is final in Java because the methods inside it cannot be modified.

If it seems unnecessary to create a final class, and you want to protect some methods in the class from being overridden, you can use the keyword final to specify those methods that cannot be overridden by subclasses. These methods are called final methods.

E.g:
public final void fun();
package page107;
class Person{
	String name;
	
	public Person() {
	}
	final String getName() {
		return "person";
	}
}
class Student extends Person{
	public Student() {
	}
	//final String getName(){
	//	return "student";
	//}
	//Override the final method of the parent class, not allowed
}

In program design, the final class can ensure that all methods of some key classes will not be modified due to inadvertently defining subclasses in subsequent program maintenance; the final method can ensure that the key methods of some classes will not be used in future programs. Modified during maintenance due to inadvertently defining subclasses and overriding methods in subclasses.

It should be noted that a class cannot be both final and abstract, that is, the keywords abstract and final cannot be used together. In a class declaration, if the keywords public and abstract (or final) need to appear at the same time, conventionally, public is placed in front of abstract (or final).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481795&siteId=291194637