Mindy Hudson

Greenhorn
+ Follow
since Dec 08, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Mindy Hudson

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.
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.
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.
Hi Sri..I can explain to u what I understand.As p is declared as
Process p = new InheritanceTest();
p is of type Process but class InheritanceTest.ie the constructor of InheritanceTest gets called.And for a subclass when there is always an implicit call from the subclass constructor to superclass noarg constructor if we dont specify any super(arg) calls.
Then methods are called by class and variables by type.So according to that I think char InheritanceTest(char c) in InheritanceTest shouldbe executed instead of char InheritanceTest(int i) in Process.Anyway when I compile it I am getting S.Howcome.
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?
Someone please help..It is urgent
class InheritanceTest extends Process {
int x=18;

public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}

InheritanceTest() {
System.out.println(true ^ true);
}

InheritanceTest(char c) {
System.out.println(c);
}

char InheritanceTest(char c) {
c='V';
return (char)c;
}
}

class Process {
int x=9;

Process() {
System.out.println("Starting Process...");
}

char InheritanceTest(int i) {
i='S';
return (char)i;
}
}
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18
I think the answer should be 5.But the correct answer is 7.Since p has its class as InheritanceTest shouldnt the method in InheritanceTest be called.Can you explain it to me.
I have no idea about this.I know that when components are more than cells.A column is alloted.But when it is less how is it.Experts please respond.
I am experimenting with GridLayout.I thought the following code should give
b1 b2 b3 b4 b5
b6 b7 b8 - -
But when I run it.What I get is
b1 b2 b3 b4
b5 b6 b7 b8.Can some one explain this.What I understood was the rows and cols are allocated first and the components are filled in them.Some one please help me understand.
/**code*/
import java.awt.*;
public class GridAp extends Frame{
public static void main(String argv[]){
GridAp fa=new GridAp();
//Setup GridLayout with 2 rows and 5 columns
fa.setLayout(new GridLayout(2,5));
fa.setSize(400,300);
fa.setVisible(true);
}
GridAp(){
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
add(new Button("Five"));
add(new Button("Six"));
add(new Button("Seven"));
add(new Button("Eight"));

}//End of constructor
}//End of Application
thanks peter for the help.Will post more doubts
Yes peter my question is why the copiler doesnt complain System.out.println("Hello"); not reached with the above code (as you said its not reached in anycase becoz of the return statement).But as soon as I put return 5 in the catch block it does so.I think we should get the error in both cases right?
I am confused about the flow of the exception statements.When I run this I get try and 3.I thought it should print Hello also.
If I include a code return 5 in the catch block the compiler shows an error saying System.out.println("Hello"); not reached.If it is because of the return 5; then why I am not getting this error with only the below code as if there is no exception the code after the finally block should be executed?Can anyone please help.
public class Test5 {
public static void main (String args []) {
System.out.println(A());
}
public static int A(){
try{
int i,j,k;
i=4;j=2;
k=i/j;
System.out.println("try");
return 3;
}
catch(ArithmeticException e){}
finally{}
System.out.println("Hello");
return 4;
}
}
Sorry I forgot to mention my email id.It is mindy.hudson@mailcity.com
I also want it.Navin.Please mail it to me.
Hi every one thaks for the answers.Will be posting more doubts.