Recent Posts

Saturday, 23 July 2016

OOPS Concepts Tutorial Part 2



8. OverLoading
     Two methods are said to be overloaded iff the method names are same, But arguments are different(Atleast Order) Lack of overloading increases complexity of the program in the case of ‘c’ language.
      For the same requirement we should maintain different method names if arguments are same
In C
abs(int i) -> only for int arguments.
fabs(float f) -> only for float arguments.
labs(long l) -> only for long arguments.
      But in java two methods with the same name is allowed even though arguments are different.
abs(int i)
abs(float f)
abs(long l)
E.g
class Test {
   public void m1() {
      System.out.println("no-args");
   }
   public void m1(int i) {
      System.out.println("int-args");
   }
   public void m2(double d) {
      System.out.println("double-args");
   }
   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
      t.m1(10);
      t.m1(10.5);
   }
}
     The overloading method resolution is the responsibility of compiler based on reference type and method arguments. Hence overloading is considered as compile-time polymorphism or EarlyBinding.
Automatic promotion in overloading
E.g
class Test {
   public void m1() {
      System.out.println("no-args");
   }
   public void m1(int i) {
      System.out.println("int-args");
   }
   public void m1(float f) {
      System.out.println("float-args");
   }
   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
      t.m1(10);
      t.m1(10l);
      t.m1(10f);
      t.m1('a');
      t.m1(10.5);
   }
}
     In the case of overloading if there is no method with the required argument then the compiler won’t raise immediately compile time error. First it will promote arguments to next level and checks is there any matched method with promoted arguments, if there is no such method compiler will promote the argument to the next level and checks for the matched method. After all possible promotions still the compiler unable to find the matched method then it raises compile time error.
E.g
class Test {
   public void m1(String s) {
      System.out.println("String Version");
   }
   public void m1(Object o) {
      System.out.println("Object Version");
   }
   public static void main(String arg[]) {
      Test t = new Test();
      t.m1("raju"); -> String Version
      t.m1(new Object()); -> Object Version
      t.m1(null);
   }
}
     In the case of overloading the more specific version will get the chance first.
class Test {
   public void m1(String s) {
      System.out.println("String Version");
   }
   public void m1(StringBuffer sb) {
      System.out.println("StringVersion");
   }
   public static void main(String arg[]) {
      Test t = new Test();
   t.m1("raju");
   t.m1(null);
   }
}
     So it produces Ambiguity error.

9. Overriding
     What ever the parent has by default available to the child class through inheritance, If the child class is not satisfied with the parent class implementation then the child is allowed to overwrite that parent class method to provide it’s own specific implementation, this concept is nothing but “overriding”.
E.g
class P {
   public void property() {
      System.out.println("Land, gold, cash");
   }
   public void mary() {
      System.out.println("Subbalakshmi");
   }
}
class C extends P {
   public void mary(){
      System.out.println("Priyanka");
   }
}
class Test {
   public static void main(String[] args) {
      P p1 = new P();
      p1.mary(); //Subbalakshmi
      C c1 = new C();
      c1.mary(); //Priyanka
      P p2 = new C();
      p2.mary(); //Priyanka
   }
}
     Overriding method resolution will take care by JVM based on runtime Object. Hence overriding is considered as runtime polymorphism or dynamic polymorphism or latebinding. The process of overriding method resolution is also known as “dynamic method dispatch".
Rules for overriding
1. The method names and arguments(including order) must be same. i.e signatures of the methods must be same.

2. In overriding the return types must be same but this rule is applicable only until 1.4 version but from 1.5 version onwards co-variant return types also allowed. i.e the child class method return type
need not to be same as parent class method return type it’s child class also allowed.
E.g
Parent : Object(covariant returntype).
Child : String, StringBuffer, Object….. // Fine

Parent : String
Child : Object  // C.E
     Because String to Object is not a covariant.

Parent : double
Child : int // C.E
Because co-variant return types are not applicable for primitive types.

3. final method can’t be overridden in child classes. Private methods are not visible in the child classes. Hence they won’t participate in overriding. Based on our requirement we can take exactly same declaration in child class, But It is not overriding.
E.g
class P {
   private void m1() {
   }
}
class C extends P {
   private void m1() {
   }
}
     Here it is not overriding child class specific method
4. native methods can be overridden as non-native, similarly we can override abstract methods, synchronized methods. We can override a non-abstract method as abstract also.

5. while overriding we are not allowed o decrease access privileges. Otherwise compile time error but we can increase access privileges.

6. while overriding the size of checked exceptions we are not allowed to increase in throws class but we can decrease the size of checked exceptions. But there are no restrictions on unchecked exceptions.

Overriding in static methods
     A static method can’t be overridden as non-static
E.g
class P {
   public static void m1() {
   }
}
class C extends P {
   public void m1() {
   }
}

     Similarly a non-static method can’t be overridden as static method.
    If both parent and child class methods are static then there is no compile time error or run time error it seems that overriding is happened but it is not overriding this concept is called “method hiding”. All the rules of method hiding are exactly similar to overriding, except both methods declared as static.

   In the case of method hiding method resolution will take care by compiler based on reference type(But not runtime object).
E.g
class P {
   public static void m1() {
      System.out.println("parent method");
   }
}
class C extends P {
   public static void m1() {
      System.out.println("child method");
   }
}
class Test {
   public static void main(String arg[]) {
      P p = new P();
      p.m1(); //parent method
      C c = new C();
      c.m1(); //child method
      P p1 = new P();
      p1.m1(); //parent method
   }
}
     In the case of method hiding the method resolution will take care by compiler based on reference type. Hence method hiding is considered as ‘static polymorphism’ or ‘compile time polymorphism’ or ‘early binding’.

Overriding in the case of Variable
     Overriding concept is not applicable for variables. And it is applicable only for methods. Variable resolution always takes care by compiler based on reference type.
E.g
class P {
   int i = 888;
}
class C extends P {
   int i = 999;
}
class Test {
   public static void main(String arg[]) {
      P p = new P();
      System.out.println(p.i); //888
      C c = new C();
      System.out.println(c.i); //999
      P p1 = new C();
      System.out.println(p1.i); //888
   }
}

Next Tutorial  OOPS Concepts Tutorial Part 3

Previous Tutorial  OOPS Concepts Tutorial Part 1

No comments:

Post a Comment