Friday, 31 May 2013

Inheritence in JAVA


Inheritance: Inheritance can be defined as the process where one object acquires the properties of another. 
To know the concept of inheritance clearly you must have the idea of class and its features like methods, data members, access controls, constructors, keywords this, super etc.

As the name suggests, inheritance means to take something that is already made. It is one of the most important feature of Object Oriented Programming. It is the concept that is used for re-usability purpose. Inheritance is the mechanism through which we can derived classes from other classes. The derived class is called as child class or the subclass or  we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword "extends" is used. 

With use of the extends keyword the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
************************************************************

Example1:

************************************************************ 

                          public class Inheritence1 {

                               public String name;
                              private String notAccess;
                              public String getSTring(){
                                return name;
                              }

                        }


************************************************************               

                        public class Inheritence2 extends Inheritence1 {

                          public static void main(String a[]) {

                             Inheritence2 inheritence2 = new Inheritence2();
                               inheritence2.name = "Inheritence";
                            // inheritence2.notAccess = "Inheritence";
                              System.out.println(inheritence2.getSTring());
                          }

                         }
***********************************************************
In above example Inheritence1 is BaseClass/ParentClass and Inheritence2 is SubClass/Child Class which get the properties of Inheritence1.
In above example we access the properties of parent class using child class object. Because Inheritence2  extends the Inheritence1 class.
But here private fields does not inherited by child class. In our example following line gives the error say "The field Inheritence1.notAccess is not visible".
Public fields are acquired by the child class from parent class. 

************************************************************

Example2: With more detailed explanation
public class InheritenceSample1 {

public void getName() {
System.out.println("getName of InheritenceSample1");
}

public void getName1() {
System.out.println("getName1 of InheritenceSample1");
}

}

*************************************************************
public class InheritenceSample2 extends InheritenceSample1 {

public void getName() {
System.out.println("getName of InheritenceSample2");
}

public void getClassName() {
System.out.println(" getClassName of InheritenceSample2");
}

}
*************************************************************
public class InheritenceDemo {

public static void main(String a[]) {

/**
 * we did use superclass reference to refer subclass object
 * --polymorphism
 */
InheritenceSample1 inheritenceSample1 = new InheritenceSample2(); 

  //In above line we have assigned child class object to parent class reference.

InheritenceSample2 inheritenceSample2;


 /**
   * Following line of code does not allow because getClassName is method of 
   * InheritenceSample2 but inheritenceSample1 is reference
* of InheritenceSample1
   */

  // inheritenceSample1.getClassName(); 

/**
 * Following line will call the getName() of InheritenceSample2 class
 * not InheritenceSample1. Because InheritenceSample2 extends
 * InheritenceSample1 and override the getName()method. If
 * InheritenceSample2 did not override the getName() method then it will
 * call getName() of InheritenceSample1.
 */

inheritenceSample1.getName();

/**
 * Following line will call the getName1() of InheritenceSample1 class
 * not InheritenceSample2. Because InheritenceSample2 extends
 * InheritenceSample1 and not override the getName1()method.
 */

inheritenceSample1.getName1(); 
inheritenceSample2 = new InheritenceSample2(); 
/**
 * we did not use subclass reference to refer super class object
 */

// inheritenceSample2 = new InheritenceSample1();

inheritenceSample2.getClassName();
inheritenceSample2.getName(); 
/**
 * Following line will call the getName1() of InheritenceSample1 class.
 * Because InheritenceSample2 extends InheritenceSample1.
 */

inheritenceSample2.getName1();
}
}
***************************************************************
Why Use Inheritance(Advantages)?
  1. Re-Usability: Inheritance allows programmers to re-use code they've already written.
  2. Extensibility: Extending the base class logic as per business logic of the derived class.
  3. Data hiding: Base class can decide to keep some data private so that it cannot be altered by the derived class.
  4. Overriding: With inheritance, we will be able to override the methods of the base class so that meaningful  implementation of the base class method can be designed in the derived class.
  5. With inheritance, we can create a reference to the base class, but assign a derived class object to that reference. This concept is very widely used in java.
Disadvantages:
  • Disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled. 
  • This means one cannot be used independent of each other.
  • During maintenance adding new features both base as well as derived classes are required to be  changed. If a method signature is changed then we will be affected in both cases (inheritance & composition)
  •  If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that  method.Here things can get a bit complicated in case of inheritance because our programs will still compile, but the  methods of the subclass will no longer be overriding superclass methods. These methods will become independent  methods in their own right.
*************************************************************

No comments:

Post a Comment