Showing posts with label polymorphism. Show all posts
Showing posts with label polymorphism. Show all posts

Sunday, April 9, 2017

Overloading - Access Level

In the scenario of method overloading. The access modifier of methods can be less restrictive or more accessible. There are two methods eat() and sleep(). One is public and other is protected in class P. You see there are restrictive and more accessible version of methods.

class P
{
  public void eat() {}
  protected void eat(int a) {}
 
  protected void sleep() {}
  private void sleep(String s) {}
  public void sleep(String s, float x) {}
 
}

public class OverloadingAccessTest
{
  public static void main(String args[])
  {
 
  }
}

Overriding - Access Level

When method is overriding in subclass the access modifier should be taken into consideration otherwise there will be compilation error. The rule is that access level of overriding methods should not be restrictive than the overridden method in parent class.

In this example we have method eat() with public access modifier but subclass overriding version defined with protected which is restrictive than public. Method sleep() with protected access modifier but subclass overriding version defined with private which is also restrictive than protected. So it violates to the overriding rule.

class X
{
  public void eat() {}
  protected void sleep() {}
}

class Y extends X
{
  protected void eat() {}
  private void sleep() {}
}

public class OverridingAccessTest
{
  public static void main(String args[])
  {
  
  }
}

Results:
OverridingAccessTest.java:9: error: eat() in Y cannot override eat() in X
  protected void eat() {}
                 ^
  attempting to assign weaker access privileges; was public

OverridingAccessTest.java:10: error: sleep() in Y cannot override sleep() in X
  private void sleep() {}
               ^
  attempting to assign weaker access privileges; was protected
2 errors

Overriding - Return Types

Method Overriding happens when method signatures are the same for subclass and its superclass. Return types should be either the same or return type of subclass is compatible with its superclass. Compatible means return type of overriding method is subtype of its overridden method.

Dog is subclass of Canine. Canine has two methods and Dog inherits these two methods. You can see methodA has same method signature as well as return type. For method getType the return type is Dog and Canine. Because Dog is subclass of Canine. So overriding method can use subclass as return type. Otherwise it raises compilation error of "return type is not compatible with....".

class Canine
{
  public Canine getType(Canine d)
  {
    return d;
  }
  
  public float methodA(int a, int b, float c)
  {
 return c*c;
  }
}

class Dog extends Canine
{
  public Dog getType(Dog d)
  {
    return d;
  }

  public float methodA(int a, int b, float c)
  {
 return a*b;
  }
}

public class OverriddingTest1
{
  public static void main(String args[])
  {
     Canine c1 = new Canine();
     Dog d1 = new Dog();
     Canine c2 = new Dog();
     System.out.println("Canine c1: "+c1.getType(c1).getClass());
     System.out.println("Dog d1: "+d1.getType(d1).getClass());
     System.out.println("Dog c2: "+c2.getType(c2).getClass());

     System.out.println("\nd1: "+d1.methodA(4,8,2.2f));
     System.out.println("d1: "+d1.methodA(5,9,3.2f));
  }
}

Results:
Canine c1: class Canine
Dog d1: class Dog
Dog c2: class Dog

d1: 32.0
d1: 45.0

Saturday, April 8, 2017

Overloading - Argument Lists

In this section we will talk about the argument lists in method overloading. Method overloading can be achieved if method signature is different. It can be different number of parameters, data types or sequence but return type doesn't matter. In our example we created a program for some simple calculation.

method 1: two parameters (two int)
method 2: three parameters (three int)
method 3: two parameters (one int, one float)
method 4: two parameters (one int, one float) but in different sequence

class A
{
   public static int method1(int x, int y)  {return x+y;}
   public static int method2(int x, int y, int z)  {return x+y+z;}
   public static float method3(int x, float y)  {return y-x;}
   public static float method4(float x, int y)  {return x-y;}
}

public class OverloadingTest1
{
   public static void main(String args[])
   {
  System.out.println("method 1: "+A.method1(5,9));
  System.out.println("method 2: "+A.method2(4,7,2));
  System.out.println("method 3: "+A.method3(5,14.2f));
  System.out.println("method 4: "+A.method4(7.8f,2));
   }
}

Result:
method 1: 14
method 2: 13
method 3: 9.2
method 4: 5.8

Polymorphism Concepts

Polymorphism is one of main four concepts in Java. The word polymorphism means many forms. That means it has many different kinds of forms. There are two types of polymorphism: dynamic and static. Actually it has many ways to describe polymorphism but in fact it refers to method overridding and overloading concepts.

What is method overriding and overloading ?

Method overridding: There are two methods with same name as they share same method signature. Method signature includes data types, number, sequence and return type. Technically speaking method overridding can have different return types but it should obey some rules that return type of overridding method should be the same or a subtype of its overridden method in parent class.

Method overloading: Method overloading is similar to method overridding. It has same method name but different method signature. Return type doesn't matter for method overloading, It can be same or different.

Polymorphism Comparison


Below list summarizes differences between method overidding and overloading.

Argument list
Overriding: Same method name as well as same data type, number, sequence
Overloading: Same method name but different method signature (data type, number, sequence)

Return type
Overriding: Same or compatible as subtype
Overloading: Same or different (doesn't matter)

Polymorphism type
Overridding: Runtime, object type (not reference variable type) determines which methods should be overridden at runtime
Overloading: Compile time

Binding
Overridding: dynamic => same method signatures make compiler confuse
Overloading: static => private, static and final are restricted to local class scope

Applies to
Overridding: inherited and abstract methods, static methods can be redeclared in subclass but nothing to do with static method in parent class
Overloading: in the same class or subclass, constructor, private, static and final methods

Exceptions
Overridding: can throw any unchecked exceptions. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
Overloading: can throw different exceptions

Performance
Overridding: Because done by runtime
Overloading: Better

Class Requirement
Overridding: Two classes (superclass and subclass)
Overloading: within one class

Access Level
Overridding: cannot be restrictive than overridden method of parent class
Overloading: can be restrictive or more accessible

Abstraction vs Interface

Abstract Interface method abstract and concrete abstract only, support default and static method ...