• Top Posts

    Polymorphism and Abstraction in Java

    Polymorphism
    Polymorphism is a object oriented programming feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method animalSound(), here we cannot give implementation to this method as we do not know which Animal class would extend Animal class. So, we make this method abstract like this:
    public abstract class Animal{
      ...
      public abstract void animalSound();
    }
    Now suppose we have two Animal classes Dog and Lion that extends Animal class. We can provide the implementation detail there.
    public class Lion extends Animal{
    ...
       @Override
       public void animalSound(){
           System.out.println("Roar");
       }
    }
    and
    public class Dog extends Animal{
    ...
       @Override
       public void animalSound(){
           System.out.println("Woof");
       }
    }
    As you can see that although we had the common action for all subclasses animalSound() but there were different ways to do the same action. This is a perfect example of polymorphism (feature that allows us to perform a single action in different ways).
    Types of Polymorphism
    1) Static Polymorphism
    2) Dynamic Polymorphism
    Static Polymorphism:
    Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading can be considered as static polymorphism example.
    Method Overloading: This allows us to have more than one methods with same name in a class that differs in signature.
    class DisplayOverloading
    {
       public void disp(char c)
       {
            System.out.println(c);
       }
       public void disp(char c, int num)  
       {
            System.out.println(c + " "+num);
       }
    }
    public class ExampleOverloading
    {
      public static void main(String args[])
      {
          DisplayOverloading obj = new DisplayOverloading();
          obj.disp('a');
          obj.disp('a',10);
      }
    }
    Output:
    a
    a 10
    When I say method signature I am not talking about return type of the method, for example if two methods have same name, same parameters and have different return type, then this is not a valid method overloading example. This will throw compilation error.
    Dynamic Polymorphism
    It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather, thats why it is called runtime polymorphism.
    Example
    class Animal{
      public void animalSound(){
    System.out.println("Default Sound");
      }
    }
    public class Dog extends Animal{

      public void animalSound(){
    System.out.println("Woof");
      }
      public static void main(String args[]){
    Animal obj = new Dog();
    obj.animalSound();
      }
    }
    Output:
    Woof
    Since both the classes, child class and parent class have the same method animalSound. Which of the method will be called is determined at runtime by JVM.
    Few more overriding examples:
    Animal obj = new Animal();
    obj.animalSound();
    // This would call the Animal class method

    Dog obj = new Dog();
    obj.animalSound();
    // This would call the Dog class method

    Animal obj = new Dog();
    obj.animalSound();
    // This would call the Dog class method

    Abstraction
    Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when you login to your bank account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to server, how it gets verified is all abstracted away from the you.

    Abstraction in java is achieved by using interface and abstract class. Interface give 100% abstraction and abstract class give 0-100% abstraction.

    Encapsulation
    Encapsulation simply means binding object state(fields) and behavior(methods) together. If you are creating class, you are doing encapsulation.
    Encapsulation example in Java
    How to
    1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
    2) Have getter and setter methods in the class to set and get the values of the fields.


    class EmployeeCount
    {
      private int numOfEmployees = 0;
      public void setNoOfEmployees (int count)
      {
          numOfEmployees = count;
      }
      public double getNoOfEmployees ()
      {
          return numOfEmployees;
      }
    }
    public class EncapsulationExample
    {
      public static void main(String args[])
      {
         EmployeeCount obj = new EmployeeCount ();
         obj.setNoOfEmployees(5613);
         System.out.println("No Of Employees: "+(int)obj.getNoOfEmployees());
       }
    }
    Output:
    No Of Employees: 5613

    The class EncapsulationExample that is using the Object of class EmployeeCount will not able to get the NoOfEmployees directly. It has to use the setter and getter methods of the same class to set and get the value.

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728