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

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

王林
王林forward
2023-09-07 09:17:201081browse

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();
   }
}

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete