-4

I have a abstract class B with two abstract methods

public abstract class B
{
    public abstract void C();

    public abstract void D();
}

I am trying to create the object of abstract class by using

B objB = new B(){

        @Override
        public void C() {

        }

        @Override
        public void D() {

        }};

Is ObjB an instance of Abstract class? Also I tried with instanceof operator as System.out.println(objB instanceof B); which is returning me true.

if the above is not the object of Abstract Class then why above statement is returning true?

Richard
  • 106,783
  • 21
  • 203
  • 265
  • 3
    Possible duplicate of [Why can't an object of abstract class be created?](https://stackoverflow.com/questions/2700256/why-cant-an-object-of-abstract-class-be-created) – Ashok Kumar N Feb 06 '19 at 07:35
  • Your code is incomplete so it is hard to see what you actually have in your code. If I understand correctly, you're creating a singleton of an anonymous (non-abstract) class (using `B objB = new B() { ... }`). `instanceof` will give you `true` not just for object's class, but also for any of its ancestors. So you have `B` (abstract) being extended by anonymous (non-abstract), being instantiated by `objB`. – Amadan Feb 06 '19 at 07:38
  • 1
    From the [Java tutorial](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html): *"The `instanceof` operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface."* – Robby Cornelissen Feb 06 '19 at 07:40

1 Answers1

1

No, you can't immediately instantiate an abstract class, the reason for this is: it is abstract.

In order to create an instance of such a class, you have to create a subclass, which provides an implementation for all abstract members of the class. Even if you haven't added any abstract methods, this still remains true.

instanceof

doesn't mean "This variable is of the type", but rather, this variable is of either this type, or an instance of a type that inherits from this type. So, on any object in Java: myObject instanceof Object will, if myObject is instantiated, return true, no matter the actual type of myObject.

The closest you can get to instantiating an abstract class without creating a concrete and named child class, is by creating an anonymous implementation:

B object = new B() {
            @Override
            public void C() {

            }

            @Override
            public void D() {

            }
}

The above block, as you did it, is not a direct instantiation of the abstract class.

It's the creation of a new class, for which you didn't provide a name, and instantiation of that.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • 1
    "will return true, no matter the actual type of myObject." Unless it is null! – Andy Turner Feb 06 '19 at 07:39
  • @AndyTurner Good point. – Stultuske Feb 06 '19 at 07:40
  • 1
    "without creating a child class" I think it is misleading to imply this doesn't create a child class. It's not using the `class` keyword, but it most definitely does create a class, and then causes instances of that class to be created at runtime. – Andy Turner Feb 06 '19 at 07:42
  • I indeed had some ambiguity in my explanation, as later I stated: "It's the creation of a new class, for which you didn't provide a name, and instantiation of that." – Stultuske Feb 06 '19 at 07:44