Rajinder Menu

Hiding in Java



Hiding is the concept which applies to both fields and methods of a class and is related to inheritance. Lets first see hiding for fields.

Hiding a field of a class means providing a field with same name in its subclass. For Example

class Parent{

  int num=10;

}

public class Child extends Parent{
  
  int num=13;
  
  public void print() {

    System.out.println("The value of num is:"+num);
  }

  public static void main(String[] args) {

    Child childObject= new Child();
    childObject.print();
  }
}

Output:

The value of num is:13

In above code there is a field named num both in superclass and subclass. We will say num in subclass is hiding the num in superclass because both have same names.

Now child class has to two nums- one from superclass having value 10 and one from subclass having value 13. But because num in subclass is hiding num in superclass the print method in subclass will print num of subclass.

For hiding, fields should have only their names same. Their data types can differ.

For example

class Parent{

  String num="ten";
}

public class Child extends Parent{
  
  int num=10;

  public void print() {

    System.out.println("The value of num is:"+num);
  }

  public static void main(String[] args) {

   Child childObject= new Child();
   childObject.print();
  }
}

Output:

The value of num is:10

Note: Hiding of fields is considered bad programming practice.


Hiding of methods

The concept hiding does not apply to the instance methods[Link]. It only applies to static methods[Link]. For instance methods the term overriding is used.

For static methods if a static method in subclass have same signature i.e. same name, equal number and type of its parameters and same return type as that of a static method in superclass then we will say that static method in subclass hides the static method in superclass.

Had it been the instance methods then we would say that instance method in subclass has overridden instance method in superclass but for static methods we use the term hiding.

For Example

class Parent{

  static int num=10;

  public static void print() {

    System.out.println("This method is in Parent class");
    System.out.println("The value of num is:"+num);
  }
}

public class Child extends Parent{

  public static void print() {

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

  public static void main(String[] args) {

    Parent.print(); //will call parent static method print()
  }
}

Output:

This method is in Parent class
The value of num is:10

Here in above code static method print() in subclass is hiding(not overriding) the static method in superclass.

The version of static method i.e. whether of Parent or Child is called depends on which class name we use to call it i.e. whether we use subclass name to call it or superclass name to call it.

If we change line

Parent.print();

to

Child.print();

It will call Child version of static method print() and output will be:

Output:

This method is in Child class
The value of num is:10

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