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

Master exam question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem here is that you are using super in a static context.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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:

 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks..
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because of the concept of overriding is deeply associated with runtime polymorphism and hence to the associated object instance.
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic