Sandeep P
Sandeep P

Reputation: 4411

Why can abstract class be instantiated like this?

I read that the major difference between Class and Abstract Class is , abstract class cannot be instantiated,

but i can create object for abstract class

public abstract class Earth {

    abstract void sand();

    void land() {

    }
}

and using new key word i created the object, for the abstract

    Earth mEarth = new Earth() {

        @Override
        void sand() {

        }
   };

I had some questions on it which have not proper answer on Inet,

1) is new keyword is used to instance the class ?

2) is instance is nothing but object ?

3) is mEarth is called object (instance of Earth) ?

now i can call any method (as callback or as value return) mEarth.sand(); mEarth.land(); using earth object

Upvotes: 3

Views: 193

Answers (4)

LMK
LMK

Reputation: 2952

 Earth mEarth = new Earth() {

    @Override
    void sand() {

    }
};

mEarth --- reference varible holding , sub class object (anonymous sub/inner class of Earth)
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

 new Earth() {

    @Override
    void sand() {

    }
};

Is Class without name

1) is new keyword is used to instance the class ?
A) Yes

2) is instance is nothing but object ?
A)Yes

3) is mEarth is called object (instance of Earth) ?
A) mEarth is reference varible holding sub class(anonymous implementation) object

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You can't create an abstract class:

  new Earth(); // <- that's compile time error

However you can create non-abstract derived class as you do

  // New non-abstract (local) class that extends Earth 
  new Earth() {
    // You have to override an abstract method
    @Override
    void sand() {
      ...
    }
  }

The answers for questions:

  1. Yes, the new keyword creates new instance.
  2. No; the created instance is an object that extends Earth class, not just Object
  3. mEarth field declared as Earth and contains an object that extends class Earth, so you call any methods of Earth.

Upvotes: 5

Bhavin Nattar
Bhavin Nattar

Reputation: 3215

May this help you:

No buddy, you are not creating the instance of your Abstract Class here.
Instead 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.

= new Earth() {}; means that there is an anonymous implementation, not simple instantiation of an object, and object should have been like = new Earth();
An abstract type is defined largely as one that can't be created. We can create subtypes of it, but not of that type itself.

Upvotes: 1

Eypros
Eypros

Reputation: 5723

1) Yes it does. Although it's not the only way around its the most frequent (and safe I think).

2) As far as I know also yes. Instance means you have a special aspect of a class meaning with values to its instance members (local variables) etc. These values are specific for every instance that is object.

3) In here you are creating an object of an anonymous subclass of Earth (since Earth is abstract and cannot be instantiated). This means you don't specifically give a name to your subclass just provide the implementation of sand() in order to be concrete (and be instantiatable).

Upvotes: 1

Related Questions