0% found this document useful (0 votes)
4 views1 page

Code

The document contains Java code defining a Vehicle class and a Car class that extends Vehicle. The Vehicle class has a method to move and a property for the number of wheels, while the Car class overrides the move method and adds a honk method. The main method demonstrates creating instances of Vehicle and Car, showcasing polymorphism with method overriding.

Uploaded by

avostyv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Code

The document contains Java code defining a Vehicle class and a Car class that extends Vehicle. The Vehicle class has a method to move and a property for the number of wheels, while the Car class overrides the move method and adds a honk method. The main method demonstrates creating instances of Vehicle and Car, showcasing polymorphism with method overriding.

Uploaded by

avostyv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class Vehicle {

public int numWheels;

public Vehicle(int wheels){


[Link] = wheels;
}

public void move(){


[Link]("Vehicle is moving.");
}
}

class Car extends Vehicle{


public int numDoors;

public Car(int wheels, int doors){


super(wheels);
[Link] = doors;
}

public void move(){


[Link]("Car is moving.");
}

public void honk(){


[Link]("Beep beep");
}
}

public class Main {


public static void main(String[] args) {

Vehicle v = new Vehicle(4);


[Link]();
[Link](" | Wheels: " + [Link]);

[Link]();

Car c = new Car(4, 2);


[Link]();
[Link]();
[Link]("Doors: " + [Link]);
[Link]("Wheels: " + [Link]);

[Link]();

// Polymorphism example
Vehicle v2 = new Car(4, 4);
[Link](); // runs Car's move() because of overriding
[Link]("Wheels: " + [Link]);
}
}

You might also like