Calling child functions with parent class

user3547560

I have a java program that simulates a store. I have a Product class with 3 child classes (Game, Book, and Movie). I have an array of Products, and I want to do something with each item in the array depending on what particular type of child class it is.

The problem I am having is that the array is an array of Products and, as such, items from it cannot invoke functions of the child classes, even though each "Product" in the array actually is one of the three child classes.

How do I fix this? I know I can put a bunch of abstract functions into the Product class that are overridden in the child classes, but I was wondering if there was an easier way of doing this.

Michael K. Sondej

If you want to use different functions for each child class you just have to use the "instanceof" operator and type casting like so:

For example somewhere in a loop:

if(items[i] instanceof Game) {
   Game g = (Game)items[i];
   g.SomeGameSpecificFunc();
} else if(items[i] instanceof Book) {
   //...
}

You can also get a string with a name of the class of current object through reflection, but this is rather slow:

String className = items[i].getClass().toString();

//in Java 7+
switch(className) {
   case "Book":
      //do something
      break;
      //etc
}

But you should use these techniques only if you really really have to use functions with different names and parameters.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get the calling parent class in a static child class

Calling parent functions from child component

Calling and linking a child and parent class together in Java

Calling child method from obj of parent class

Calling a child method in a parent class in Python

Bind child class 'this' to all parent prototype functions

Calling a parent method from a child class with a variable of that child

Calling parent class function to compare parent and child classes

Calling constructor of parent class in static method of child class

Calling a parent class constructor from a child class in python

Flutter calling child class function from parent class

Java - parent class is calling a method from a child class?

Is calling a "private" variable from a parent class (as a child class) violating Encapsulation

C++ Parent Class with Abstracted Function that Uses Functions in a Child Class

Calling overloaded parent methods from child class in D

Calling different child class function from the same parent invocation

Calling an overloaded parent's method from a child class in C++

child class get undefined value when calling parent function

python - remain in base class when calling parent method from child

Calling child method to parent class and getting error related to no attribute

Calling child variables from another generic class which generic is parent

Calling parent trait method from child class in Groovy

Calling child's overridden method from parent class

Can a parent class which is reinterpreted as a child class use the child's functions in its functions?

Traversing up a PHP class structure by calling parent functions recursively

functions - if statement for parent and child

Why is Typescript calling an overridden child method from within the parent class if I call child.listen()?

Calling `super()` in parent class

Calling parent method in class?