简体   繁体   中英

Can someone explain why I have to instantiate subclass?

Why do you have to instantiate subclass() in the mymethod()? I thought that we only need to make objects in the main() method.(which is done here too) Why do we have to do it twice? Is there a special property of inheritance here?

class Super_class {
   int num = 20;

   // display method of superclass
   public void display() {
      System.out.println("This is the display method of superclass");
   }
}

public class Sub_class extends Super_class {
   int num = 10;

   // display method of sub class
   public void display() {
      System.out.println("This is the display method of subclass");
   }

   public void my_method() {
      // Instantiating subclass - why do you have to do this?
      Sub_class sub = new Sub_class();

      // Invoking the display() method of sub class
      sub.display();

      // Invoking the display() method of superclass
      super.display();

      // printing the value of variable num of subclass
      System.out.println("value of the variable named num in sub class:"+ sub.num);

      // printing the value of variable num of superclass
      System.out.println("value of the variable named num in super class:"+ super.num);
   }

   public static void main(String args[]) {
      Sub_class obj = new Sub_class();
      obj.my_method();
   }
}

You don't have to do these things you've done on the subclass. When you're inside the class you don't need to instantiate it to call its attribute and methods: you can do it directly, so doing like this is enough:

  public void my_method() {

  // Invoking the display() method of sub class
  display();

  // Invoking the display() method of superclass
  super.display();

  // printing the value of variable num of subclass
  System.out.println("value of the variable named num in sub class:"+ num);

  // printing the value of variable num of superclass
  System.out.println("value of the variable named num in super class:"+ super.num);
  }

You do not have to do that, you can call the display method of the sub class with the this keyword or even without that inside my_method() you do not need object of sub class.

But what you have done here simply created a new object inside my_method(), so basically your my_method() method and display() methods are getting called by different object.

You don't need to instantiate a new object, and in fact, I would consider that a bad coding practice. What you need to do is reference the current object using the this keyword, or just calling display(); , which implicitly means this.display(); . If you create a new object, if there are any instance variables set, they would reset to their default values because the instance you are referencing is not the same instance the method was called on, so you could get unexpected behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM