Please explain the difference between object one and two:
car one = new opel();
opel two = new opel();
Class opel extends class car.
Please explain the difference between object one and two:
car one = new opel();
opel two = new opel();
Class opel extends class car.
You could reassign one
to an object of some other subclass of car
:
one = new Ford(...);
But you can't reassign two
like that, since it's restricted to being an opel
.
If m
is a method that's defined in the opel
class but not the car
class, then the compiler will give you an error if you do this:
one.m();
But this is OK:
two.m();
since it knows two
is restricted to being an opel
, so it knows that method m
will exist.
Generally, you want to declare your variables to be the broadest type possible. That is, if you're only going to use methods in car
, then declare it with type car
(like you did with one
), because you're telling the reader that the algorithm only needs to know that one
is a car
, it doesn't need to know what kind of car it is.
More: It's necessary to understand that a variable has both a compile-time type and a runtime type. The compiler sees one
as a car
, because it doesn't know what kind of car
the variable will be at any given time. But the runtime type of both will be opel
. If you have a method mm
that is defined for car
, and then overridden for opel
, one.mm()
and two.mm()
will both call the same method. Once the compiler looks at the compile-time type and decides the call is legal, then which one gets called when the program is run depends on the runtime type.
Well in your example there isn't any difference, variables one and two are referencing two separate instances of the class 'opel'. Variable titled 'one' could have been assigned to any other object which extends car, or car itself, where variable two can only be assigned to a object of type opel.
One reason you might use the base class to declare the variable without assigning it could be because you don't know what specific class will be used until runtime, say
private Car buildCar(string brandOfCar){
Car car;
switch (brandOfCar){
case "Honda": car = new Honda();
break;
case "Opel": car = new Opel();
break;
}
return car;
}
You could also create an array of Car, and fill it with a mix of objects which extend Car, where if you created an array of Opel, it could only contain Opels.
That said, cars are not good examples, as the brand of the car should probably simply be a property of the class Car, not an actual class which extends car.
public class Opel extends Car{
private Car car;
private String TypeofCar;
public Opel(){
TypeofCar="Honda";
}
public void display(){
System.out.println("Car :" + TypeofCar);
}