Home > Java > javaTutorial > body text

In Java, can we define an abstract class without abstract methods?

王林
Release: 2023-09-07 09:17:20
forward
1054 people have browsed it

In Java, can we define an abstract class without abstract methods?

Yes, we can declare an abstract class without abstract methods in Java.

  • Abstract class means a function definition that hides the implementation and displays it to the user.
  • An abstract classs has both abstract methods and non-abstract methods.
  • For abstract class, we cannot create objects directly. But we can create objects indirectly using subclass objects.
  • Java abstract classes can have instance methods that implement default behavior.
  • Java abstract classes can have instance methods that implement default behavior. >Abstract classOnly one class or one abstract class can be extended at a time.
  • Declaring a class as abstractwith no abstract methods means we are not allowed
  • Abstract classes used in Javameans we cannot directly Create an object of this class.

Example

abstract class AbstractDemo { // Abstract class
   private int i = 0;
   public void display() { // non-abstract method
      System.out.print("Welcome to Tutorials Point");
   }
}
public class InheritedClassDemo extends AbstractDemo {
   public static void main(String args[]) {
      AbstractDemo demo = new InheritedClassDemo();
      demo.display();
   }
}
Copy after login

In the above example, we did not define the abstract method in the AbstractDemo class. The compiler will not throw any compile-time errors.

Output

Welcome to Tutorials Point
Copy after login

The above is the detailed content of In Java, can we define an abstract class without abstract methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template