Home  >  Article  >  Java  >  What is an abstract class in java? abstract class declaration

What is an abstract class in java? abstract class declaration

青灯夜游
青灯夜游Original
2018-11-24 17:49:117074browse

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:

What is an abstract class in java? abstract class declaration

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:

What is an abstract class in java? abstract class declaration


## Note:

1. If there is an abstract method in a class, the class must be abstract.

2. If you extend an abstract class with abstract methods, you must provide the implementation of the method or make the class abstract.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

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!

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