• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

having problems understanding polymorphism combinations

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i have food class and pie class which inherits food
regarding the accsess level

i have


i know that both are reference variable of pie,
but x is of type pie and y is of type food.
what is the difference between the two regarding method accsess in both classes
?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:if i have food class and pie class which inherits food



Food and Pie. By convention, Java class names start with uppercase letters.

i have



"I have." By the rules of English grammar, the personal pronoun "I" is always capitalized. If you UseRealWords(⇐click) it makes it easier to read and understand your question.



i know that both are reference variable of pie,



No. The "x" variable is of type reference-to-Pie, and the "y" variable is of type reference-to-Food.


what is the difference between the two regarding method accsess in both classes
?



What do you mean?
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if in both classes i have a (non static) mathod "eat"
and will do

x will call the method from pie
and y will call the method from pie.

question regarding case 1:
if "food" doesnt have a method "eat"

what will happen?

and on the opposite case 2:
if "food" has a method "eat" but pie doesnt ave one

what will happen?

and the same as case 2

what will happen?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:what is the difference between the two regarding method accsess in both classes?


x can call all the methods defined in pie and y can call only the inherited methods of food in pie.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so "y" can use only the methods in pie which are present in food
correct?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:question regarding case 1:
if "food" doesnt have a method "eat"

what will happen?


It won't compile because there is no eat() method defined for the type food.

donaldth smithts wrote:
and on the opposite case 2:
if "food" has a method "eat" but pie doesnt ave one

what will happen?
and the same as case 2

what will happen?


The eat() method in food will be called since pie inherits the method eat() method from its super class food.

This thread might interest you.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:so "y" can use only the methods in pie which are present in food
correct?


methods in inherited in pie. Say if a final method is present in food which will not be present in pie, but inherited by pie.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:so if in both classes i have a (non static) mathod "eat"
and will do



Why don't you try it and see?

And please, have the common courtesy to write proper English. Thanks!
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not native english speaker so please be considerate

So to summerise my cases:



regarding x.eat():
if there no eat method in pie 'but there is one in food then it will call the method from food because it inherits food class.
if there is eat in pie then it will call the method from pie.

regarding y.eat():
if there is no eat method in food then it wont compile because y is of type food.
if there is no eat in pie but there is one in food then it will call the method eat in food.

correct?

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:correct?



As long as the access modifiers of each of the methods allows access, yes.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

donaldth smithts wrote:i am not native english speaker so please be considerate



I am being considerate. I'm simply asking you to do the same.

Even though you are not a native speaker, I'm sure you learned the basics of capitalization and punctuation. I don't expect your grammar to be perfect, but it is fair to expect you to try. I politely asked you to make an effort to write correctly, briefly told you why, and provided a link with more information. You do not seem to be making an effort however, so I will bow out now and let others help you.

Good luck.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic