---
title: "Can we instantiate abstract class?"  
description: "Can we instantiate abstract class?"  
author: "Anonymous User"  
published: 2015-07-24  
updated: 2015-07-25  
canonical: https://www.mindstick.com/forum/23373/can-we-instantiate-abstract-class  
category: "java"  
tags: ["java", "abstract class"]  
reading_time: 2 minutes  

---

# Can we instantiate abstract class?

```
abstract class my {    public void mymethod() {        System.out.print("Abstract");    }}class poly {    public static void main(String a[]) {        my m = new my() {};        m.mymethod();    }}
```

Here, I'm creating [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of my class and calling [method](https://www.mindstick.com/forum/166/webservice-method) of [abstract class](https://www.mindstick.com/articles/1430/abstract-class). Can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) [please explain](https://www.mindstick.com/forum/344/hi-rohith-i-am-new-for-mvc-please-explain-how-to-set-implicitly-isauthenticated-true) this to me?

## Replies

### Reply by Anonymous User

No, you are not creating the instance of your [abstract](https://www.mindstick.com/forum/156718/when-to-use-abstract-classes-in-c-sharp) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.\
To practically see that the class being instantiated is an Anonymous SubClass, you just need to compile both your classes. Suppose you put those classes in two different files:

```
My.java:abstract class My {    public void myMethod() {        System.out.print("Abstract");    }}Poly.java:class Poly extends My {    public static void main(String a[]) {        My m = new My() {};        m.myMethod();    }}
```

Now, compile both your source files:\

```
javac My.java Poly.java
```

Now in the directory where you compiled the source code, you will see the following class files:\

```
My.classPoly$1.class  // Class file corresponding to anonymous subclassPoly.class
```

See that class - Poly$1.class. It's the class file created by the compiler corresponding to the anonymous subclass you instantiated using the below code:

```
new My() {};
```

So, it's clear that there is a different class being instantiated. It's just that, that class is given a name only after compilation by the compiler.\
In general, all the anonymous subclasses in your class will be named in this fashion:\

```
Poly$1.class, Poly$2.class, Poly$3.class, ... so on
```

Those numbers denote the order in which those anonymous classes appear in the enclosing class.


---

Original Source: https://www.mindstick.com/forum/23373/can-we-instantiate-abstract-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
