Inheritance in OOPs.!


Inheritance in OOPs

Inheritance is a very important feature in object oriented programming.
Inheritance is extremely useful in enabling the re-usability of the code and is enabled by using the extends keyword.

Inheritance and its advantages

Just like a child inherits some behaviors from their parents, Objects in the programs also get some methods and properties from their parent classes.
Inheritance helps  to access the basic functionality already declared in the parent types and gives an opportunity to declare new functionality in the sub types.
This gives the child an opportunity to declare some new methods and properties in it and still be able to have access to its super classes methods and properties through its instances.
Please note that there is no multiple inheritance in Java. A class can extend only one class. One has to consider using interfaces to add more functionality.
Let me explain Inheritance with an example.
Consider we have a type called vehicle. Its has a basic behavior move. Next we are creating a type called Car. It has more behaviors like move forward and backward. Next i am creating a type called Ford. This has more advanced behavior to move faster.
Let us create the classes for these. Refer to my article on Identifying object in requirement and classes and types for more information on creating types.
Inheritance in Java
To create a new type we need to create classes. So let me create the class Vehicle.
class Vehicle{
 
 void move(){
 
 System.out.println("Vehicle moves")  ;
 
}
 
}
 
class Car extends Vehicle{
 
void moveForward(){
 
System.out.println("Car moving forward") ;
 
}
 
void moveBackward(){
 
System.out.println("Car moving backward") ;
 
}
 
}
 
class Ford extends Car{
 
int moveFaster(){
 
int speed =140;
 
System.out.println("Ford-moving at speed");
 
return speed;
 
}
 
}

Now as per the inheritance rule, when i create an instance of Ford type, it should be able to access the methods of Car and Vehicle using the Fords Instance. 
In the same fashion, when i create an instance of Car type, i will be able to access the cars and vehicles methods but not its child Ford. 
So, Please note that in inheritance, a child instance has access to all the members of its super classes but a parent will not have any idea what types are actually extending it.
class InheritanceDemo{
   public static void main(String[] args){
   Ford f = new Ford();
   f.move();
   f.moveForward();
   f.moveBackward();
   //Car instance
   System.out.println("-->using the Car instance< --");
   Car c = new Car();
   c.move();
   c.moveForward();
   c.moveBackward();
   }
}
Output:
Inheritance in oops example
Using Super keyword:
Super keyword is used access the super class methods and properties in the sub class methods. This keyword helps to access the super class members without creating any instances.  Super keyword can only be used in a class with extends another class.
Super keyword can also be used to invoke the super class constructors. Super(Parameters) should be used in the first line of the constructor.
Note: The accessibility of the properties and methods of a type are subject to the access modifiers applied to the respective members. The methods and properties of super class are visible to the sub classes only when the access levels are set to public, default, protected. More about these in Java access modifiers.

Comments

Popular posts from this blog

Approval Process

Sending Mass Emails From Salesforce

Spring Batch 2.0 -Basic Concepts