Abstract classes in Java can also be instantiated

Source: Internet
Author: User

Can abstract classes really not be instantiated in Java?

In the course of learning, a problem has been found that abstract classes cannot be constructed by new before all abstract methods are implemented, but abstract methods can have their own construction methods. This makes me confused, since there is a construction method, and can not be created by new, then the abstract class is not become a concrete class when it is possible to instantiate it?

Find information by surfing the internet, citing: blog.sina.com.cn/s/blog_7ffb8dd5010120oe.html

An abstract class can actually be instantiated, but instead of creating an object through new, the instantiation of the parent class is indirectly implemented by referencing the parent class to an instance of the subclass (because the child class is bound to instantiate his parent class before it is instantiated). This creates an object that inherits the subclass of the abstract class and instantiates its parent class (abstract class). However: interfaces cannot be instantiated (interfaces have no constructorsat all).

The code is as follows:

 Abstract classB {PrivateString str;  PublicB (String a) {System.out.println ("The parent class has been instantiated");  This. str=A;    System.out.println (str); }         Public Abstract voidplay ();} Public classAextendsb{ PublicA (String a) {Super(a); System.out.println ("Subclass has been instantiated"); } @Override Public voidPlay () {System.out.println ("I implemented the method of the parent class."); }         Public Static voidMain (string[] args) {B AA=NewA ("a"); }}

The results are as follows:

The parent class has been instantiated
A
Subclasses have been instantiated

Other than that:
GetInstance () in the calendar

Calendar cal= calendar.getinstance ();
The calendar is an abstract class that cannot be passed directly through the new object, but the provided static getinstance () is the object created for the calendar.
The instance obtained from Calendar.getinstance () is actually a "Greogriancalendar" object

Greogriancalendar is the child of the calendar, and he implements the abstract method in the calendar.a reference to the parent class to point to an instance of the subclass to indirectly implement the instantiation of the parent class. At the same time, using getinstance () has many benefits:
1. New must be generated to allocate memory; getinstance () does not have to be created again, it can use an existing reference to you, which is better than new in performance;

2. New can only be used once when it is created, and getinstance () can be used across the stack area or remotely across regions. So getinstance () is usually created by static instance methods.

Abstract classes in Java can also be instantiated

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.