Java Inheritance Classes & Super Print Help, Please!

Status
Not open for further replies.
G

Guest

Guest
Hi, all!
I am stumped on this project, hoping someone can help me!

Here are the terms of the project:
You will create 4 Java classes for this project.
The first is a Product class. This will be a generic class for Products that a company may sell. It will have variables for the following: name, description, price and productID. Use appropriate data types. Write or generate getters and setters for all variables. Write a function called print() that prints the Product information to the screen.

The second is a Phone class. It will Inherit from the Product class. It will have variables for make (Apple, Motorola, Samsung, etc), model (iPhone 6, Moto X, Galaxy S5, etc), and storage space (in gigabytes). Write or generate getters and setters and Overload the print() method that's inherited from the Product class. Make use of the super.print() call.

The third is a TV class. It will Inherit from the Product class as well. It will have variables for make, screen size, type (LCD, LED, Plasma) and whether or not it is 3D capable. Write or generate getters and setters and Overload the print() method in the same manner you did with the Phone class.

Finally, write a driver class that instantiates a Product, a Phone and a TV, sets the variables and then prints them all three to the screen.

Below are my 4 classes that I have written.
My issue is with the printing. I can't get the info to pull into the right spaces when I print.
I know I'm missing something, like calling out the string that I'm printing from.
Any help/advice would be great! I'm still a rookie with java, but learning!
Thank you in advance!

 
Solution

Pinhedd

Distinguished
Moderator


It looks to me like the person who assigned you this assignment has confused overloading with overriding.

In C++, C#, and Java, overloading occurs when two methods (the fancy object-oriented term for functions) have the same name but different parameters. This results in a different signature. For example,

MySuperClass has a method named MyMethod with return type MyReturnType and parameters MyParameterA and MyParameterB.

A child class that inherits from MySuperClass inherits this method if it's qualified as non-private. The child class may also create its own method named MyMethod with return type MyReturnType and paramters MyParamterA, MyParameterB, and MyParameterC. Even though the methods have the same name within the same scope (the child class) the compiler will be able to select the correct one based on the number of provided parameters. Code within the superclass will not be able to call the method in the child class because it's out of scope.

Method overriding occurs when a child class creates a function with the exact same signature as a function in the parent class. When this happens, the functionality of the method in the parent is overridden by the functionality of the method in the child. For example,

MySuperClass has a method named MyMethod with return type MyReturnType and parameter MyParameterA. This method performs foo.

MyChildClass inhereits from MySuperClass and overrides MyMethod. The overriden method performs bar.

An object of type MyChildClass is created and passed into a function that accepts an object of type MySuperClass. The function then calls MySuperClass.MyMethod(), and the resulting operation is bar.

How does this happen? Whenever a class overrides a function in a parent the resulting object data includes a virtual function table. The virtual function table includes pointers to the actual method that should be called whenever the overriden method is called. Calling MyMethod() may invoke whatever method is pointed to by the first value in the virtual function table. In MySuperClass that will be the the one that performs foo, in MyChildClass it will point to the method that performs bar. If a second child class were created which also overrides it, it will point to one that performs blah or whatever we want it to do.

EDIT: A child can call super.MyMethod() to invoke the parent's implementation of that function as if it were not overridden. This is not required, but it is handy for ensuring that a child's amendment to a parents behaviour doesn't cause it to deviate too much if the parents behaviour is later changed. For example, in the overriding method that performs bar, calling super.MyMethod() will also perform foo. This way, the child need only be concerned with the behaviour of the elements that it adds to the parent.

Now that you're armed with a basic understanding of overloading vs overriding you should be able to better understand what your instructor wants you to do. From what I understand he or she wants you to create a parent class with some print function, and two child classes which override that print function (not overload it).
 
Solution
G

Guest

Guest



Thank you so very much for that awesome detailed answer!
You helped me understand much better. I really appreciate it!
Armed with the knowledge that you gave me, I did a little more research, watched a few more tutorials and figured it out!
Thank you! :D
 

Pinhedd

Distinguished
Moderator


You're most welcome!
 
Status
Not open for further replies.