Posts Tagged tutorial 10
Java Tutorial 11 – Inheritance
inheritance means Inheritance ! (hehe you know that )
i mean consider two classes one is parent and second is child class and if you inherits child from parent class that child class can access all the variables and methods which are public in parent.
Let we have 2 classes Car and TataNano:
Car:
public class Car {
public void breaks(){
// breaks the car
}
public void Speedup(){
// speed up the car
}
public void indicator(){
// indicator operation
}
}
Now we have a model car which has functions like break speedup and indicator.
now consider for a while that you are making a Racing game and that has lots of different cars, but this break and indicators will be common in each of the cars. So you have to define all this three methods in each car.
Instead what u can do is throw common methods in one class called car and all the different cars will be inherited from it so all the cars automatically has all the three methods.
In short in this example Car is parent and specific car like Tata, Nissan, etc are childs and so it contains all the public methods of car.
How to inherit? :
if you want to inherit Tata from Car all you have to say is:
public class Tata extends Car {}
now you can directly call all three method of Car inside Tata class without creating object of Car.
Again doubts are invited.
have a good1, Peace.