Lizzie
Lizzie

Reputation: 351

Array of Objects

In the question below I not getting the bold line. Does that line mean create an array objects of class compartment? Because as per my knowledge Java doesn't have concept of pointers.

Create an abstract class compartment to represent a rail coach. Provide an abstract function notice in the class. Derive FirstClass, General, Ladies and Luggage classes from the Compartment class. Override notice function in each of them. Create a class TestCompartment. Write main function to do the following: Declare an array of compartment pointers of size 10.

Upvotes: 1

Views: 878

Answers (4)

Cristina_eGold
Cristina_eGold

Reputation: 1553

Although the underlying implementation of references in Java comes down to C-like pointers (because it does, at some point), I think references would have been a better way of putting through the request.

You only have to declare a classic array.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

Java has no pointers as such.Everything that's not a primitive is a reference.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

On one hand, Java does not have a concept of pointers; on the other hand, everything other than primitives in Java (i.e. all Object-derived things) could be through of as "pointers", although technically they are not called that.

Java calls them references, but since there is such thing as null reference, they behave very much like pointers in C and C++.

Anyway, when you create an array of ten non-primitives, you create an array of references, each one set to null:

Compartment[] compartments = new Compartment[10];

This is different from creating ten Compartment objects, in that the objects themselves are not created when you create an array, only places through which you could reference these objects later if you need to.

Upvotes: 2

tskuzzy
tskuzzy

Reputation: 36476

It most likely means: create a size 10 array of references.

Upvotes: 0

Related Questions