Why can't I use a new keyword to initialize an abstract class in Java?

I read somewhere that we cannot initialize the interface, for example:

interface MyInterface{};

And the following code is definitely illegal:

MyInterface m = new MyInterface();

And as I recall, the text I read said: this is because the keyword is newused to allocate memory for class members; therefore, in the case of an interface, we have only abstract functions, therefore there is nothing to highlight in the interface; therefore, initialization of the interface is prohibited.

Well, that makes sense to me.

But in the case of an abstract class, we are allowed to declare and define abstract functions, not an abstract function, as well as ordinary variables, so why are we also not allowed to initialize an abstract class? And because of this, I was wondering when and how variables in an abstract class, if any, are allocated by memory?

+5
source share
10 answers

No object is an instance of a "simple" abstract class - it is always an instance of a particular class. Otherwise, you can call abstract methods ... and there will be no implementation to call.

, , , - "" , .

: , . , . , , , . VM , , "" .

, - , ; .

+14

"", : . - ; - .

, . . , ... , . . .

, . Mercedes, - . , , , , .


: , , :

  • abstract class. , .

  • -: - , .

  • interface: " - , -". ( .)

+5

, : . , .

, . , ?

new , , .

+3

abstract - , ( ). , . .

+3

. , . , ?

.

+1

, new , extends .

, , , .

0

, , ( , ). , .

- - . , .

0

, .

abstract class AC{
    abstract public void m();
}

class Main{
    public static void main(String[] a){
        AC obj = new AC();
        obj.m();  /* Nothing to execute. */
    }
}

.

0

, , :

Serializable x = new Serializable () {};

Empty braces are also required for the abstract class. I can’t think of using these constructs.

0
source

All Articles