Java inheritance ????

hey how's it going everybody it's you bro hope you're doing well and in this video i'm going to teach you guys how inheritance works in java so sit back relax and enjoy the show make sure you like comment and subscribe one like equals one prayer for the youtube algorithm alright everybody let's talk about inheritance this is the process where one class acquires the attributes and the methods of another so here's an example i have my class that contains my main method an additional class called vehicle a class called car and a class called bicycle what i would like to do is have my car class and my bicycle class inherit everything from the vehicle class so let's list a few attributes that vehicles might have so all vehicles have a speed supposedly so let's make a double variable called speed and we do not really need to assign this for this example and maybe two methods perhaps a go method void go and what should we print maybe this vehicle is moving and let's create a stop method void stop this vehicle is stopped so i have everything related to vehicles within my vehicle class so i would like my car class and bicycle class to receive these attributes and methods from the vehicle class so that i do not have to list this twice so in order to use inheritance when you define the class we're going to add extends and then the name of the class which you would like to receive everything from so car extends by vehicle class and we'll do the same thing for bicycle bicycle extends vehicle now the car class and the bicycle class are subclasses of the vehicle class also known as child classes and the vehicle class is the superclass also known as the parent class so this vehicle class is like an ancestor it's giving an inheritance to its descendants meaning its attributes and its methods so let's create a car object and a bicycle object to see if this theory is true so let's create a car car we'll name it car equals new car and we'll have our car use the go method and it says this vehicle is moving but we didn't list anything within the car class so how can this be how is that possible well since car is a descendant of the vehicle class it receives everything that the vehicle class has meaning a speed and a go method and a stop method now let's create a bicycle object so bicycle let's call this bike equals new bicycle and we'll have our bicycle object use the stop method which it apparently has even though we have nothing listed within the bicycle class and then both the car class and the byte class should have their own speed variable so let's also display that so let's system.out.printline car dot speed the same thing goes with our bike object system.out.printline bike.speed and they should both be zero which they are so that's one of the benefits of using inheritance for one we do not need to list all of these attributes and methods twice now another benefit of this is that we can have this car class and this bicycle class have their own unique additional attributes and methods since not all cars and bicycles are the same they're two completely different objects so we could say that cars will also have a perhaps number of wheels so end wheels and we'll set this to four and then bicycles will say int wheels equals two and then with cars they have doors we can create another variable for that int doors and this could be like two or four i'll just say four for now and then bicycles don't have doors but they do have pedals so and pedals and this will be two and if we head back to our main method let's display the amount of doors that cars have and the amount of pedals that bikes have so we can do this with a print line statement car dot doors and we'll display this and we'll do the same thing with bikes but display the number of pedals bike dot pedals so our car has four doors and our bike has two pedals now these attributes are unique to the car class and the bike class so cars don't have pedals and if we try and access the pedals variable well it's not going to be recognized and our bike doesn't have doors so this variable isn't recognized either so when might you want to use inheritance well for one you'll probably want to use it if you have more than one class so if you have two separate classes for example cars and bicycles you can list everything that they have in common such as a speed some ability to move or go and an ability to stop and then we can just list that within another class and have our card class and bicycle class just receive everything that this vehicle class has because this is what they all have in common so that's the basics of inheritance if you would like a copy of all this code i will post all of this in the comments down below but yeah that's the basics of inheritance in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you