Java question on car classes task (polymorphism) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Java question on car classes task (polymorphism)

Hi, I think there is a bug in this assignment. Below is the full code I implemented which returns completely nothing. Even when I erase ElectricVehicle and HybridVehicle classes to just output vehicle.start() and .resource() which has been given in the assignment, nothing happens. Could you help me on this? class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle electric = new ElectricVehicle(); Vehicle hybrid = new HybridVehicle(); //calls vehicle.start(); vehicle.resource(); electric.start(); electric.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class ElectricVehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } } class HybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use petrol and electricity"); } }

7th Jan 2022, 2:25 PM
Piotr
8 Answers
+ 5
class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle electric = new ElectricVehicle(); Vehicle hybrid = new HybridVehicle(); //calls vehicle.start(); vehicle.resource(); electric.start(); electric.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class ElectricVehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } class HybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use both petrol and electricity"); } }
24th Dec 2022, 9:47 AM
TELUGU SUSMITHA
TELUGU SUSMITHA - avatar
+ 2
There is a wrong curly brackett. The one above class HybridVehicle is too much. https://code.sololearn.com/crmAjqs5n7RL/?ref=app
7th Jan 2022, 2:37 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle elVehicle = new elVehicle(); Vehicle hybrid = new hybridVehicle(); //calls vehicle.start(); vehicle.resource(); elvehicle.start(); elvehicle.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class elvehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } class hybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use both petrol and electricity"); } } 7th May 2022, 4:29 PM
24th Dec 2022, 9:26 AM
TELUGU SUSMITHA
TELUGU SUSMITHA - avatar
0
Hi, thanks for replying. That wasn't it (not sure how it got here), since I would have just got a curly bracket error. It was a bug in the end, had to reload everything and it worked.
7th Jan 2022, 4:02 PM
Piotr
0
class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle elVehicle = new elVehicle(); Vehicle hybrid = new hybridVehicle(); //calls vehicle.start(); vehicle.resource(); elvehicle.start(); elvehicle.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class elvehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } class hybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use both petrol and electricity"); } }
27th Apr 2022, 1:15 PM
Edward Akwesi
0
class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle elVehicle = new elVehicle(); Vehicle hybrid = new hybridVehicle(); //calls vehicle.start(); vehicle.resource(); elvehicle.start(); elvehicle.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class elvehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } class hybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use both petrol and electricity"); } }
7th May 2022, 4:29 PM
U.L.F. Feroza
0
Can anyone tell me why I get no output? class Main { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle electric = new ElectricVehicle(); Vehicle hybrid = new HybridVehicle(); //calls vehicle.start(); vehicle.resource(); electric.start(); electric.resource(); hybrid.start(); hybrid.resource(); } } class Vehicle{ public void start(){ System.out.println("Starting"); } public void resource(){ System.out.println("I use petrol"); } } class ElectricVehicle extends Vehicle{ /*reimplement resource() method to output "I use electricity"*/ public void resource(){ System.out.println("I use electricity"); } } class HybridVehicle extends Vehicle{ /*reimplement resource() method to output "I use both petrol and electricity"*/ public void resource(){ System.out.println("I use both petrol and electricity"); }
8th Aug 2022, 7:00 AM
Alex Jan Lubbertsen
Alex Jan Lubbertsen - avatar
0
class main { public static void main(String args[]) { System.out.println("Starting"); System.out.println("I use petrol"); System.out.println("Starting"); System.out.println("I use electricity"); System.out.println("Starting"); System.out.println("I use both petrol and electricity"); } }
19th Mar 2023, 3:46 AM
Pavizhan S
Pavizhan S - avatar