Home > database >  How does the program decides which method to run because the object is of child class [closed]
How does the program decides which method to run because the object is of child class [closed]

Time:09-18

So I have the following code:

import java.util.Scanner;

class Shape{
   double length, breadth;

   Shape(double l, double b) { //Constructor to initialize a Shape object  
      length = l;
      breadth = b;
   }

   Shape(double len) { //Constructor to initialize another Shape object  
      length = breadth = len;
   }

   double calculate(){ // To calculate the area of a shape object
      return length * breadth ;
   }
}

public class Test1 extends Shape {
   double height;

   Test1(double l, double h) {
      super(l);
      height = h;
   }

   Test1(double l, double b, double h) {
      super(l, b);
      height = h;
   }
   @Override
   double calculate(){
      return length*breadth*height;
   }   

   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
       double l=sc.nextDouble();
       double b=sc.nextDouble();   
       double h=sc.nextDouble(); 
    
       Test1 myshape1 = new Test1(l,h);
       Test1 myshape2 = new Test1(l,b,h);
    
       double volume1;
       double volume2;
    
       volume1 = myshape1.calculate();
       volume2 = myshape2.calculate();
    
       System.out.println(volume1);
       System.out.println(volume2);
   }
}

and I don't understand how does it decides which of the calculate() method to run because both of them are called from child class object yet one of them decides to run parent class method.

Is it related to constructor overloading? If yes how?

CodePudding user response:

Constructor overloading has nothing to do with "which method runs". Constructors are only used to initialize the instance, and "which method runs" may be a question related to method overloading, which is not the case in your question.

In both of cases:

volume1 = myshape1.calculate();
volume2 = myshape2.calculate();

the lowest available implementation of calculate() in the Test1 ... -> java.lang.Object class hierarchy, is invoked - that is Test1::calculate, in your case.

You do not invoke the superclass's calculate(), but rather, your class's calculate() uses fields inherited from the superclass Shape, as:

double calculate(){
      return length*breadth*height;
}

When you instantiate class, it's created with all the members (even private) of its superclasses, and that's why you use the fields of superclasses, as if they were defined in the class in question.

Side-note: private members of the superclass, are not directly accessible, in the subclass. You need appropriate accessors/getters for accessing them.

CodePudding user response:

If the method is not private, static or final, then the method is virtual by default.

That is, the method of the lowest class will be used.
You have overridden the method in Test1, so it will be called. I advise you to write @Override over a method to show yourself and others that this method is inherited.

  • Related