Abstract classes in Java can also be instantiated, and java abstraction can also be used as an example.

Source: Internet
Author: User

Abstract classes in Java can also be instantiated, and java abstraction can also be used as an example.

Can an abstract class be instantiated in Java?

In the course of learning, we found a problem. abstract classes cannot use new to construct this object until they implement all abstract methods, however, abstract methods can have their own constructor methods. In this way, I am confused. Since there are constructor methods that can not be created through new, can the abstract class be instantiated when it is not changed to a specific class?

Search for information and reference through the internet: blog.sina.com.cn/s/blog_7ffb8dd5010120oe.html

An abstract class can be instantiated, but its instantiation method is not to create an object through the new method, instead, the parent class is referenced to point to the subclass instance to indirectly instantiate the parent class (because the subclass will first instantiate its parent class before it is instantiated. In this way, an object that inherits the subclass of an abstract class is created, and its parent class (abstract class) is instantiated). However, an interface cannot be instantiated (the interface does not have a constructor at all ).

The Code is as follows:

Abstract class B {private String str; public B (String a) {System. out. println ("the parent class has been instantiated"); this. str = a; System. out. println (str);} public abstract void play ();} public class A extends B {public A (String a) {super (a); System. out. println ("subclass instantiated");} @ Override public void play () {System. out. println ("I implemented the parent class method");} public static void main (String [] args) {B aa = new A ("");}}

The result is as follows:

The parent class has been instantiated.
A
Subclass has been instantiated

 

In addition:
GetInstance () in the Calendar ()

Calendar Cal = Calendar. getInstance ();
Calendar is an abstract class that cannot directly use the new object, but the static getInstance () provided is to create an object for the Calendar.
The instance obtained from Calendar. getInstance () is actually a "GreogrianCalendar" object.

GreogrianCalendar is a subclass of Calendar. It implements the abstract method in Calendar. By referencing the parent class, you can point to the Child class instance to indirectly instantiate the parent class. At the same time, using getInstance () has many advantages:
1. New must generate a new object and allocate memory. getInstance () does not have to be created again. It can be used to reference an existing object, which is superior to new in efficiency;

2. New can only be used once after creation, while getInstance () can be used across stack regions or remotely across regions. Therefore, getInstance () is usually used to create static instances.

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.