1/5/15

Inheritance : Object Oriented Programming

Inheritance


Inspite of creating everything from scratch, re-using already available things is called Inheritance.
In Java we can do this by extending already available classes.

For Example:-



class BasicCar
{
engine;
wheels;
steering;
Keys;
start()
{
// some logic for starting a Basic car using keys
}
}






class BMW extends BasicCar
{
BMWLogo;
Remote;
// overridden start
start()
{
// logic for starting BMW using remote.
}

Now in this example BasicCar has necessary things to make a car. When someone wants to create BMW car he/she does not need to create it from scratch. And take necessary things from BasicCar by extending it and override things that need to be changed in BMW. Like BasicCar starts using keys while BMW starts using remote. 

This is Inheritance.