Java study notes-abstract classes and interfaces

Abstract class

1. What is an abstract class?

  • Classes and classes have common features. Extracting these common features forms an abstract class.
  • The class itself does not exist, so abstract classes cannot create objects (cannot be instantiated)

2. What type of abstract class is it?

  • Abstract class is a reference type

3. How to define abstract class?

  • [Modifier list] abstract class class name {}

4. Abstract classes cannot be instantiated and objects cannot be created, so abstract classes are used to be inherited by subclasses.
5. final and abstract cannot be used together
. 6. The subclass of the abstract class can be the abstract class
7. Although the abstract class cannot be instantiated, it has a constructor , but it is for the subclass (super ())
8. The abstract class Related to a concept: abstract methods

  • Abstract methods represent methods that are not implemented, and methods that do not have a method body. Such as:public abstract void doSome();
  • Features: There is no method body, which ends with a semicolon. There is an abstract keyword in the preceding modifier list

9. There is not necessarily an abstract method in the abstract class, but the abstract method must appear in the abstract class.
10. The abstract class is semi-abstract. In the abstract class, you can write abstract methods or ordinary methods.

10. Important conclusion: ***** five stars

  • A non-abstract class inherits the abstract class, and the abstract methods in the abstract class must be implemented
  • Overwriting or rewriting here can also be called implementation. (Realization of abstraction)
    • Reason: If you do not overwrite the method, then it is equivalent to the abstract method appears in the ordinary class, is absolutely not allowed.

Abstract class has a constructor

public class AbstractTest{
	public static void main(String[] args){
		Cat c = new Cat();
	}
}
abstract class Aniaml{
	//有参数构造方法
	public Aniaml(int a){	
	}
}
class Cat extends Aniaml{
}

Abstract methods can only appear in abstract classes

public class AbstractTest{
	public static void main(String[] args){		
	}
}
abstract class Aniaml{
	//抽象方法
	public abstract void move();
}

class Cat extends Aniaml{
	//如果不进行方法覆盖
	//那么就相当于抽象方法出现在了普通类中,是绝对不允许的
	//public void move(){}
}

Guess you like

Origin www.cnblogs.com/zy200128/p/12716473.html