Answer the question
In order to leave comments, you need to log in
How is inheritance (extends) different from creating new class instances (new)?
You can create new class instances or instances. For example, there was a Car class, and we created two instances of car1 and car2.
Car car1 = new Car;
Car car2 = new Car;
class Car1 extends Car {
}
class Car2 extends Car {
}
Answer the question
In order to leave comments, you need to log in
The Car class is a stencil.
The new operator is the creation of a PRODUCT according to this stencil
extends is the creation of a new stencil (possibly with additions) by which products can be created.
Is there a difference between creating a product and creating a new stencil?
Your example does not contain code, so it is not correct.
What if we write like this:
class Vehicle {
public speedUp (int newSpeed) {
speed = newSpeed;
}
}
class Car extends Vehicle {
private boolean opened = false;
public void openDoor {
opened = true;
}
}
class Tank extends Vehicle {
private ammo = 10;
public void fire(Vehicle enemy) {
ammo -= 1;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question