Use of Inheritance
* To promote code reuse
* To use polymorphism
Inheritance in java is done by either extending a class or implementing the interfaces.
Inheritance is all about having a parent child relationship.So instead of rewriting the same functionality several times , you can just inhert those from the parent class.
Inheritance relationship can be of two types.
IS-A Relationship
Has-A Relationship
Is-A relationship is said to be direct relationship and can be done by either extending the parent class or by implementing the interface as said earlier.
For example
class Vehicle{}
class car extends vehicle{}
class ford extends car{}
In this case ford IS-A car . car IS-A Vehicle . So in the class Vehicle we can define several properties and functionalities that are similar , and declare several functionalities as abstract so that they can be overriden by the subclasses that inherit them.Since we have abstract methods in Vehicle class , we need to declare Vehicle as a abstract class.
Has-A relationship is said to be indirect relationship , in the sense , you dont want to extend or implement anything , rather create a instance of the class you want to inherit , and use that instance to invoke its methods.In this way the user dont have to know anything about the inherited class.For him,it seems like the functionality is in his Class.
for example
class Animal{}
class Halter { void tie(Rope r) { } }
class horse extends Animal {
Halter myhalter;
void tie(Rope r) { myhalter.tie(Rope r); }
}
In this case horse IS-A Animal.But Halter is not a Animal.Its just a equipment used to tie the horse. So horse Has-A Halter.Though horse doesnot extend or implement Halter it can use the 'tie' functionality by creating a instance of the Halter class.This is done as shown above.Now it seems like , we are invoking the 'tie' functionality of the horse, which in turn invokes the 'tie' functionality of Halter Class.so the actual implementation goes in Halter class and the user need not be aware of what is happening in the Halter class.For him its the tie method in the horse class is performing the operations.
* To promote code reuse
* To use polymorphism
Inheritance in java is done by either extending a class or implementing the interfaces.
Inheritance is all about having a parent child relationship.So instead of rewriting the same functionality several times , you can just inhert those from the parent class.
Inheritance relationship can be of two types.
IS-A Relationship
Has-A Relationship
Is-A relationship is said to be direct relationship and can be done by either extending the parent class or by implementing the interface as said earlier.
For example
class Vehicle{}
class car extends vehicle{}
class ford extends car{}
In this case ford IS-A car . car IS-A Vehicle . So in the class Vehicle we can define several properties and functionalities that are similar , and declare several functionalities as abstract so that they can be overriden by the subclasses that inherit them.Since we have abstract methods in Vehicle class , we need to declare Vehicle as a abstract class.
Has-A relationship is said to be indirect relationship , in the sense , you dont want to extend or implement anything , rather create a instance of the class you want to inherit , and use that instance to invoke its methods.In this way the user dont have to know anything about the inherited class.For him,it seems like the functionality is in his Class.
for example
class Animal{}
class Halter { void tie(Rope r) { } }
class horse extends Animal {
Halter myhalter;
void tie(Rope r) { myhalter.tie(Rope r); }
}
In this case horse IS-A Animal.But Halter is not a Animal.Its just a equipment used to tie the horse. So horse Has-A Halter.Though horse doesnot extend or implement Halter it can use the 'tie' functionality by creating a instance of the Halter class.This is done as shown above.Now it seems like , we are invoking the 'tie' functionality of the horse, which in turn invokes the 'tie' functionality of Halter Class.so the actual implementation goes in Halter class and the user need not be aware of what is happening in the Halter class.For him its the tie method in the horse class is performing the operations.
While playing with minimum of 1 or 2 classes its easy to implement the concept of inheritence using concrete(normal) classes.
But in case of inheriting properties within a hierarchy or family , its better to use Abstract classes , so that you can define some functionalities that is same throughout the hierarchy and you can override several functionalities so that its more specific to that subclass.
So only the circumstance differ and not the concept of inheritence.
But in case of inheriting properties within a hierarchy or family , its better to use Abstract classes , so that you can define some functionalities that is same throughout the hierarchy and you can override several functionalities so that its more specific to that subclass.
So only the circumstance differ and not the concept of inheritence.
It is a process of hiding the implementation details from other classes and just providing a way to access and modify its data.So that ,you can make necessary changes or corrections to your code without affecting or changing the classes that inherits your class.
Consider a situation, where you write a class and your friends inherit it. your class consist of a instance variable declared public as shown below.
class your{
public int size;
}
Now, your friend assigns a value to size , that produces a error in your code.
class friends{
yours y=new yours();
yours.size=-32;
}
Though assigning -32 to size is legal, its bad for you.Because you would be expecting positive values alone. So what will you do now? you would write a setter method that check the value and assigns it to size.
So, there is a need to change your code , as well as your friends code to access the method you have implemented in your class.
This where Encapsulation comes in place.
Benefit of Encapsulation
The ability to make changes in your implementation code without breaking the code of others who use your code is a key benefit of encapsulation.Thus bringing in the Flexibility and Maintainability,and scalability.
In order to do that, Declare your instance variables as private. provide methods to get and set the values of the variable ,like something present in javabeans.
Now lets rewrite your class, so that it is flexible , maintainable , and scalable.
class yours{
private int size;
public int getSize() { return size; }
public void setSize(int x) { //code for checking whether x is positive size=x; }
}
Not only checking, you can also do several other operations, whithout affecting other classes that inherits it.
Consider a situation, where you write a class and your friends inherit it. your class consist of a instance variable declared public as shown below.
class your{
public int size;
}
Now, your friend assigns a value to size , that produces a error in your code.
class friends{
yours y=new yours();
yours.size=-32;
}
Though assigning -32 to size is legal, its bad for you.Because you would be expecting positive values alone. So what will you do now? you would write a setter method that check the value and assigns it to size.
So, there is a need to change your code , as well as your friends code to access the method you have implemented in your class.
This where Encapsulation comes in place.
Benefit of Encapsulation
The ability to make changes in your implementation code without breaking the code of others who use your code is a key benefit of encapsulation.Thus bringing in the Flexibility and Maintainability,and scalability.
In order to do that, Declare your instance variables as private. provide methods to get and set the values of the variable ,like something present in javabeans.
Now lets rewrite your class, so that it is flexible , maintainable , and scalable.
class yours{
private int size;
public int getSize() { return size; }
public void setSize(int x) { //code for checking whether x is positive size=x; }
}
Not only checking, you can also do several other operations, whithout affecting other classes that inherits it.
No comments:
Post a Comment