This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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: 23956
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: 23956
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: 23956
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.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic