Wednesday, August 10, 2016

Method Overriding

When we define a method in a derived class where name and the argument exactly same with in parent class, then we already override method in parent class. If we invoke method that has been overridden by the object of the derived class, then the method that will be executed is the method contained in the derived class, not in contained in the parent class. In other words, if we do override the method in the parent class, then the method in the parent class will be hidden in derived class. Consider the following code examples:

class A {

   private int a;

   public void setA(int value) {
      a = value;
   }

   public int getA() {
      return a;
   }

   public void showValue() {
      System.out.println("Value a : " + getA());
   }
}

class B extends A {

   private int b;

   public void setB(int value) {
      b = value;
   }

   public int getB() {
      return b;
   }

   //override method showValue() in class A
   public void showValue() {
      System.out.println("Value b : " + getB());
   }
}

class DemoOverride1 {

   public static void main(String[] args) {

      B obj = new B();

      obj.setA(100);
      obj.setB(200);

      //will call method in class B
      obj.showValue();
   }
}

The result that will be given by the above program as follows:

Value b : 200

As shown in the above result that when we create object of class B, and we call method showValue() through the object, then the method showValue() will be called is method showValue() in class B. Method showValue() contained in class A will be hidden. However, in certain cases, sometimes we also need to call the method contained in the class A. To overcome this, we can use keyword super, as shown in the following code:

class A {

   private int a;

   public void setA(int value) {
      a = value;
   }

   public int getA() {
      return a;
   }

   public void showValue() {
      System.out.println("Value a : " + getA());
   }
}

class B extends A {

   private int b;

   public void setB(int value) {
      b = value;
   }

   public int getB() {
      return b;
   }

   //override method showValue() in class A   
   public void showValue() {
      super.showValue(); //call method in class A
      System.out.println("Value b : " + getB());
   }
}

class DemoOverride2 {

   public static void main(String[] args) {

      B obj = new B();

      obj.setA(100);
      obj.setB(200);

      //will call method in class B
      obj.showValue();
   }
}

This time, the program will provide the following result:

Value a: 100
Value b: 200

Why? Because, in method showValue() that contained in class B, explicitly we also invoke method showValue() contained in class A.

3 comments:

  1. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete
  2. I took a break from Java programming, but recently started to interview again for Java roles. This blog has been very helpful in refreshing my Java knowledge. I only regret that I didn't have this page when I first started learning Java years ago. Great Job! Thank you.

    ReplyDelete