//Creating a constructor function function Vehicle() { this.vehicleName="vehicleName"; throw new Error("You cannot create an instance of Abstract Class"); } Vehicle.prototype.display=function() { return "Vehicle is: "+this.vehicleName; } //Creating a constructor function function Bike(vehicleName) { this.vehicleName=vehicleName; } //Creating object without using the function constructor Bike.prototype=Object.create(Vehicle.prototype); var bike=new Bike("Honda"); document.writeln(bike.display()); </script>
Question
//Creating a constructor function function Vehicle() { this.vehicleName="vehicleName"; throw new Error("You cannot create an instance of Abstract Class"); } Vehicle.prototype.display=function() { return "Vehicle is: "+this.vehicleName; } //Creating a constructor function function Bike(vehicleName) { this.vehicleName=vehicleName; } //Creating object without using the function constructor Bike.prototype=Object.create(Vehicle.prototype); var bike=new Bike("Honda"); document.writeln(bike.display()); </script>
Solution
This script is written in JavaScript. It's creating an abstract class named Vehicle and a subclass named Bike.
Here's a step-by-step explanation:
-
A constructor function
Vehicle()is created. This function sets a propertyvehicleNameto "vehicleName" and throws an error if you try to create an instance of it. This is a way to simulate an abstract class in JavaScript, which doesn't natively support abstract classes. -
A method
display()is added to theVehicleprototype. This method returns a string that includes thevehicleName. -
Another constructor function
Bike(vehicleName)is created. This function sets thevehicleNameproperty to the argument passed in. -
The `Bike
Similar Questions
orrect answerWhat is the prototype of the default constructor of this Java class? public class prototype { }
Select the correct answerWhat is the prototype of the default constructor of this Java class? public class prototype { }Optionsprototype(void)prototype( )public prototype(void)public prototype( )
To create an instance of a class, we must invoke the constructor, but we never directly invoke the constructor method directly. Briefly (in one sentence), state what we need to do in order to create an instance of a class?
class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, as member. Inherit the classes bicycle, car, bike, and lorry from vehicle class. Consider car and bike only as passenger vehicle. Get inputs engine number, chassis number, top speed, number of passengers, number of gears for Passenger vehicle, and if is not a passenger vehicle, get inputs total amount of load to carry for the respective inherited classes. Generate a report for the vehicles. Implement the concept using inheritance and method overriding.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.