Searene
Searene

Reputation: 27564

create an object of an abstract class != instantiate the abstract class?

I have learned that we can't instantiate an abstract class. But today i tested some codes and i feel confused about it.

package MainPackage;

abstract class abstractClass {
    abstract abstractClass a_function();
}

public class Src {
    abstractClass m;
    public abstractClass abstractClassTest() {
        return m.a_function();
    }
    public static void main(String args[]) {
        System.out.println("Hello world!");
    }
}

Here i create an abstract class abstractClass and return it in the abstractClassTest() function. And it is compiled successfully without errors! IMO before return something, the computer should create an object of that type. And here it should create an object of abstractClass before return m.function(),which i cant understand. i think that we cant instantiate an abstract class means that we cant create an object of an abstract class or we cant new a class(e.g. abstractClass m = new abstractClass() is illegal). But from codes above, it seems that we can create an object of an abstract class. how can it realize? For the code abstractClass m, what does the computer do when it sees the code?We cant say java has instantiate the abstract class m for the code abstractClass m? and if java dont instantiate the class abstractClass, how can it return the object of abstractClass in the code abstract abstractClass a_function();?

Upvotes: 0

Views: 4950

Answers (3)

Stephanie Miller
Stephanie Miller

Reputation: 134

m is never assigned and you never try to instantiate an a new abstractClass with the new keyword. If your code ever actually got to abstractClassTest you would just get a null pointer exception. I think you may be mixing up declaring a variable and actually giving it a value.

Upvotes: 1

Neil Coffey
Neil Coffey

Reputation: 21795

You can't directly instantiate an abstract class, and your code doesn't actually do so. What you have is merely a reference to some subclass or other of your abstract class, and when you come to instantiate 'm' you would have to provide an actual concrete subclass.

You get a similar pattern with interfaces. It's quite legal to write, for example:

List m;
...
m.size();

but when you came to instantiate, you would do so with an implementing subclass, e.g.:

List m;
...
m = new ArrayList();
...
m.size();

Notice how at instantiation, we use ArrayList, not List (which is just the interface).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500135

Yes, it should compile without errors. It would throw a NullPointerException on execution though, if you ever called abstractClassTest, because you never initialize the m variable to refer to an actual instance. In order to do so, you'd have to create a concrete class which subclasses your abstract one.

For example:

public class ConcreteClass extends AbstractClass {
    @Override AbstractClass a_function() {
       return this;
    }
}

public class Src {
    private AbstractClass m = new ConcreteClass();

    public AbstractClass abstractClassTest() {
        return m.a_function();
    }
    public static void main(String args[]) {
        new Src().abstractClassTest();
    }
}

Note that nothing in your code creates an instance of the abstract class. Just because you've got a variable of that type doesn't mean that an object of that type has been created.

Upvotes: 5

Related Questions