aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Urgent help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Urgent help needed" Watch "Urgent help needed" New topic
Author

Urgent help needed

Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
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?
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
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


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
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

Joined: Sep 08, 2000
Posts: 61
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

Joined: Dec 08, 2000
Posts: 28
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.
Sri Yamujala
Greenhorn

Joined: Dec 07, 2000
Posts: 18
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]

Sri
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
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

Joined: Dec 07, 2000
Posts: 18
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

Joined: Jan 08, 2001
Posts: 290
I think this post will help your question.
http://www.javaranch.com/ubb/Forum24/HTML/008124.html
Joe
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
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

Joined: Dec 08, 2000
Posts: 28
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.
duchang
Greenhorn

Joined: Feb 18, 2001
Posts: 3
Static methods of a super class are shadowed just like member variables of the super class.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent help needed