Home Java javaTutorial Abstract classes in Java

Abstract classes in Java

Sep 22, 2023 am 11:53 AM
inheritance implementation abstract class

Abstract classes in Java

A class that contains the abstract keyword in its declaration is called an abstract class.

  • Abstract classes may or may not contain abstract methods, that is, methods without a body (public void get(); )
  • However, if a class has at least one abstract method, Then the class must be declared abstract.
  • If a class is declared abstract, it cannot be instantiated.
  • To use an abstract class, you must inherit it from another class and provide implementations of the abstract methods in it.
  • If you inherit an abstract class, you have to provide implementations for all the abstract methods in it.

Examples

This section provides you with examples of abstract classes. To create an abstract class, just use the abstract keyword before the class keyword in the class declaration.

/* File name : Employee.java */
public abstract class Employee {
   private String name; private String address; private int number;
   public Employee(String name, String address, int number) {
      System.out.println("Constructing an Employee");
      this.name = name; this.address = address;
      this.number = number;
   }
   public double computePay() {
      System.out.println("Inside Employee computePay"); return 0.0;
   }
   public void mailCheck() {
      System.out.println("Mailing a check to " + this.name + " " + this.address);
   }
   public String toString() {
      return name + " " + address + " " + number;
   }
   public String getName() {
      return name;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String newAddress) {
      address = newAddress;
   }
   public int getNumber() {
      return number;
   }
}
Copy after login

You can observe that except for the abstract methods, the Employee class is the same as a normal class in Java. The class is now abstract, but it still has three fields, seven methods, and a constructor.

Now you can try to instantiate the Employee class in the following way -

/* File name : AbstractDemo.java */
public class AbstractDemo {
   public static void main(String [] args) {
      /* Following is not allowed and would raise error */
      Employee e = new Employee("George W.", "Houston, TX", 43);
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
    }
 }
Copy after login

When you compile the above class, it will give the following error -

Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43); ^ 1 error  
Copy after login

The above is the detailed content of Abstract classes in Java. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the code hierarchical relationship problem in C++ development How to solve the code hierarchical relationship problem in C++ development Aug 22, 2023 am 11:22 AM

How to solve the code hierarchical relationship problem in C++ development. When developing complex C++ programs, a common problem is the management of code hierarchical relationships. Incorrect hierarchies can make code difficult to read, maintain, and extend. In order to solve this problem, we can adopt the following strategies. First, we can use a suitable directory structure to organize the code files. A good directory structure can arrange code files in a more orderly manner, making it easier for us to quickly locate or modify related code during the development process. Generally, it is recommended to

Solutions to common code reuse problems in C++ Solutions to common code reuse problems in C++ Oct 09, 2023 pm 01:50 PM

Solutions to common code reuse problems in C++ In C++ programming, code reuse is an important technology that can improve development efficiency and code maintainability. However, we often encounter some common code reuse problems, such as repeated code fragments, complex inheritance relationships, etc. This article will introduce several common methods to solve these problems and provide specific code examples. Function encapsulation Function encapsulation is a common code reuse method. By encapsulating a piece of code into a function, it can be called multiple times in other places to avoid writing the same code repeatedly.

Abstract classes in Java Abstract classes in Java Sep 22, 2023 am 11:53 AM

A class that contains the abstract keyword in its declaration is called an abstract class. An abstract class may or may not contain abstract methods, i.e. methods without a body (publicvoidget();) However, if a class has at least one abstract method, the class must be declared abstract. If a class is declared abstract, it cannot be instantiated. To use an abstract class, you must inherit it from another class and provide implementations of the abstract methods in it. If you inherit from an abstract class, you provide implementations for all abstract methods in it. Examples This section provides you with examples of abstract classes. To create an abstract class, just use the abstract keyword before the class keyword in the class declaration. /*Filename:Emplo

How to do object-oriented programming in C++? How to do object-oriented programming in C++? Aug 27, 2023 am 08:34 AM

How to do object-oriented programming in C++? Object-Oriented Programming (OOP) is a very common and important software development paradigm. C++ is a multi-paradigm programming language that includes support for object-oriented programming. In C++, through the concepts of class and object, we can easily implement object-oriented programming. First, we need to define a class. Class is a custom

How to use abstract classes to build the infrastructure of a PHP application How to use abstract classes to build the infrastructure of a PHP application Aug 02, 2023 pm 03:09 PM

How to use abstract classes to build the infrastructure of PHP applications Summary: In PHP application development, abstract classes are a very important tool. This article will introduce how to use abstract classes to build the infrastructure of PHP applications, and provide code examples to help readers better understand. Introduction: An abstract class is a class that cannot be instantiated directly. It is mainly used to define shared properties and methods and provide a normative template for subclasses. Abstract classes can be inherited, and subclasses must implement all abstract methods in the parent class. In a PHP application, use pump

A problem in many binary search implementations? A problem in many binary search implementations? Sep 10, 2023 pm 04:21 PM

We know that binary search algorithm is better than linear search algorithm. The time required to execute this algorithm is O(logn). Most of the time though, there are some issues with the implemented code. Let us consider a binary search algorithm function, as shown below - example intbinarySearch(intarray[],intstart,intend,intkey){ if(start<=end){ intmid=(start+end)/2);//midlocationof

How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? Sep 06, 2023 pm 01:40 PM

How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? Introduction: In Java programming, we often face situations where we need to extend existing classes. However, sometimes the classes we encounter are declared final, that is, they cannot be inherited. So, how to achieve code abstraction and reuse in this case? This article will introduce a method of delegating final classes through forced inheritance to achieve better code abstraction while maintaining code security. Limitations of final classes in Java

super keyword in Java super keyword in Java Sep 16, 2023 pm 10:57 PM

The super variable refers to the direct parent class instance. Super variables can call direct parent class methods. super() acts as a direct parent class constructor and should be on the first line in a child class constructor. When calling the superclass version of an overridden method, use the super keyword. Example live demonstration classAnimal{ publicvoidmove(){ System.out.println("Animalscanmove"); }}cl

See all articles