• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instance doubt

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at the code and ans explain me

Class A{
public static void method a{
int x=1;}

}
class B extends A {
public static void method a{
int x=0;
}

public static metod b{
}

public static void main{
A ins = new B();
ins.a();
ins.b();
s.o.p(ins.x);

}

1. Is this code compile ? can you override the static method?
2. ins.a() will compile ?
3.ins.b() will compile ?
4. what is ins.x value ?


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


1. Is this code compile ?



I feel this question is easy enough to answer on your own. Try to compile it and see what happens.


2. ins.a() is correct?
3.ins.b() is correct ?



What do you mean by correct? Are you asking if you are calling the method correctly?


4. what is ins.x value ?



Try running the program with System.out.println(ins.x()) and see what the output is. Of course you will need a public static void main(String[] args) {} somehwere to run the program.


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

Your code sample provided is riddled with mistakes...also please use the code tags to make it much easier for people to
look at and answer your question if you want a speedy reply. The method definitions are incorrect, correct syntax:

you don't have any round brackets. Also you call a method x() but there is NO such method in your code
maybe you are referring to the variable x?? Please repost with proper syntax and code tags.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am completely confused...what is this ???


as it stands your code will NOT compile at all!!
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Meghna Bhardwaj wrote:I am completely confused...what is this ???


as it stands your code will NOT compile at all!!



I think the Anitha was trying to put in my System.out.println suggestion.

Anitha, can you make the changes we've suggested, then repost your code in code tags? On the screen where you enter your post there is a button that says "Code" that you can use to place code tags around your code.

Also, if you are trying to put in the suggestions, please post real code. Having s.o.p(ins.x) doesn't help us help you, whereas System.out.println(ins.x) is very clear as to what you are trying to do.
 
Anitha Srinivasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am not asking about the syntax i am talking about the concept ,
1. can we override a static method ?
2. If a super class object ref the subclass what will be the runtime reference of the object?
thanks
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. can we override a static method ?



No, and here is a link to explain.


2. If a super class object ref the subclass what will be the runtime reference of the object?



I think you are asking about this:

A ins = new B();

While the reference is pointing to a B, as far as the JVM is concerned ins is an A reference variable.
 
Anitha Srinivasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wonderful Joe. This is what i was looking for . That link really helped me.

so...
1. if we override a static method compiler doesn't throw error. It is actually hiding a class method not overriding static method.
2. static method doesn't have runtime polymorphism
3. instance method have runtime polymorphism

A ins = new B();

While the reference is pointing to a B, as far as the JVM is concerned ins is an A reference variable.


So ins.b() will throw a compilation error? since ins is a A reference variable , A does not have method b.

one more doubt

final A ins =new A();
ins.x=5;

Here i am creating a final class reference ins. And i am trying to assign ins.x=5 ; can i assign a value for final class variable ?


Thanks, ANitha
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags when you post a source code...
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anitha Srinivasan wrote:


Use code brackets.

Anitha Srinivasan wrote:
While the reference is pointing to a B, as far as the JVM is concerned ins is an A reference variable.


Because you declared ins as a A reference variable, it will ALWAYS be a A reference variable.
Overriding is all about what kind of object it is and if the method is declared in both classes.

Anitha Srinivasan wrote:
So ins.b() will throw a compilation error? since ins is a A reference variable , A does not have method b.


Right.

Anitha Srinivasan wrote:
one more doubt
final A ins =new A();
ins.x=5;
Here i am creating a final class reference ins. And i am trying to assign ins.x=5 ; can i assign a value for final class variable ?


No you can't . But you don't do it in your example. You can't reassign/modify any final variable. But you can reassign/modify some variables IN the final variable. You can't do the following:

cheers
Bob
 
Anitha Srinivasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

Can i reassign a class variable to final class ref?

for example,
class A{

int x=0;}

class B extends class A{
int x =1;};

public static void main(args []){
final A ins=new A();
ins.x=5;
}




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

Anitha Srinivasan wrote:Thanks.

Can i reassign a class variable to final class ref?

for example,
class A{

int x=0;}

class B extends class A{
int x =1;};

public static void main(args []){
final A ins=new A();
ins.x=5;
}


Code it yourself and you will see it works. You can put it this way: You are allowed to change the properties of an object to which a final reference points. But you can't change the reference itself.
If you use final primitives, you can't change the variable at all.

cheers
Bob
 
Anitha Srinivasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bob!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic