Regain 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
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
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
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
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
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
- ##Classes and classes Inheritance Single inheritance, multi-layer Inheritance

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
