• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

polymorph question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i was doing a practice test today and here is a screen shot:



Am I right by saying they are incorrect because SuperClass does not have access to Subclasses methods?
 
author
Posts: 23964
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Am I right by saying they are incorrect because SuperClass does not have access to Subclasses methods?



First of all, please quote your source -- tell us where this question is from.

As for your question... No. Take a look at the type of method it is, and how is it being called. Are you saying the main method doesn't have access to the static method in question? And in the way it is being called?

Henry
 
curtis ashby
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok my source is from uCertify - SCX310-055.

So your saying that because this method is "public static" the SuperClass has access? I tried reproducing this in eclipse with a couple test classes and I couldn't get it to compile.
 
curtis ashby
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example:

public class Item {
/*public void hiFromItem() {
System.out.println("hello from super");
}*/
}

public class Laptops extends Item{
public void hiFromLaptops() {
System.out.println("hello from child");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Item it = new Laptops();
it.hiFromLaptops();
//it.hiFromItem();
System.out.println(it);
}
}

Above gives compelation error. However,

public class Item {
/*public void hiFromItem() {
System.out.println("hello from super");
}*/
}

public class Laptops extends Item{
public void hiFromLaptops() {
System.out.println("hello from child");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Item it = new Laptops();
((Laptops) it).hiFromLaptops();
//it.hiFromItem();
System.out.println(it);
}
}

compiles. So maybe this is a casting situation.
 
Henry Wong
author
Posts: 23964
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So your saying that because this method is "public static" the SuperClass has access? I tried reproducing this in eclipse with a couple test classes and I couldn't get it to compile.



No. I am saying that it is a static method of the subclass. And it is being called from another static method of the subclass. Why can't a static method call another static method, of the same class?

Henry
 
Henry Wong
author
Posts: 23964
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So your saying that because this method is "public static" the SuperClass has access?



Oh... I see where you are getting confused. It is *not* being called from the SuperClass. It is being passed a SuperClass instance via a parameter. That is not the same as the SuperClass accessing the SubClass.

Henry
 
curtis ashby
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Oh... I see where you are getting confused. It is *not* being called from the SuperClass. It is being passed a SuperClass instance via a parameter. That is not the same as the SuperClass accessing the SubClass.

Henry



First of all Henry THANKS for your help because this one is really confusing me!!!

But isn't the variable "m" of Type SuperClass calling the method? And wouldn't that restrict the variable "m" to the scope of the SuperClasses methods? I mean the SuperClass isn't inheriting from the SubClass so how could it invoke a method of the SubClass?
 
curtis ashby
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOOOOOOOHHHHHHHHHHH I GET IT! The method isn't being invoked by the m variable its being invoked by the main method..... ok i'm feeling dumb now but at least i get.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic