search
HomeJavajavaTutorialRegain the basics of Java (9): Summary of abstract classes and interfaces

Regain the basics of java (9): Summary of abstract classes and interfaces

1. Final keyword

  1. Can modify classes, attributes, methods

2. Modified class: final class final class Pet{}

3. Modified attributes: the value of the attribute cannot be modified final Modified attributes must be manually assigned PI

4. Modification method: This method cannot be overridden 5. All methods in the final class default to final

2. Abstract class

  1. Pet Dog Cat has no realistic meaning

2. The parent class only acts as a code template

3. abstract, abstract, keyword

4. Modified class abstract class Pet{ //Abstract class }

5. Abstract class cannot be instantiated. Instance == object. The process of creating an object is called instantiation

6. After a class becomes an abstract class, what impact will it have on the members in the class? a. Attribute No change b. Construction method No change c. Get/set method No change d. Ordinary functional method No change 7. It is recommended that all parent classes be changed to abstract classes in the future, abstract parent classes

3. Abstract methods

  1. can modify methods, and functional methods in parent classes can generally be changed to abstract methods

2. Characteristics a. Cannot have Method body b. Abstract methods must appear in abstract classes c. Abstract methods must be overridden

3. There can be both ordinary methods and abstract methods in abstract classes

4. It is recommended to change the functional methods in the parent class to abstract methods

5. What keywords cannot the abstract keyword abstract coexist with?

final: If the method is abstracted, it needs to be rewritten, but final cannot be rewritten, so there is a conflict.

private: If the method is private and subclasses cannot inherit it, how to rewrite it?

static: No object is needed. Abstract methods can be called through class names, but calling abstract methods is meaningless.

4. Preliminary understanding of interfaces

  1. In real life, what is an interface? Water pipe interface socket USB interface

2. The function is similar, and it is named "interface"

3. Function a. To ensure scalability and maintainability, the interface It is a form of polymorphism (USB interface) b. Practical application: unified standards and specifications (USB interface)

4. In Java, what exactly is an interface? The essence of the interface is a more special abstract class

5. Define an interface: interface = abstract class

6. The class that inherits the interface is not called a subclass, but is called an implementation class. Replace extends with implements.

7. Compared with abstract classes, interfaces are more special:

a. Interfaces cannot be instantiated and cannot have constructors

b. All attributes in the interface They are all modified by public static final. If you don’t write it, it will be automatically added.

c. All methods in the interface must be abstract methods, and the public abstract keyword will be automatically added.

d. A class can Implement multiple interfaces

e. Multiple inheritance is possible between interfaces

f. The interface does not inherit the Object class

5. Interface is a form of polymorphism

  1. Function: Ensure that the program has good scalability and maintainability

2. Upward transformation of the interface: Interface name Object name = new implementation class();

3. Application scenario:

a. If the passive party needs to use attributes, then inheritance-polymorphism can only be used

b. If The passive party does not need to use attributes, or the focus is on functions (methods), then interfaces should be used. Case: changing to a different encryption algorithm (Company A needs to encrypt data, and initially used the algorithm provided by Company B, but later found that this algorithm is easy was cracked, and then replaced with the encryption algorithm of Company C...)

6. Relationship-Summary

  1. ##Classes and classes Inheritance Single inheritance, multi-layer Inheritance

2. Interfaces and Interfaces Inheritance Multiple Inheritance

3. Classes and Interfaces Implementation Multiple Implementations

4. class A extends B implements C, D

7. Practical application of interface

An interface is a standard or specification that is used to unify the method naming of all parties



The above is the content of Regaining the Basics of Java (9): Summary of Abstract Classes and Interfaces. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool