Rajinder Menu

Overriding in Java



Overriding is related to methods of superclass and subclass.

When a subclass provide a method with same signature as that of some method in superclass then we say that subclass method overrides the superclass method. The signature here means the name of method, the number and type of its parameters and the return type of the method. For example


class Parent{

 public void setName(String name) {
   System.out.println("This method is in Parent class");
   System.out.println("The name is:"+name);
 }

}

public class Child extends Parent{

  //Overrides the setName() method in Parent class
  public void setName(String name) {

    System.out.println("This method is in Child class");
    System.out.println("The name is:"+name);
  }

  public static void main(String[] args) {

    Child childObject= new Child();
    childObject.setName("Sandy");
  }
}

Output:
This method is in Child class
The name is:Sandy

Note that here in this case always subclass version of the method or method which has overrided is called not the superclass version of the method as also apparent from the output.


Overriding of Static Method

If a subclass provides a method with same signature as that of some static method in superclass then we say subclass static method hides the superclass static method. In case of static methods we do not say that it overrides the static method of superclass. For example

class Parent{

 public static void setName(String name) {
  System.out.println("This method is in Parent class");
  System.out.println("The name is:"+name);
 }
}


public class Child extends Parent{

  //Hides the static setName() method in Parent class
  public static void setName(String name) {

    System.out.println("This method is in Child class");
    System.out.println("The name is:"+name);
  }

  public static void main(String[] args) {

    Child childObject= new Child();
    Parent.setName("Sandy"); //calls Parent version of static method
  }
}

Output:
This method is in Parent class
The name is:Sandy

The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass name or the subclass name.

For Example if we use

Child.setName("Sandy");

instead of

Parent.setName("Sandy");

then it will call Child version of static method and output would be:

This method is in Child class
The name is:Sandy


@Override annotation

When a method in subclass overrides a method in superclass then we can annotate the subclass method with @Override annotation. This annotation tells the compiler that the annotated method i.e. method on which this annotation is applied is overriding some method in superclass. This annotation is not required but if some method in subclass is annotated with this annotation and no such method with same signature is present in superclass then compiler will generate error.

For example

class Parent{

  public void setName(String name) {
    System.out.println("This method is in Parent class");
    System.out.println("The name is:"+name);
  }
}

public class Child extends Parent{

   @Override
   //Overrides the setName() method in Parent class
   public void setName(String name) {
     
    System.out.println("This method is in Child class");
    System.out.println("The name is:"+name);
   }

   public static void main(String[] args) {

    Child childObject= new Child();
    childObject.setName("Sandy");
   }
}

If we delete setName(String) method from superclass then above code will give compile time error.

@Override annotation cannot be applied to static methods in subclass because static method in subclass hides the static method with same signature in superclass not overrides it. If you do it will give compile time error.


Access Modifiers and Overriding

The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass.
For Example

class Parent{

  public void setName(String name) {
    System.out.println("This method is in Parent class");
    System.out.println("The name is:"+name);
  }
}

public class Child extends Parent{

  @Override
  //Overrides the setName() method in Parent class
  protected void setName(String name) { //this line will give //compile time error

   System.out.println("This method is in Child class");
   System.out.println("The name is:"+name);
  }

  public static void main(String[] args) {
  
    Child childObject= new Child();
    childObject.setName("Sandy");
  }
}

Above code will give compile time error because we have changed the access of setName() method in subclass to less restrictive i.e. from public in Parent class to protected in Child class.

And also you will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass, and vice versa.

I would like to know your comments and if you liked the article then please share it on social networking buttons.




No comments:

Post a Comment