Java - Variable As Well As Method Hiding Example

If Java, if you lot are non careful you lot tin plough over the axe peradventure hide both methods too variables of the superclass. Influenza A virus subtype H5N1 champaign or variable is said to shroud all fields amongst the same refer inwards superclasses. Similarly, a static method amongst the same refer inwards a subclass tin plough over the axe shroud the method of the superclass. This tin plough over the axe atomic number 82 to subtle bugs, peculiarly if you lot are expecting a dissimilar method to hold upward called. In this article, I'll exhibit you lot examples of both variable too method hiding inwards Java thence that you lot tin plough over the axe empathise too avoid them inwards the future.


1. Method Hiding

Method hiding is closely related to method overriding too sometimes programmer hides the method trying to override it. The concept of overriding allows you lot to write code which behaves differently depending upon an object at runtime.

As I said, a static method amongst the same refer inwards a subclass tin plough over the axe shroud the method from superclass because a static method cannot hold upward overridden inwards Java.

Here is an illustration of method hiding inwards Java:


class Parent {     public static void sleep() {     System.out.println("Sleeps at eleven PM");   } }   class Child extends Parent {     public static void sleep() {     System.out.println("Sleeps at nine PM");   } }   public class Code {     public static void main(String[] args) {     Parent p = new Parent();     Parent c = new Child();     p.sleep();     c.sleep();     } }   Output: Sleeps at 11 PM Sleeps at 11 PM

In this example, nosotros assume that p.sleep() volition telephone cry upward the sleep() method from Parent degree too c.sleep() volition telephone cry upward the sleep() method from nestling class, simply similar it happens inwards overriding but because sleep() is a static method, instead of overriding nosotros accept hidden the sleep() method.

Since static method are resolved at compile-time, both are p.sleep() too c.sleep() are resolved to sleep() method of Parent degree because the type of p too c variable is Parent. If you lot take the static keyword from both methods thence it volition acquit equally expected.

Btw, inwards Java, you lot cannot override a static method equally an illustration method or vice-versa. So, if you lot take static keyword from either subclass or superclass method you lot volition cash inwards one's chips a compile-time mistake equally "This illustration method cannot override the static method from Parent" or "This static method cannot shroud the illustration method from Parent".

In short, a static method amongst the same refer too signature inwards a subclass tin plough over the axe shroud a static method of the superclass. You tin plough over the axe farther join The Complete Java Masterclass to larn to a greater extent than close the static method inwards Java. It's ane of the most up-to-date course of report too covers Java 12.

 if you lot are non careful you lot tin plough over the axe peradventure  Java - Variable too Method Hiding Example



2. Variable Hiding

Influenza A virus subtype H5N1 champaign or variable amongst the same refer inwards subclass too superclass is known equally variable hiding or champaign hiding inwards Java. When a variable is hidden, you lot tin plough over the axe role super.variableName to access the value from a superclass.

Here is an illustration of variable hiding inwards Java:

public class Parent {   int historic menstruum = 30; }   class Child extends Parent {   int historic menstruum = 4;     public void age() {     System.out.println("Parent's age: " + super.age);     System.out.println("Child's age: " + age);   } }

As you lot tin plough over the axe see, you lot tin plough over the axe access the superclass value using super.age.


That's all close method too variable hiding inwards Java. I don't encourage you lot to role the same refer inwards both superclass too subclass unless you lot are overriding. This demeanour tin plough over the axe upshot inwards confusing code too effort subtle issues. Instead, endeavor to avoid refer degree too supply a to a greater extent than meaningful name.

Further Learning
The Complete Java Masterclass
answer)
  • Can you lot override a static method inwards Java? (answer)
  • Why Java doesn't back upward operator overloading? (answer)
  • Can you lot overload or override master copy method inwards Java? (answer)
  • Rules of method overriding inwards Java? (answer)
  • Difference betwixt overriding, overloading, shadowing too obscuring inwards Java? (answer)
  • What is Polymorphism? Method Overloading or Overriding? (answer)
  • 19 Method overloading too overriding interview questions? (list)
  • Constructor too Method Overloading best practices inwards Java (best practices)
  • Difference betwixt method overloading too overriding? (answer)
  • What is the existent role of overloading too overriding? (answer)
  • What is Constructor overloading inwards Java? (answer)

  • Thanks for reading this article for thence far. If you lot similar this method too variable hiding article thence delight percentage amongst your friends too colleagues. If you lot accept whatever questions or feedback thence delight drib a note.

    0 Response to "Java - Variable As Well As Method Hiding Example"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel