Reputation: 2874
Why can I not instantiate an abstract class but make an array of the abstract class?
public abstract class Game{
...
}
Game games = new Game(); //Error
Game[] gamesArray = new Game[10]; //No Error
Upvotes: 15
Views: 39630
Reputation: 45070
Game[] gamesArray = new Game[10];
Instantiation means creation of an instance of a class. In the above scenario, you've just declared a gamesArray
of type Game
with the size 10
(just the references and nothing else). That's why its not throwing any error.
You'll get the error when you try to do
gamesArray[0] = new Game(); // because abstract class cannot be instantiated
but make an array of the abstract class?
Later on, you can do something like this
gamesArray[0] = new NonAbstractGame(); // where NonAbstractGame extends the Games abstract class.
This is very much allowed and this is why you'll be going in for an abstract class on the first place.
Upvotes: 18
Reputation: 33544
- An Abstract
class is one whose instance CANNOT be created.
- Creating an Array which holds the Object Reference Variable of that Abstract class are just the references not the object itself.
- An Abstract
class as well as an Interface
in Java is used for implementing behaviors which keeps changing. Consider the Abstract class like laying down some protocols for its concrete classes.
Eg:
public abstract class Vehicle{
}
public class Car extends Vehicle{
public static void main(String[] args){
Car car1 = new Car(); (Possible)
Vehicle car2 = new Car(); (Possible)
Vehicle car3 = new Vehicle(); (NOT Possible)
}
}
Upvotes: 2
Reputation: 372
Abstract classes do not have methods entirely fleshed out. They are used as base classes that would be further extended by other classes that "inherit" them.
For instance, you can have an abstract class called Pet.
public abstract class Pet{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
abstract String makeNoise();
}
That can be extended by inherited classes:
public class Cat extends Pet {
public Cat(){ ... }
...
public String makeNoise(){
return "Meow!";
}
}
...
public class Dog extends Pet {
public Cat(){ ... }
...
public String makeNoise(){
return "Bark!";
}
}
From there, you can have things in your code such as:
Pet thisPet = new Dog();
...
thisPet = new Cat();
And you can create arrays of Pets which would take in both Cats and Dogs as acceptable elements.
Pet petArr[] = new Pet[2];
petArr[0] = new Cat();
petArr[1] = new Dog();
For more information, I'd recommend you look at the java tutorial section covering abstract classes here.
Upvotes: 0
Reputation: 1926
Abstract Class as name implies can't be instantiated. When you did Game[] gamesArray = new Game[10];
it is creating an array
which can hold Game references
. Lets try to relate it to a real life scenario.
Say you have an abstract class Animal
. You can't instantiate it as animal is not a concrete class so Animal obj = new Animal()
would fail. But Animal[] animalArray = new Animal[10]
would create an array which can hold references to 10 animal type objects like dog, cat, horse and so on.
Upvotes: 1
Reputation: 2248
Game games = new Game(); This is creating instance of Abstract Class Game which is not allowed.
Creating array with following Game[] gamesArray = new Game[10];
is just as a declaring Game Object here it is not creating instance. like Game game;
Either you declare 10 object of Game class or crating array of game both are same, just allocation of memory will be different.
Thanks
Tej Kiran
Upvotes: 1
Reputation: 4310
Because you don't violate the abstract class
rules.Essentially,
Game games = new Game();
is broken down to:
Game games; //Will Work because it's just a declaration
games=new Game(); //Will not work because it's instantiation
While creating objects is perfectly valid for abstract classes, initializing is not permitted.
Upvotes: 2
Reputation: 5348
Abstract classes cannot be instantiated, they can be extended. Arrays are in fact objects, you just say to your JVM : hey buddy, make some room for 10 Game objects. That's all, you don't instantiate any Game objects.
Upvotes: 1
Reputation: 79876
Because when you make an array of some object type, you're not trying to instantiate the objects. All you're making is a number of slots to put references in.
new Game[10];
makes 10 slots for Game
references, but it doesn't make a single Game
.
Upvotes: 3