Inheritance
Inheritance
What is the meaning of Inheritance ?
- Simply,inheritance is owning or belonging something/s from one person/generation to another person/generation.It may be anything.
- Taking the things/properties in one JAVA class to another JAVA class is called as Inheritance.
- sub class (child) - the class that inherits from the initial class.
- super class (parent) - the class that is used to inherit other classes.
- First of all we should create a relationship between 2 classes.
- For that,we use the keyword extends.
- If we consider the process of manufacturing a new car as an example,that car may contain newest features and it will include the features of its previous version.
- So, the oldest version can be identified as the super class and the modern version will be the sub class.
- The older version has a colour,name,lights,..etc.Actually they're variables.And it perform common tasks like drive,reverse,break,..etc.They're methods.
- Modern car will contain all those features as before and it may have modern features like cameras,gps,wifi,autopilot,...etc
- So we can inherit those things like below.
class Oldcar{
String color;
String name;
void drive(){
}
void break(){
}
}
class Newcar extends Oldcar{
String camera;
string wifi;
void autopilot(){
}
}
So this is a simple description about Inheritance.
Let us meet with another new article soon !
Comments
Post a Comment