Java polymorphism 🏁

preview_player
Показать описание
Java polymorphism tutorial example explained

#Java #polymorphism #tutorial #example #explained
Рекомендации по теме
Комментарии
Автор


public class Main {

public static void main(String[] args) {

// polymorphism = greek word for poly-"many", morph-"form"
ability of an object to identify as more than one type

Car car = new Car();
Bicycle bicycle = new Bicycle();
Boat boat = new Boat();

Vehicle[] racers = {car, bicycle, boat};

for(Vehicle x : racers) {
x.go();
}

}
}

public class Vehicle {

public void go() {
// TODO Auto-generated method stub

}
}

public class Car extends Vehicle{


@Override
public void go() {
System.out.println("*The car begins moving*");
}
}

public class Bicycle extends Vehicle{


@Override
public void go() {
System.out.println("*The bicycle begins moving*");
}
}

public class Boat extends Vehicle{


@Override
public void go() {
System.out.println("*The boat begins moving*");
}
}

BroCodez
Автор

The ammount of times this man has saved me this semester is unreal.

bahaeddinebenabdallah
Автор

I had struggled with figuring out what polymorphism is for weeks
But with just watching this video
I feel the power in my hands, everything is so clear now
I extremely respect what you did

lequangnghi
Автор

Wow, astonished on how well explained that was. Great video.

kevinnguyen
Автор

This is the best video to understand polymorphism. No doubt! Thank you a lot Bro ❤

mrp.m
Автор

my classmates and my batchmates kept on asking me how come i already know all the topics our professor taught.. i told them i binged watch youtube tutorial on your channel and ask them favor subscribe, like and comment . so you'll make more videos for us... we learned so much from you more than what is taught in our school we so much appreciate your efforts bro..🥰🥰🥰

kapebreak
Автор

another way is to use instanceof inside the for loop, we each time check :
if ( x instanceof Car ) x.go();
if ( x instanceof Bicycle ) x.go();
if ( x instanceof Boat ) x.go();
In case someone don't want to redefine the method go in superclass

khSoraya
Автор

As always: a brilliant and helpful tutorial vid! I really appreciate you content, it really helps me learn Java!

Keep up the good work bro!

fredericoamigo
Автор

You are the best Bro. I really appreciate what you did. May the God bless you and keep you enjoy with a happy and relax life all the time.

ramanaj
Автор

great video bro! Also in your examples can you include user input, i think its an important skill to memorize

cdjosh
Автор

very good teachin and clear example. Thank you!

leonelmessi
Автор

Well this was more effective than my W3Schools, school lesson, and ChatGPT combination learning method. Thanks Bro Code!

Curious_Clover
Автор

Very good video thank you from Kazakhstan

alikkk
Автор

Powerful, very educ
ative and easy to understand, thanks bro code

jQuery
Автор

Thanks for this videos, you are the best lecturer bro!

albertoam
Автор

nicely explained, thanks good for repeating :))

MatejDrbohlav
Автор

Thank you for making these videos. Great content.

longkesh
Автор

You are the best, thank you for helping me through college <3

fire_krystal