• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Super keyword issue and polymorphism question

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to use the keyword super in an assignment and not really sure what I'm doing. The book says to always put it at the top when using it in a parameter, but now it's basically redirecting everything and making the Customer objects always show up as Elvis. Even when I use the copt constructor & method to make a new customer. But I might be using that wrong as well.

Customer.java





CustDemo.java



Person.java



Last thing, is I'm supposed to use an example of polymorphism, but I can't even understand what it is, muchless what it looks like. Could someone please help? :3

The problem with the copy, is that if I try to copy cust2, it still outputs the info of cust1.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super is nothing more than a way to walk back up the chain of inheritance. If you have an object A that extends B, then B is the parent of A. If you have an Override, like when you Override paintComponent in a JPanel for doing graphics, then you call super.paintComponent(g) as the first line of your local paintComponent so the object can be initialized by the parent class. In this case, the most notable action, the call to super.paintComponent(g) will clear your GraphicsContext g in preparation for the new painting cycle.


So if you have hidden a method, Overridden, and need access to the original, you can get there with a call using super.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikki Smith wrote:
Last thing, is I'm supposed to use an example of polymorphism, but I can't even understand what it is, muchless what it looks like. Could someone please help? :3



Polymorphism is simply the concept that when you call a method on an object, you know that something will be done, but there's no guarantee *how* it will be done. Sound confusing? It isn't, really. Let's consider a concrete example from nature.

Mammals reproduce by giving birth to live young. So, if we were to describe that in a class, you might have something like this:



Note that I didn't bother to define the superclass Animal of the method giveBirthToLiveYoung; this example is not intended to be code complete!

Now, for a fun fact about nature: not all mammals give birth to live young. (You may or may not already know this). There is a group of mammals called monotremes that actually lay eggs. (They are still mammals because the mothers produce milk for the young once they have hatched.) The Platypus and the Echidna are the members of this group. We may describe this behaviour in a class this way:



Now, we have a subclass of Mammal that lays eggs instead of giving birth when asked to reproduce. It can inherit other methods of Mammal (perhaps "produceMilk()"), but when it comes time to reproduce, it lays eggs.

So this method:



takes a Mammal as a parameter and tells it to reproduce. You know that it it going to reproduce *somehow*, but not exactly how (unless, of course, you know the exact class of the parameter-- but you don't, and in most cases where polymorphism comes into play, you really shouldn't care). That Mammal could be a live-birth mammal (like a Cat or a Dog) or it could be a Monotreme -- it could lays eggs.

And that's polymorphism in a nutshell. For a more detailed example (again, using animals), check out our campfire story How My Dog Learned Polymorphism
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just an additional thought on the super keyword: super is generally used when a subclass wants to so something *in addition* to what the superclass does. Back to the animals!

A Marsupial is a Mammal that raises its young in a pouch. Kangaroos, Opossums, Koalas -- these are all Marsupials. The baby is born, like any other mammal, but then it moves to the pouch and continues development there. Let's model that in a class:



There: the Marsupial reproduces just like any other Mammal (we ensure that by calling super.reproduce(), which call the version of the method defined in the Mammal class), but then it adds the bit about making sure the baby moves to the pouch. Yes, we could have just overridden the method like this:




and it would have the same effect. For now. If the definition of reproduce in the Mammal class changes, this will still do things the old way, so it doesn't stay up-to-date. Also, if the definition of reproduce in the Mammal class were 100 lines long, we would have to copy the 100 lines here. Using the super keyword allows for code reuse (instead of code copying), and it ensures that the subclass will stay in sync with the parent class. So don't use the second example, because while it achieves the same effect, it is an inferior solution. Use super instead.

Incidentally, you are only forced to call super first in constructors. In methods, it can be anywhere in the method, although it typically is first there, as well (like in our example above). When it is not first, it is last -- meaning that you want to do something custom *before* calling the super method. It is very rare to find it in the middle of a method, though, just because most process flows don't need it there. (And it can make the code hard-to-read! Code that is easy to read is also easy to maintain, so that is an important consideration.)
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic