• Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphism and super call  RSS feed

 
Nick Apostol
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day to every one.

I am new to the forum and I have just begun my software engineering degree. Having taken two preparatory courses in java over the summer, I thought I had an understanding of the language. But now, I find myself in my Software Engineering 1 course (uml, design patterns etc.) and my professor mentioned in one of his slides that
the super keyword breaks/nullifies polymorphism. The class seemed to go along and so I raised no objection. The example he used was the following:



He then has a class Director and asks which implementation we would choose (a or b):



I would have chosen (b).

My question is why does he claim that super keyword nullifies polymorphism. From my understanding, polymorphism works with super/sub types and the appropriate method call is determined by the actual object type and not the variable type. I apologize if the question seems unclear or too vague, but it is something that has been bothering since that class. Any explanation would be greatly appreciated. many thanks in advace.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cast in option a does nothing, because of the way that polymorphism works. So if you tried to run that option, you would find that Director's getSalary() method was calling itself. This would eventually throw an exception once the call stack got full. On the other hand option b does what it's supposed to do, so it's the right option to choose.

I have no idea why your professor said that. In my opinion the super() keyword is an important feature of polymorphism, rather than being a bug in it. But anyway I would think that if you wanted an explanation of why Person X said something, the best person to ask would be Person X. Have you considered that?
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapton has an apt explanation and I don't see why would someone explain the concept as something that nullifies polymorphism. I don't think that this is a nice term to explain things to a class and more over the listed examples definitely don't add anything to explain the phrase. I guess that he was trying to convey that when you call super.method then the super classes' method gets called and thus at run time overriding does not happen at this particular statement call but then isn't that exactly why we'd want to use super in this scenario? To avoid rewriting logic that may already be present in the super class?

I think it's better that you catch up with your prof and ask him for an explanation... just like paul says
 
Nick Apostol
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your prompt responses. I have in fact scheduled an appointment with him this upcoming week, but I wanted to make sure that my doubts were well founded. What bothers me more is the fact that every one in my class seemed to be agreeing (nodding heads, saying "Oh yeah, I remember..." etc.).
At least now, I can go in to see him without thinking that I have lost my mind or wasted an entire summer of java.
Once again, thank you both.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Nick. Anyway words said by a certain person do not get etched on stone - so you are free to investigate and draw your own conclusions and it's good that you are doing that
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Apostol wrote:Thank you both for your prompt responses. I have in fact scheduled an appointment with him this upcoming week, but I wanted to make sure that my doubts were well founded.


But do try actually executing both versions. You never know, it's possible that I'm wrong. Remember... "Trust, but verify". If I'm right then you will be prepared for your meeting. But if I'm wrong then you might look like a fool. I would prefer the former outcome and I'm sure you would too.
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to understand the point , i tried a example :



In the above , the subclass getSalary() is getting called, but with a super call we are able to call the super class method , probably this is a way of looking at things , can say that 'super' is a workaround for polymorphism

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!