• 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

Urgent help needed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 programs here.But I cant understand how we get the results in both,.
1)public void amethod(){
System.out.println("base");
}
jbase(){
amethod();
}
}
public class Rtype extends jbase{
int i=1;
public static void main(String arg[]){
jbase b=new Rtype();
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("R");
}}
Ans--
In this we get
R
99
R.
I have problem only with the last R.

Another similar program is given below.
2)public class Superclass {
public static void main(String[] args) {
System.out.println(new Subclass().methodA());
}

Superclass() {
System.out.println("SuperClass Constructor Executed");
}

private int methodB() {
System.out.println("methodB in Superclass");
return 9;
}

int methodA() {
System.out.println("methodA in Superclass");
return methodB();
}
}

class Subclass extends Superclass {
Subclass() {
System.out.println("SubClass Constructor Executed");
}

protected int methodB() {
System.out.println("methodB in Subclass");
return 1;
}
}
Here Ans is
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9
How is MetodB in Superclass called instead of methodB in SubClass?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you make your code more readable ? As I am having a little difficulty in reading code.
http://www.javaranch.com/ubb/ubbcode.html
------------------
http://www.mantrotech.com
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou gaurav for the explanation you gave for my other doubt.
Expecting your help for this one also.
/**1st program*/
1. class JBase{
2. public void amethod(){
3. System.out.println("base");
4. }
5. JBase(){
6. amethod();
7. }
8. }
9. public class Rtype extends JBase{
10. int i=1;
11. public static void main(String arg[]){
12. JBase b=new Rtype();
13. System.out.println(b.i);
14. b.amethod();
15. }
16. public void amethod()
17. {
18. System.out.println("R");
19. }
20. }
Ans--
R
99
R.
I have problem only with the last R.If we call amethod() in JBase() shouldnt the JBase amethod() be executed.I know that if you call b.amethod() the RType amethod is called.

I have another similar doubt in this program
/**2nd program*/
public class Superclass {
public static void main(String[] args)
{
System.out.println(new Subclass().methodA());
}
Superclass() {
System.out.println("SuperClass Constructor Executed");
}
private int methodB()
{
System.out.println("methodB in Superclass");
return 9;
}
int methodA()
{
System.out.println("methodA in Superclass");
return methodB();
}
}
class Subclass extends Superclass
{
Subclass()
{
System.out.println("SubClass Constructor Executed");
}
protected int methodB()
{
System.out.println("methodB in Subclass");
return 1;
}
} //end

Ans is
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9

Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mindy but the code in Question 1 will not even compile as class JBase has not int i. So line 13 will not compile.
You can format code as by using different tags mentioned at http://www.javaranch.com/ubb/ubbcode.html .
Can you quickly repost one question at a time and let me work with you to try to resolve your issues.

------------------
http://www.mantrotech.com
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that I missed the value for i.I have no idea how to use the link you gave me gaurav.Can you help me.
/**1st program*/
1. class JBase{
2. int i=99;
3. public void amethod(){
4. System.out.println("base");
5. }
5. JBase(){
7. amethod();}
8. }
9. public class Rtype extends JBase{
10. int i=1;
11. public static void main(String arg[]){
12. JBase b=new Rtype();
13. System.out.println(b.i);
14. b.amethod();
15. }
16. public void amethod()
17. {
18. System.out.println("R");
19. }
20. }
Ans--
R
99
R.
I have problem only with the last R.If we call amethod() in JBase() shouldnt the JBase amethod() be executed.I know that if you call b.amethod() the RType amethod is called
I have another similar doubt in this program

/**2nd program*/
public class Superclass {
public static void main(String[] args)
{
System.out.println(new Subclass().methodA());
}
Superclass() {
System.out.println("SuperClass Constructor Executed");
}
private int methodB()
{
System.out.println("methodB in Superclass");
return 9;
}
int methodA()
{
System.out.println("methodA in Superclass");
return methodB();
}
}
class Subclass extends Superclass
{
Subclass()
{
System.out.println("SubClass Constructor Executed");
}
protected int methodB()
{
System.out.println("methodB in Subclass");
return 1;
}
} //end

Ans is
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9

Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the indexing, I just found out actually - use the UBB codes. Whenever you post some code, to save the indenting, put all your code between .
Like here:

Ans is
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9

Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
[/B]
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I attempted to do didn't work. To see how to format your code just click on the edit box above someone's post. You won't be able to edit their post but you will be able to find out how they were able to format their code.
Once you figure this out could you fix your code and maybe delete the repeated versions? Thanks.
I'm not sure but in response to your second code I believe the problem is that the methodA is calling methodB from within Superclass so it is going to call the Superclass method of methodB. I made both methods static and used the this keyword and still get your result so as far as the method is concerned it must not see subclass. Here's my code:

You've brought up an interesting question. I look forward to the replies.
Joe
[This message has been edited by Joseph Russell (edited February 15, 2001).]
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mindy,
If you take out private keyword for methodB() in Superclass, it's then printing 1 - so, it's got something to do with the access modifier in this case - can someone clarify, please?
Thanks,
Sri

Originally posted by Joseph Russell:
[B]What I attempted to do didn't work. To see how to format your code just click on the edit box above someone's post. You won't be able to edit their post but you will be able to find out how they were able to format their code.
Once you figure this out could you fix your code and maybe delete the repeated versions? Thanks.
I'm not sure but in response to your second code I believe the problem is that the methodA is calling methodB from within Superclass so it is going to call the Superclass method of methodB. I made both methods static and used the this keyword and still get your result so as far as the method is concerned it must not see subclass. Here's my code:

You've brought up an interesting question. I look forward to the replies.
Joe
[This message has been edited by Joseph Russell (edited February 15, 2001).][/B]


 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this post will help your question.
http://www.javaranch.com/ubb/Forum24/HTML/008124.html
Joe
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sri your right. I took out my static references and when I put in either protected or friendly in Superclass it calls on methodB in Subclass but when I have Superclass' methodB as private and Subclass' methodB as protected, private, public or friendly it calls on methodB in Superclass. When I leave my static references in there it calls on methodB in Superclass everytime. Can anyone provide insight as to why this happens?
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John Russel static methods cannot be overriden.They are only hidden(as are private methods).So it behaves in the same way.So we do not get a compiler error but there is no overriding as such in the case of static methods.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods of a super class are shadowed just like member variables of the super class.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duchang,
Please read the JavaRanch Name Policy and re-register with a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic