What is an abstract class in java? abstract class declaration
The content of this article is to introduce abstract classes in Java, so that everyone can have a simple understanding of abstract classes, know what abstract classes are and how to declare them. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Before we introduce abstract classes, let’s first understandWhat is abstraction in Java?
Abstraction in java is the process of hiding implementation details and showing only functionality to the user.
The abstraction only shows the basic content to the user and hides the internal details, for example, send an SMS, enter text in it and send the message; but we do not know what is the internal processing of messaging.
Abstraction allows you to focus on what the object does, rather than how it is done.
So how to achieve abstraction?
There are two ways to implement abstraction in java
1. Abstract class (0 to 100% implementation of abstraction)
2. Interface (100% Implementing abstraction)
Let’s introduceWhat is an abstract class in Java?
A class declared as abstract in java is called an abstract class. It can have abstract and non-abstract methods, needs to be extended and implemented, but cannot be instantiated.
Points to remember about Java abstract classes:
1. Abstract classes must be declared using the abstract keyword.
2. It can have abstract and non-abstract methods.
3. It cannot be instantiated.
4. It can also have constructors and static methods.
5. It can have a final method, forcing subclasses not to change the body of the method.
Abstract class example:
abstract class A{}What are abstract methods in Java
Methods declared as abstract and not implemented Called abstract method.
Example of abstract method
abstract void printStatus(); //没有方法体和抽象
Example of abstract class
Abstract with abstract method Example of class
In this example, Bike is an abstract class and contains only one abstract method. Its implementation is provided by Honda class.
abstract class Bike{
abstract void run();
}
class Honda extends Bike{
void run(){
System.out.println("安全运行..");
}
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
}
}Run result:

Abstract class with constructor, data members and methods
Abstract classes can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
//具有方法体的抽象类的示例
abstract class Bike{
Bike(){
System.out.println("自行车制造");
}
abstract void run();
void changeGear(){
System.out.println("齿轮更换");
}
}
class Honda extends Bike{
void run(){
System.out.println("安全运行..");
}
}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}Running result:

## Note:
The above is the detailed content of What is an abstract class in java? abstract class declaration. For more information, please follow other related articles on the PHP Chinese website!
Java Application Performance Monitoring (APM) ToolsJul 24, 2025 am 03:37 AMCommon JavaAPM tools include NewRelic, DatadogAPM, AppDynamics, SkyWalking, Pinpoint and Prometheus Grafana Micrometer combinations; whether APM is required depends on system lag, complex microservice calls, performance details and optimization requirements; APM should consider deployment methods, learning costs, performance impact, cost and integration capabilities; when using them, you should pay attention to reasonable configuration, sampling rate, alarm rules, and analyze the root cause in combination with code.
Building Reactive Java Applications with RxJavaJul 24, 2025 am 03:35 AM1.RxJava is a responsive framework based on observer pattern and functional programming, suitable for handling asynchronous and non-blocking tasks. 2. Core types include Observable, Flowable, Single, etc., which are used to represent different forms of data flow. 3. Data conversion and combination are performed through operators such as map, filter, and flatMap to simplify complex logic. 4. Use Schedulers.io(), Schedulers.computation(), AndroidSchedulers.mainThread() and other schedulers to control thread switching. 5. Specify the data flow start thread through subscribeOn, obse
Implementing a Thread-Safe Singleton in JavaJul 24, 2025 am 03:35 AMWhen using double check lock to implement lazy loading singletons, the volatile keyword is required to ensure thread visibility and prevent instruction re-arrangement; 2. It is recommended to use static internal classes (BillPugh scheme) to implement thread-safe lazy loading singletons, because the JVM ensures thread safety and no synchronization overhead; 3. If you do not need lazy loading, you can use static constants to implement simple and efficient singletons; 4. When serialization is involved, enumeration method should be used, because it can naturally prevent multiple instance problems caused by reflection and serialization; in summary, general scenarios prefer static internal classes, and serialized scenarios to select enumerations. Both have the advantages of thread safety, high performance and concise code.
Comparing Java, Kotlin, and Scala for Backend DevelopmentJul 24, 2025 am 03:33 AMKotlinoffersthebestbalanceofbrevityandreadability,Javaisverbosebutpredictable,andScalaisexpressivebutcomplex.2.Scalaexcelsinfunctionalprogrammingwithfullsupportforimmutabilityandadvancedconstructs,KotlinprovidespracticalfunctionalfeatureswithinanOOPf
Managing Dependencies in a Large-Scale Java ProjectJul 24, 2025 am 03:27 AMUseMavenorGradleconsistentlywithcentralizedversionmanagementandBOMsforcompatibility.2.Inspectandexcludetransitivedependenciestopreventconflictsandvulnerabilities.3.EnforceversionconsistencyusingtoolslikeMavenEnforcerPluginandautomateupdateswithDepend
Mastering Java 8 Streams and LambdasJul 24, 2025 am 03:26 AMThe two core features of Java8 are Lambda expressions and StreamsAPI, which make the code more concise and support functional programming. 1. Lambda expressions are used to simplify the implementation of functional interfaces. The syntax is (parameters)->expression or (parameters)->{statements;}, for example (a,b)->a.getAge()-b.getAge() instead of anonymous internal classes; references such as System.out::println can further simplify the code. 2.StreamsAPI provides a declarative data processing pipeline, the basic process is: create Strea
Java Security Tokenization and EncryptionJul 24, 2025 am 03:24 AMSecurityToken is used in Java applications for authentication and authorization, encapsulating user information through Tokenization to achieve stateless authentication. 1. Use the jjwt library to generate JWT, select the HS256 or RS256 signature algorithm and set the expiration time; 2. Token is used for authentication, Encryption is used for data protection, sensitive data should be encrypted using AES or RSA, and passwords should be stored with hash salt; 3. Security precautions include avoiding none signatures, setting the expiration time of tokens, using HTTPS and HttpOnlyCookies to store tokens; 4. In actual development, it is recommended to combine SpringSecurity and
The role of `var` for Local-Variable Type Inference in JavaJul 24, 2025 am 03:23 AMvar was introduced in Java 10 for local variable type inference, to determine the type during compilation, and maintain static type safety; 2. It can only be used for local variables in methods with initialized expressions, and cannot be used for fields, parameters or return types; 3. Non-initialization, null initialization and lambda expression initialization are prohibited; 4. It is recommended to use them when the type is obvious to improve simplicity and avoid scenarios that reduce readability. For example, types should be explicitly declared when complex methods are called.


Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

SublimeText3 Chinese version
Chinese version, very easy to use

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),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.







