File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Master exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Master exam question" Watch "Master exam question" New topic
Author

Master exam question

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49


this question is from K&B master exam...
the answer of this code is a compilation error at #1.. in the explanation of this program says "only instance methods can be overridden and calls to super only apply to overridden methods" i have not understood this...
please expalin me what is menas... and what is System.out.print() statement doing its only for input at the command prompt i guess???

thanks in advance...
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
The problem here is that you are using super in a static context.


SCJP 5, SCWCD 5
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
'super' cannot be used from a static context. Hence



will give compile time error.

only instance methods can be overridden


This is because of the concept of overriding is deeply associated with runtime polymorphism and hence to the associated object instance.

calls to super only apply to overridden methods

Not sure what is meant by this. You can call 'super' in any instance method as is being done here



what is System.out.print() statement doing its only for input at the command prompt i guess???

System.out.print() is for displaying the output in the console. Not for input.

Also I notice a few compilation issues with your code. Maybe this is what you wanted:



Thanks and Regards
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
thanks..
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
you all mean to say that we can't use super with staticm methods. If yes, can you please expalin me why can't we use super with static methods .
please...


Thanks<br />Dinesh
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
This is because of the concept of overriding is deeply associated with runtime polymorphism and hence to the associated object instance.


Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
static members are not linked to an instance of a class : they do not belong to an object, they belong to the class.
static members are shared among all the instances of the class.

They are invoked that way : Class_Name.static_member.
For example:

class Test{
public static int count;
public static void hello(){
System.out.println("Hello Test, count="+count);}
}

To invoke hello() method: Test.hello();
You don't need to do that(but you can!!): Test t=new Test(); t.hello();

static members are shared among all the instances of the class :

Test.count=45;
Test.hello(); // shows Hello Test, count=45
Test t=new Test();
t.hello(); // shows Hello Test, count=45
t.count=12;
t.hello(); // shows Hello Test, count=12
Test.hello(); // shows Hello Test, count=12

count, which is a static variable, is shared among all the instances of the class: it does not belong to a specific object.

So, if you call this or super from a static context, this doesn't work because this and super are linked to an instance of the class
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
You must always remember that super and this can't be used in static context .

super indicates that super class of current object . You can use instance
variables in instance method .

as public static void main(String [] args)

is static method . you can call it without creating any object . If any
instance method is called using super keyword , how will you get instance variables's value .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
If any instance method is called using super keyword , how will you get instance variables's value .


id idn't get your sentence can you please explain in simple terms. Please with example, tht would be great..
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Dinesh,

We really want to help you pass this exam, but you have to help yourself too! What part of the answer is confusing to you? Are you clear on instance methods and instance variables vs. static methods and static variables? Do you understand how to access instance vs. static members?

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Berates,

I know the difference, instance variables are related to the class . i.e defines outside the method and can be accesed only when object of class is created.
While static variables are class variables, they can be accessed using the classname, not by creating the object of class.

I am not sure to access the instance methods and static methods??

Correct me if i am wrong.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Dinesh,

Would you be willing to write some code that demonstrates the creation and use of static and instance class members?

Bert
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
Dinesh,
You wrote "While static variables are class variables, they can be accessed using the classname, not by creating the object of class."

Of course you can access a static variable or method creating an object of the class !!!
All objects of the class share the static variables: if one object modifies the value of a static variable, other objects of the class will share the same value.
A static variable or method can be accessed from non-static members and static members,
but a static method CAN'T access non-static members.
adam simo
Greenhorn

Joined: Sep 01, 2008
Posts: 4
Hello,

You have 2 versions of getRating() method, one marked static and other not ok,
from static context, in this case from main method, you can not access instance methods, so instance method getRating without static is not an option, but you can access static getRating() from main, but the problem here that the program use "super" to access a static member which is not alowed, so the result is :

C- Compilation fails due only to an error on line #1
Eric Battley
Greenhorn

Joined: Dec 02, 2009
Posts: 3
disclaimer: I'm a very green greenhorn and am utterly subject to correction (and I welcome it) on all of this . . . .

I ran across this same Master Exam question yesterday. It suprised me as well. I figured it would compile and run since I wrongly assumed that super would have the effect of standing in for the name of the immediate parent class.

I then got quite confused by the "correct answer explanation" from the Master Exam (which I now regard to be in error).

As S. Singh mentioned, the provided Explanation text states the following, "only instance methods can be overridden and calls to super only apply to overridden methods".

I find that the first clause of the explanation isn't especially relevant.

Then as Satya points out above, the second part "calls to super only apply to overridden methods" is at least confusing. In my tests, non-static method code can legally, successfully call super.myMethod() even when no overriding happens, specifically when:
  • the calling method does not override anything in the super instance
  • the method of the parent object called is not overridden in the calling object

  • (Of course using super absent any overriding is probably always unnecessarily redundant).

    In fact, while my NetBeans IDE shows a warning, a non-static method can even compile calling super.myMethod() when myMethod() is a static method in the superclass. It seems equivalent to calling the static method using ClassName.staticMethod() notation. Of course this makes sense I guess since you can call a static method through a reference to an instance from the same class.

    The most helpful explanation of why the call won't compile came from Antonio who said:
    So, if you call this or super from a static context, this doesn't work because this and super are linked to an instance of the class


    Now, after chasing this down some, I would describe super in this context as behaving somewhat like a reference for accessing the the current object as if ignoring any overidding members (whether they exist in fact or not) of the subclass of which the object is an instance.

    I guess another way to say this is that super seems to act like a reference (to the current object) that is typed as the object's immediate parent class and functioning as if dynamic method selection is disabled from stepping down into the overrides of the current object's lowest/actual type.

    (Yuck. I'm sorry that I can't seem to find a simple way to express the above idea.)

    But, as antonio points out, from a static method there is no way to reference the current instance, since the code runs apart from an instance of its class. Thus super can't compute. The ClassName.staticMethod() notation has to be used.

    To recap, the Master Exam gives us the correct answer, I just think it is in error when it explains why, in the study guide material it offers.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Master exam question