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 constructors at all).
The code is as follows:
AbstractClassBPrivateString str;PublicB (String a) {System.out.println ("The parent class has been instantiated");This.str=A System.out.println (str); }PublicAbstractvoid play (); public class A extends< Span style= "COLOR: #000000" > b{public A (String a) {super (a); System.out.println ("Subclass already instantiated" public void play () {System.out.println ("I implemented the method of the parent class" ); } public static void main (string[] args) {B aa=new A ("a"
The results are as follows:
The parent class has instantiated a subclass already instantiated
In addition: Calendar of getinstance ()
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 advantages: 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. text: http://www.cnblogs.com/crt-share/archive/2015/08/29/4769331.html
Abstract classes in Java can also be instantiated. RP