Assignment 1: Counting Instances of Classes


Description

This assignment deals with topics of static variables as well as inheritance. I admit, the program does have a more theoretical value. We'll write more fun programs later. I promise.

Please design 3 classes:
class 1: 'Parent'
class 2: 'Child1'
class 3: 'Child2'
Each class consists of exactly one method (apart from the constructor), the method 'getClassInfo'. This method should print "I am a parent/child1/child2", depending on its class. Additionally, this method should print how many instances of the respective class were created at the time of creation of this special instance, and how many instances exist in total (there's a little difference in here, you need to understand the concept of static and non-static variables to solve this!). Example: "I am instance number 7 of my class, there's a total of 9 instances of my class".

After you designed and implemented the classes that way, create a main program (in class 'Parent'). Create an array of 30 Parents. Fill the array with 10 instances of each class . After that, call the method 'getClassInfo' for each instance. The result should be a printout of 30 sentences, hopefully giving correct information about the objects (inheritage/polymorphism... all these things have to be understood here).

good luck!

Special note: since a Child1 (or Child2) IS A PARENT, you should theoretically increase the class-counter for Parent-instances, too, when a Child1 (Child2) is created. Please don't. Just increase the counter for Parent-instances when a (pure) Parent is created. If you understood why i wrote this note, you truly understood the concept of inheritage.