Suppose I have a main class Vehicle
and it's sub class Car
. If i create an instance of car like:
Car honda = new Car();
Does this also create and instance of the Vehicle
class aswell?
Suppose I have a main class Vehicle
and it's sub class Car
. If i create an instance of car like:
Car honda = new Car();
Does this also create and instance of the Vehicle
class aswell?
The expression new Car()
will create a single Car
instance. Since Car is a subclass of Vehicle, this instance is itself also a Vehicle instance. In particular, your Car instance will have all methods of the Vehicle class.
So, the answer to your question is: Yes, a Vehicle instance is created. But this is the same instance as the Car instance.