Powered by Blogger.

Best Example of Encapsulation, and Inheritance in Java

Overriding Methods:-

If a method is defined in a sub-class so that the name, return type, and argument list must exactly those of a method in the parent class, then the new method is said to override the old one. The overriding method can not be less accessible than the method it overrides.

Overloading Methods:-

We can declare several methods of same name in a class, which is known as methods overloading. For overloading methods argument lists must be different and return type can be different. In the following code, there are four methods of same name with different argument, which is an example of overloading methods.

Invoking an Overridden Method:-

The parent keyword can be used with any method that overrides its counterpart in a parent class. When you override a method, you may not wish to obliterate the functionality of the parent but rather extend it. You can achieve this by calling the parent class’s method in the current object’s context. Let’s explore two examples which will clear the concept of obliteration and extension of parent method.

Invoking Overridden Super-class Methods:-

Sometimes within a class you need to invoke an overridden superclass method instead of invoking a method that has the same signature and is defined in the current class.

For example, suppose that the CheckingAccount class overrides the debit instance method defined in its immediate superclass, Account. You could invoke the Account debit method within a method in the CheckingAccount class by coding this statement:

Example with overriding, overloading, and Invoking:

 class Man{
   private int weight;

   public Man(int weight){
      this.weight=weight;
   }

   public void setWeight(int weight){   // seter method
      if(weight>=50 && weight<=100){
         this.weight=weight;
      }
   }

   public int getWeight(){   // geter method
      return weight;
   }

   public void show(){
      System.out.println(weight);
   }
  }

  public class SuperMan extends Man{
    public int power;

    public SuperMan(int power){
       super(55);    // invoking parent class constructor
       this.power=power;
    }

    public SuperMan(int weight,int power){    //Over loading
       super(weight);     // invoking parent class constructor
       this.power=power;
    }

    public void show(){     // overriding method
       super.show();          // invoking parent class method
       System.out.println(power);
    }

    public void show(String abc){     // overloading method
       System.out.println(abc);
    }

    public static void main(String[]args){
       SuperMan s = new SuperMan(10);
       s.show();
       SuperMan s1 = new SuperMan(50,100);
       s1.show(”Tiger”);
    }
  }

1 comment:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete