3

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

Sandeep P
  • 4,291
  • 2
  • 26
  • 45
  • You can't do `new Earth();` providing that Earth is abstract class. However you can create a derived class – Dmitry Bychenko May 23 '14 at 06:32
  • 1
    we cant do this like new Earth(); but we can do as i mention above ! – Sandeep P May 23 '14 at 06:35
  • What you're making is an anonymous subclass of `Earth`. Not instantiating `Earth` directly. – awksp May 23 '14 at 06:37
  • 2
    You are creating a new anonymous class for the Earth class when you use the { @Override}. When you use Earth mEarth = new Earth() you will see that you cannot create an abstract class. The new keyword is indeed used to create a class instance. What an object is depends on your definition of object. mEarth is an subclass of Object because all classes you will create are a subclass of Object. mEarth is a reference to an instance of Earth. Earth itself is a class but this is also a real object. – Arno van Lieshout May 23 '14 at 06:37
  • Take a look at the class files generated from compilation, you will see a `Earth$1.class`. This is an example of an [Anonymous Class](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). – Boris the Spider May 23 '14 at 06:38
  • 1
    http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class?rq=1 , http://stackoverflow.com/questions/14457076/if-abstract-classes-can-not-be-instantiated-exactly-what-is-this-code-abs-cls?lq=1 , http://stackoverflow.com/questions/22164036/difference-between-new-test-and-new-test?lq=1 – user2864740 May 23 '14 at 06:58

4 Answers4

5

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.
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1
 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

LMK
  • 2,882
  • 5
  • 28
  • 52
1

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.

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
1

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).

Eypros
  • 5,370
  • 6
  • 42
  • 75