Getting started with JavaSE 19: Java object-oriented abstract classes

Source: Internet
Author: User

Getting started with JavaSE 19: Java object-oriented abstract classes
I. Java Abstract class

In the concept of object-oriented, all objects are depicted through classes, but in turn, not all classes are used to depict objects, such

If a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract classes can not instantiate objects, but other functions of the class still exist. The access methods of member variables, member methods, and constructor methods and common classes

Same. Abstract classes cannot instantiate objects, so they must be inherited before they can be used. This is also the reason, which is usually determined at the design stage.

Do you want to design abstract classes. The parent class contains common methods of the subclass set. However, the parent class itself is abstract and therefore cannot be used.

Ii. abstract class

When a class is modified using the abstract modifier in Java, the class is called an abstract class. The function of defining abstract classes is to limit that sub-classes must be real

Some methods, but do not focus on implementation details.

Abstract classes are used in the following two scenarios:

In some cases, A parent class only knows how its subclass should contain, but cannot accurately know how these classes implement these methods.

B abstracts an abstract class from multiple classes with the same features and uses this abstract class as the template of the subclass, thus avoiding the casual design of the subclass.

Use Rules for abstract classes:

Aabstract Defines abstract classes;

Babstract Defines abstract methods. It only has declarations and does not need to be implemented. The abstract method does not end with a semicolon;

C. The class that contains the abstract method is an abstract class;

D. The abstract class can contain common methods or abstract methods;

E abstract classes cannot be directly created. You can define referenced variables.

Instance:

Source File Code of Employee. java:

 

Public abstract class Employee {// defined private variable private String name; private String address; private int number; // constructor public Employee (String name, String address, int number) {System. out. println ("Constructing an Employee"); this. name = name; this. address = address; this. number = number;} // common method 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 ;}}

 

Note that the Employee class is no different. Although this class is an abstract class, it still has three member variables, seven member methods, and one constructor.

Method.

Now, if you try the following example:

AbstractDemo. java source file code:

 

Public class AbstractDemo {public static void main (String [] args) {// The following is not allowed and an error is thrown: Employee e = new Employee ("George W. "," Houston, TX ", 43); System. out. println ("\ n Call mailCheck using Employee reference --"); e. mailCheck ();}}

 

When you try to compile the AbstractDemo class, the following error occurs:

3. inherit abstract classes

We can inherit the Employee class through the general method:

Source File Code of Salary. java:

 

Public class Salary extends Employee {// private member variable private double salary exclusive to the subclass; // constructor public Salary (String name, String address, int number, double salary) {// use the constructor super (name, address, number) of the parent class; setSalary (salary);} // rewrite the mailCheck () method public void mailCheck () of the parent class () {System. out. println ("Within mailCheck of Salary class"); System. out. println ("Mailing check to" + getName () + "with salary" + salary);} public double getSalary () {return salary;} public void setSalary (double newSalary) {// rational if (newSalary> = 0.0) {salary = newSalary;} // rewrite the public double computePay () method in the parent class () {System. out. println ("Computing salary pay for" + getName (); return salary/52 ;}}

 

Although we cannot instantiate an Employee class object, if we instantiate a Salary class object, the object will be

It inherits three member variables and seven member methods.

We will rewrite the source code of AbstractDemo. java:

 

Public class AbstractDemo {public static void main (String [] args) {// class instantiation Salary s = new Salary ("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); Employee e = new Salary ("John Adams", "Boston, MA", 2, 2400.00); System. out. println ("\ nCall mailCheck using Salary reference --"); s. mailCheck (); System. out. println ("\ nCall mailCheck using Employee reference --"); e. mailCheck ();}}

 

The above program compilation and running results are as follows:

Iv. Abstract METHODS

If you want to design such a class that contains a special member method and its implementation is determined by its subclass, you can

Class declares this method as an abstract method. Abstract modifier keywords can also be used to declare abstract methods. abstract METHODS only contain one method name

Method body. The abstract method is not defined. The method name is followed by a semicolon instead of curly brackets.

Instance:

Source File Code of Employee. java:

 

Public abstract class Employee {// private member variable private String name; private String address; private int number; // abstract method public abstract double computePay (); // other code}

 

Declaring an abstract method results in the following two results:

A. If A class contains abstract methods, the class must be an abstract class.

B. Any subclass must override the abstract method of the parent class or declare itself as an abstract class.

The subclass that inherits the abstract method must overload this method. Otherwise, this subclass must be declared as an abstract class. In the end, a subclass is required to implement this abstract method,

Otherwise, objects from the original parent class to the final subclass cannot be instantiated.

If the Salary class inherits the Employee class, it must implement the computePay () method:

Source File Code of Salary. java:

 

Public class Salary extends Employee {// private member variable of the subclass private double salary; // Annual salary // rewrite the abstract method computePay () public double computePay () {System. out. println ("Computing salary pay for" + getName (); return salary/52;} // other code}
For the moment, this is the understanding of abstract classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.