简体   繁体   中英

How to stop child class object invoking method from parent class despite same method also being in child class

I have an object of type ElectronicProduct which extends Product, I try to invoke the loadFromFile() method from within the child class, giving the exact name and argument types, yet instead the child class object invokes the loadFromFile() from the parent class. How do I stop this?

The code is as follows:

ElectronicProduct eProduct = new ElectronicProduct();
eProduct.loadFromFile(eProduct,bin); 

however it invokes the method in parent class below:

public Product loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{ 

   //some code 

}

instead of the child class shown below:

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{

// Some code

}

I did try using @Override in the child class method, but because one returns a product and the other returns an electronicProduct the override does not seem to work. Any help would be greatly appreciated.

EDIT

This is very strange as now it has started working as I wanted it to, without changing anything. Different scenario but a similar thing happened before where it gives an error warning, i delete the code and write the exact same thing, and then it works. I do not want this to happen randomly as it is a graded assignment. Do not want to go off topic but it must be related. Please advise if anyone knows what has just happened?

EDIT*2

In response to comment from Tiffado, the reason a Product object is passed in and not an ElectronicProduct object, is because within the child class method, the Product object is passed to the parent class. If I instead pass in a child class object into the child method which then passes the object to a parent method it may not work, as I have learned that parent classes do not 'see' child classes. Please correct me if this is incorrect.

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{


ElectronicProduct eProduct = new ElectronicProduct();

eProduct = (ElectronicProduct)super.loadFromFile(aProduct,bin);

eProduct.setGuarunteeDuration(Integer.valueOf(bin.readLine()));

return eProduct;

}

There is no way for the compiler to know the difference between your two methods: You need to specify different parameter ordering at the method signature, number of parameters, but no subclass parameter types or subclass return types will make a difference:

The method:

public Product loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{ 

Is just the same as

public ElectronicProduct loadFromFile(Product aProduct, BufferedReader bin) throws IOException, ParseException{

since the return type (ElectronicProduct) is a subclass of Product, so it is not part of the method signature to use polymorphism.

See also: "Java method signatures only take method name and number and type of parameters", available at: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Please see the next example, which, by overriding rules, will call the child method, but since there's a hardcoded call to the parent , it will also call the parent:

public class Product {

  static int productIdentifier = 0;
  static int electronicProductIdentifier = 0;

    public Product loadFromFile( Product aProduct ) {

    System.out.println("Call loadFromFile from Product");
        Product product = new Product();
        return product;

    }

  public Product ()  {
    System.out.println("Creating a Product with ID " + 
            ( ++ productIdentifier ) );
  }

}


public class ElectronicProduct extends Product {

    public ElectronicProduct loadFromFile( Product aProduct ) {

    System.out.println("Call loadFromFile from ElectronicProduct");
        ElectronicProduct eProduct = new ElectronicProduct();
    super.loadFromFile( eProduct ); // Hardcoded call to parent!
        return eProduct;

    }

  public ElectronicProduct ()  {
    System.out.println("Creating an ElectronicProduct with ID " + 
            ( ++ electronicProductIdentifier ) );
  }

}

Product p = new Product();
ElectronicProduct ep = new ElectronicProduct();
ep.loadFromFile( ep );

Output:

Creating a Product with ID 1
Creating a Product with ID 2
Creating an ElectronicProduct with ID 1
Call loadFromFile from ElectronicProduct
Creating a Product with ID 3
Creating an ElectronicProduct with ID 2
Call loadFromFile from Product
Creating a Product with ID 4

(There's a call of loadFromFile() from product, since it is being explicitly called from ElectronicProduct.loadFromFile() )

If you comment out the line calling to super.loadFromFile() , you'll see no call to Product.loadFromFile() :

Creating a Product with ID 1
Creating a Product with ID 2
Creating an ElectronicProduct with ID 1
Call loadFromFile from ElectronicProduct
Creating a Product with ID 3
Creating an ElectronicProduct with ID 2

Maybe you should use a method like this one :

public ElectronicProduct loadFromFile(ElectronicProduct aProduct, BufferedReader bin) throws IOException, ParseException{

Since the main difference is the object you put in the parameters, you should specify it.

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