Hey Guys I am confused on how these objects operate in this way,Instantiating as one class and being a different type , the book I am reading never mentioned this?
I don't really know the best way to explain this, but it is called polymorphism. It is easy to see how classes connect if the original designer named and described his/her classes the right way.
The method I was taught and still use is the 'is-a' relationship. In question 15 the anEmployee instance is initialized as a Programmer object. Sounds weird right? Well not with the 'is-a' relationship, a Programmer 'is-a' Employee, but not every Employee 'is-a' Programmer. If you diagram out a hierarchy of classes, the Employee class would be seen as the "parent" or "super" class to the Programmer class because it is a less specific version of what the coder is trying to express.
Yes, it is a little hard to get used to and recognize, but it is an extremely useful/important concept in creating clean and well designed code. It is just another tool used to organize large libraries and projects.
Sorry if this is hard to see, I did this in ms paint in like three seconds. But in terms of code, these three classes that 'extend' (in terms of Java) the Employee class all share the same variables and methods (like Age, Name, Time worked, etc). The only difference is that the Programmer class will have extra variables and methods along with the ones it shares with the Employee class, (like kind of computer, and what language they use). Same with the Engineer and Concept Artist. Each time you go down a level of the diagram whatever is expressed in the code is a more specific version of whatever the parent class is.
I understand polymorphism but in question 14 if method 1 is used because kid is the type Parent then why in question 15 would the method of the Programmer be used because anEmployee is the type Employee. I also don’t understand why for in question 14 for example you couldn’t just do Child kid = new Child; and have access to the parent methods as well?
To sum I understand the answer to 15 because since it is a Programmer object it would take advantage of the subclass method increaseSalary but I would have thought in 14 method 3 would have been used because it’s a child object, instead it uses the parent class method?
May I ask what website this is from? I believe question 14 is wrong. Method 3 should be executed.
I just created a test program because I doubted my memory, and yes, method 3 is the one that is actually executed.
If you have the BlueJ Java IDE already or are willing to install it, I can send you the test program I just made in a 7-Zip file.
Lol from a community college, now my confusion is gone
Unless there is something I am not seeing or do not remember, I believe that the website is wrong. Lol good luck at the college man
Thanks, I am doing community college this summer before I go to ISU this fall for CS(All because of some stupid girl)
Nice, I am just going into my junior year in high school, still don't know what I want to major in. Computer Engineering/Software Engineering with a minor in business sounds nice because I want to run my own business, but only time will tell.