Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Abstract Class instantiation / data type?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have learnt that an abstract class cannot be instantiated. But can be used as data type. Below is a statement from my text book - Intro to java prog.

You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type.

GeometricObject[] objects = new GeometricObject[10];
You can then create an instance of GeometricObject and assign its reference to the
array like this:
objects[0] = new Circle();



Here in example GeometricObject class is an abstract class.

This statement and example is very confusing. I thought new operator can only create objects. So when you say new GeometricObject[10], is it not creating an array for 10 GeometricObject objects?
 
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:So when you say new GeometricObject[10], is it not creating an array for 10 GeometricObject objects?



Indeed it is. But when you create an array, it doesn't contain any objects; all of its entries are null. It's up to you to put the objects into it.

And yes, the purpose of the new operator is to create an object. And an array is an object, which is why new GeometricObject[10] creates an array.

As for the relevance of abstract classes to that: It's true that you can't create an object whose class is GeometricObject, because that's an abstract class. But you can create an object whose class is GeometricObject[], which is an array. That array is not an abstract class; arrays cannot be abstract.
 
Saloon Keeper
Posts: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This creates an array that can hold 10 objects of type GeometricObject. It does not create an actual instance of a GeometricObject itself.
 
milind k das
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thank you very much for explanations. Another quick question. I'm doing exercises and I'm stuck at one question.


There are bunch of classes in exercise and I'm able to reason with all classes why/why not the classes are legal abstract class except for above one.

I don't see a reason why the above class is not legal abstract class. Please let me know.
 
Paul Clapham
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not valid because of your "unfinished" method. It either needs a body, you know, the place where you put its code between {braces}, or else it needs to be declared abstract.

Did you not get an error message telling you that?
 
milind k das
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's not valid because of your "unfinished" method. It either needs a body, you know, the place where you put its code between {braces}, or else it needs to be declared abstract.

Did you not get an error message telling you that?



Ahh.. yes! I'm trying my mind to be java compiler and jvm. I totally forgot to run it on ide as I was searching my mind about concepts I learnt and trying to apply why its failing.

My mind feels full and sometimes simple occurrences like in this instance for example, I know about methods and how abstract methods behave in superclasses and subclasses.

But I still couldn't point out the mistake. After I see answer key or someone suggests answer, I feel like "ahh.. I know it!"

Is there a learning mistake I'm doing or what should I do to avoid this kind of issues?
 
Sheriff
Posts: 16146
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:Is there a learning mistake I'm doing or what should I do to avoid this kind of issues?


The mistake is not validating what you thought would happen against what actually happens.

It's good that you're trying to think like you're the Java compiler of JVM but once you think you know what the compiler or JVM would do, you should validate your idea against what actually happens when you compile and/or run the code. If you don't complete the postulate-then-validate feedback cycle then your learning will not be complete.
 
Paul Clapham
Marshal
Posts: 26531
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:But I still couldn't point out the mistake. After I see answer key or someone suggests answer, I feel like "ahh.. I know it!"



I couldn't see the error right away either, and I had to compile it, after which I had the same reaction.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic