+ 3

Java question

After run this code output shows "car =0".how it generate "0"? https://code.sololearn.com/c5mkRE9Hokh7/?ref=app

25th Apr 2019, 5:57 AM
Jaliya Roshen
Jaliya Roshen - avatar
8 Réponses
+ 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
25th Apr 2019, 7:04 AM
Tashi N
Tashi N - avatar
+ 9
You're welcome ^^
25th Apr 2019, 7:54 AM
Tashi N
Tashi N - avatar
+ 1
Thank you for your explanation 😀
25th Apr 2019, 7:12 AM
Jaliya Roshen
Jaliya Roshen - avatar
+ 1
Your code is showing error. Use 'v' in place of 'V' in the word Vehicle
25th Apr 2019, 10:56 AM
Hemant Jaiswal
Hemant Jaiswal - avatar
0
Not sure if this helps but you have +vehicle and then later on + vehicle One has plus space vehicle The other has plusvechile
25th Apr 2019, 3:48 PM
Cole
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(); } }
27th Dec 2019, 10:03 AM
Mirza Adeel
Mirza Adeel - avatar
0
Check now
27th Dec 2019, 10:04 AM
Mirza Adeel
Mirza Adeel - avatar
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); }}
21st Apr 2021, 9:11 AM
Mayas j
Mayas j - avatar