+ 3
Java question
After run this code output shows "car =0".how it generate "0"? https://code.sololearn.com/c5mkRE9Hokh7/?ref=app
8 ответов
+ 12
The first thing do after the Car constructor is called is to call the constructor of the super class (Vehicle). The year variable is not instantiated at that point. The Vehicle constructor then calls the printYear method from Car.
year has still no value assigned, so it is 0 (default int value).
Btw there is a typo:
class vehicle -> class Vehicle
+ 9
You're welcome ^^
+ 1
Thank you for your explanation 😀
+ 1
Your code is showing error.
Use 'v' in place of 'V' in the word Vehicle
0
Not sure if this helps but you have +vehicle and then later on + vehicle
One has plus space vehicle
The other has plusvechile
0
class Vehicle {
int year = 1999;
public void printYear(){
System.out.println("Vehicle :"+ year);
}
Vehicle(){
printYear();
}
}
class Car extends Vehicle {
public void printYear (){
int year =2000;
System.out.println ("Car :"+year);
}
}
class Demo{
public static void main(String[] args) {
new Car();
}
}
0
Check now
0
Write an output for following program? class Vehicle{ protected int passengers; public Vehicle(){ passengers += 5; }} class FourWheelVehicle extends Vehicle { public FourWheelVehicle(){ passengers 10;}} class Bus extends FourWheelVehicle { public Bus(){ this(20); passengers++; } public Bus( int passengers){ this.passengers += passengers ; }} public class Run { public static void main(String[] args) { Vehicle myVehicle = new Bus(); System.out.println(myVehicle.passengers); }}