Ticker

6/FOX NEWS/ticker-posts

Ad Code

Responsive Advertisement

What is Polymorphism? And Why It’s Worth Learning

What is Polymorphism? And Why It’s Worth Learning Polymorphism is a powerful tool in a programmer’s arsenal. Here’s how it works and how you can use it.

If you can drive a 4-door commuter car, you can also drive a pickup truck. If you’ve driven a car with a combustion motor, you can also drive an electric car.

google drive settings 1Polymorphism" class="wp-image-15753" srcset="https://howtogetafreejob.com/wp-content/uploads/2020/10/google-drive-settings-1.jpg 737w, https://howtogetafreejob.com/wp-content/uploads/2020/10/google-drive-settings-1-300x150.jpg 300w, https://howtogetafreejob.com/wp-content/uploads/2020/10/google-drive-settings-1-665x333.jpg 665w, https://howtogetafreejob.com/wp-content/uploads/2020/10/google-drive-settings-1-20x10.jpg 20w" sizes="(max-width: 737px) 100vw, 737px" />

What is Polymorphism? And Why It’s Worth Learning

Practice Polymorphism

The shape and size of passenger vehicles may be different from one to the next. The motor that runs those vehicles could be completely different as well. But it doesn’t matter to the driver.

You just get in, buckle up, start the vehicle, put it in gear, and drive. That’s because cars, trucks, and vans are polymorphic.

Polymorphism: Breaking It Down

Let’s look at the word polymorphism. You can break it down into polymorph, and ism.

Poly means many, like how polygon means many angles. When used as a noun, a morph is a variant of a species. And ism can mean system.

So polymorphism simply means a system of many variations. That still doesn’t tell you much about how it’s used in programming, though. That’s next.

If It Walks Like A Duck… Why Polymorphic Objects Are Awesome

 Polymorphism

When you create a class in your code that inherits from another class, you’re binding the new class to a contract. The contract states that every variable and function in the parent will also be in the child.

Every vehicle has a steering wheel, gas and brake pedals, and a turn signal. You don’t need to open the hood to drive a car. All that matters is that it’s a car.

The same thing applies to classes that inherit from other classes. Here is an example in TypeScript:


class Vehicle {
private _engine: string;
private _tires: number;
constructor(engine: string = “combustion”, tires: number = 4) {
this._engine = engine;
this._tires = tires;
}
accelerate(velocity: number) {
console.log(“accelerating at a velocity of ” + velocity);
}
brake(pressure: number) {
console.log(“applying ” + pressure + ” pressure”);
}
turnLeft() {
console.log(“turning left”);
}
turnRight() {
console.log(“turning right”);
}
}
class Car extends Vehicle {
}
class Tesla extends Car {
constructor() {
super(“electric”);
}
}

In this example, there is a Vehicle class. The Car class inherits from the Vehicle class. And Tesla inherits from Car. Now let’s create a couple of objects and look at them.

let myCoupe: Car = new Vehicle();
console.log(myCoupe);
console.log(myCoupe.constructor.name);
let mySedan: Vehicle = new Tesla();
console.log(mySedan);
console.log(mySedan.constructor.name);
myCoupe.turnLeft();
mySedan.turnLeft();

You can see that we declared myCoupe to be a Car and mySedan to be a Vehicle. Then we instantiated myCoupe as a new Vehicle and mySedan as a new Tesla. If you visit the TypeScript sandbox and run the code, you’ll see that it works without error. And it behaves as you would expect, based on the contract.

In other words, all vehicles can turn left because they inherited it from the Vehicle class. The compiler knows that every child of the Vehicle agreed to the contract. So it assumes all is well, no matter what classes the objects were typed or instantiated as.

This is sometimes called “duck typing.” The compiler assumes that if it walks like a duck and talks like a duck, it might as well be a duck. So the compiler doesn’t worry about the details and just treats the object like a duck.

Polymorphism Makes Your Code Bulletproof

Another advantage of the polymorphism contract is a guarantee that your code will work. If you’ve strongly typed all your variables and what each function returns, you know that every child will always match variables, functions, and types.

That means you can add to and change the code in your classes without breaking your program. Every object that references a Vehicle object will always get data and functionality that meets expectations, no matter how much Car changes.

The code inside the function may not put out the correct results. But that’s a different kind of problem. As long as the function follows the contract and returns the expected type of variable, it won’t result in a code-breaking error.

READ MORE:www.makeuseof.com/what-is-polymorphism/

The post What is Polymorphism? And Why It’s Worth Learning appeared first on How To Get Job.



from WordPress https://howtogetafreejob.com/what-is-polymorphism-and-why-its-worth-learning/
Reactions

Post a Comment

0 Comments