Inheritance,its requirement,use and implementation

Inheritance



Inheritance example:a tree


Inheritance is the most interesting feature of java and other Object oriented programming language.It is the only reason why i hate "C".
Firstly,lets know what inheritance is??As the word suggest means taking the features....but of what??A class or an interface.But what are the features???The variables and the method the class or interface contains.
Inheritance takes the principle of re usability.Using the previously written code for a class without rewriting it again.
Just extend the class,that's it.The class which is taking the feature of another class is called child class and the class from which features are taken is called parent or base class.
It per the rules of humanity that child cannot be born parents,thus child class constructor is executed after the parent class constructor by default(discussed later).
Remember single class can extend single class through inheritance.
Requirement
No one likes to rewrite the code again and again,so inheritance comes to the rescue.Suppose for example there is class named "Mammal" which contains all the data(methods and variables) of mammals.If there is a requirement of creating a class "Bat"(which is a mammal),separate code has to be written for the "Bat" class again which contains same properties of a mammal.So,why not inherit mammal properties instead.That's what inheritance does.

With inheritance you can:
  1. Use the methods,variables of the base class
  2. Change the method,variable definition of the base class within the child class,this does not change the code of base class but we can add some features to the child class.

Use
Where to use inheritance is the question.For a large project with tons of classes,there is a need for proper management of code and increasing decreasing memory usage.You dont have to change the previously written code whenever any change occur(for better understanding of inheritance please read Head first java 2ed).
Implementation
Looking at the types of inheritance,types of inheritance are:
  1. Single inheritance
  2. Multiple inheritance(through interfaces)
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance



  1. Simple inheritance:Single base class and single child class.
  2. Multiple inheritance:Multiple base class and single child class.Due to ambiguity of data(confusion) which data to inherit,java does not allow multiple inheritance directly but can be achieved through interfaces.
  3. Multilevel inheritance:The child class is base class for other class/es,i.e. sandwich of classes(yummy). 
  4. Hierarchical inheritance:Single base class and multiple child classes.
  5. Hybrid inheritance:Combination of multilevel and multiple inheritance.
The implementation is quite simple.
1.Simple inheritance:
class Base_class{
//variables and method declaration
int some_var=10;
int some_var2=5;
 void method1()
 {
 //some code for method
 }
}
class Child_class extends Base_class
{
public static void main(String args[])
{
Child_class child1=new Child_class();//object declaration for child class
System.out.println(child1.some_var);//accessing variable of base class and printing in console
child1.method1();//calling method that is defined in base class,how cool is that!!
}
}
2.Multilevel inheritance:
class Base_class{
//variables and method declaration
int some_var=10;
int some_var2=5;
 void method1()
 {
 //some code for method
 }
}
class Base_class_for_lower_class{
//variables and method declaration
 void method2()
 {
 //some code for method
 }
}

class Child_class extends Base_class_for_lower_class
{
public static void main(String args[])
{
Child_class child1=new Child_class();//object declaration for child class
System.out.println(child1.some_var);//accessing variable of base class and printing in console
child1.method1();
child1.method2();
//calling method that is defined in base class and super class.
}
}

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Exceptions and exceptional handling

Lists in java