This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 14 ⭐, review and consolidate Java😁 with friends

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤

Link to details of this article:🌈Java select ✨ Study notes ✨ (7) Small white essential object-oriented – abstract detailed explanation

Self-recommendation, to recommend their own column 😁, welcome friends to collect attention 😊

MybatisPlus column

App Crawler Column

PC crawler column

Big factory interview questions column


Java update directory details at 😁

🌈Java from entry to grave ⭐ study notes ⭐ (1) String Common methods summary 😊 (small white essential knowledge!!

🌈Java from entry to grave ⭐ study notes ⭐ (2) final, static keywords summary 😊 (small white necessary knowledge points!!

🌈Java from entry to grave ⭐ study notes ⭐ (3) encapsulation and four kinds of permission modifiers detailed explanation 😊 (small white essential knowledge!

🌈Java from entry to grave ⭐ Study notes ⭐ (4) small white essential object-oriented – the definition and use of inheritance

🌈Java from entry to grave ✨ Study notes ✨ (5) Small white essential object-oriented – detailed explanation of polymorphism

🌈Java from entry to grave ⭐ study notes ⭐

1. Abstract overview

What is an abstract class?

Classes and classes have common features, these common features are extracted, the formation of abstract class. The class itself does not exist, so abstract classes cannot create objects.

Considerations for abstract classes

  • An abstract class must be a parent class, because it is constantly extracted
  • There are no abstract methods defined in an abstract class. The purpose of the class is not to create objects. Methods can be used directly by subclasses
  • The abstract keyword abstract cannot coexist with the following keywords:
  • privateAbstract: Private methods are not inherited from subclasses, and there is no override. If you use a modified method with a private method, both the abstract method and the private method need to be implemented by the subclass, while the private method does not have a superclass at all
    • Final: Final-decorated classes cannot be inherited, and abstract classes must be superclasses
    • Static: static, which cannot be modified, but can be called directly by the class, whereas abstract, which has no method entities and cannot be called directly

2. Application scenario

Abstract classes are defined using abstract in the Java language

==Abstract== Keyword use

  • Abstract

  • Abstract can be used to modify structures: classes, methods

  • Abstract class: abstract class

    • This class cannot be instantiated
    • An abstract class must have a constructor that can be called when a subclass instantiates
    • In the development, will provide the abstract class subclass, let the subclass object instantiate, complete the related operation.
  • Abstract modification method: Abstract method

    • Abstract methods have only method declarations, not method bodies
    • A class containing abstract methods must be an abstract class. Conversely, an abstract class can have no abstract methods.
    • If a child class overrides all abstract methods in the parent class, the child class can be instantiated
    • If a child class does not override all the abstract methods in its parent class, it is also an abstract class and needs to use the abstract modifier

Before using abstract

public class AbstractTest {

    public static void main(String[] args) {
        Person person = newPerson(); person.eat(); }}class Person{
    String name;
    int age;

    public Person(a) {}public void eat(a) {
        System.out.println("People eat.");
    }

    public void walk(a) {
        System.out.println("Man walks."); }}class Student extends Person{}Copy the code

Next, we abstract the Person class

You can see the error, Person cannot be instantiated, so the abstract class cannot be instantiated.

Abstract the method

public class AbstractTest {

    public static void main(String[] args) {
        Student student = newStudent(); student.eat(); }}abstract class Person{
    String name;
    int age;

    public Person(a) {}// Abstract method
    public abstract void eat(a);

    public void walk(a) {
        System.out.println("Man walks."); }}class Student extends Person{

    @Override
    public void eat(a) {
        System.out.println("Students eat"); }}Copy the code

A child class can instantiate only after it overrides all abstract methods in its parent class


3, abstract class summary

  1. An abstract class cannot be instantiated (a beginner’s mistake). If it is, an error will be reported and the compilation will fail. Only non-abstract subclasses of abstract classes can create objects.
<font size="4"> <font size="4"> <font size="4"> 3. <font size="4"> <font size="4"> <font size="4"> <font size="4"> <font size="4"> <font size="4"> <font size="4"> <font size="4">Copy the code





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~