aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please help" Watch "please help" New topic
Author

please help

Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
public class Geek {

int i = 1;
public int getI(){
System.out.print("Super");
return i;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Geek ga = new Arabik();
System.out.print( ga.i+" " +ga.getI());
}

}
class Arabik extends Geek{
int i = 2;
public int getI(){
System.out.print("sub");
return i;
}
}

Why the answer is "sub1 2"??


Rajeev Kumar
SCJP 6.0 Certified 96%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Rajiv please Use A Meaningful Subject Line for your topics and UseCodeTags for your code. What do you think the answer should be?? Remember, in the statement
The complete expression inside the brackets will be solved, so ga.getI() method will be called first, so sub will be displayed, then the above statement will display 1 2...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
Ankit Garg wrote:Rajiv please Use A Meaningful Subject Line for your topics and UseCodeTags for your code. What do you think the answer should be?? Remember, in the statement
The complete expression inside the brackets will be solved, so ga.getI() method will be called first, so sub will be displayed, then the above statement will display 1 2...



Thanks Ankit .. If it is so simple... Appreciated!!
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Rajiv Chopra wrote:public class Geek {

int i = 1;
public int getI(){
System.out.print("Super");
return i;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Geek ga = new Arabik();
System.out.print( ga.i+" " +ga.getI());
}

}
class Arabik extends Geek{
int i = 2;
public int getI(){
System.out.print("sub");
return i;
}
}

Why the answer is "sub1 2"??
------------------------------------------
-----------------------------------------------------

Object of class Arabik is created which is reference to Class Geek with ref var ga.

W.r.t instance methods if the method of superclass is overidden in subclass.

Then instance method of subclass is called which is overridden.

And w.r.t instance var of super class is called to which a ref is created.

In other words 'Polymorphism'.
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
dimple bav wrote:
Rajiv Chopra wrote:public class Geek {

int i = 1;
public int getI(){
System.out.print("Super");
return i;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Geek ga = new Arabik();
System.out.print( ga.i+" " +ga.getI());
}

}
class Arabik extends Geek{
int i = 2;
public int getI(){
System.out.print("sub");
return i;
}
}

Why the answer is "sub1 2"??
------------------------------------------
-----------------------------------------------------

Object of class Arabik is created which is reference to Class Geek with ref var ga.

W.r.t instance methods if the method of superclass is overidden in subclass.

Then instance method of subclass is called which is overridden.

And w.r.t instance var of super class is called to which a ref is created.

In other words 'Polymorphism'.


Hi Dimple,
Yes I got it. But print statement was strange for me. I was thinking it will print 1 sub2. but Ankit comment are good for that..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Rajiv did you check your private messages??
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Hi,

But i would ask how the print statement goes .

Ok with the methods.

What about instance variables ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

dimple bav wrote:But i would ask how the print statement goes .
Ok with the methods.
What about instance variables ?

What part did you not understand can you specify more clearly...
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Ankit Garg wrote:
dimple bav wrote:But i would ask how the print statement goes .
Ok with the methods.
What about instance variables ?

What part did you not understand can you specify more clearly...


As Rajiv said he was stuck with the print statement.

I m clear with the output 'sub'.
I just gave a thought and i m stuck how both the instance variables are print.

I mean var of Superclass '1' and then var of subclass i.e '2'.
but in print we have 'ga.i' .

so how both var of super and sub ?

Thank you.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17


When you access an instance variable using a reference variable, the value returned is not polymorphic. This article should help you in understanding the concept...
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Geek ga = new Arabik();

System.out.print(ga.getI()+ " " +ga.i); ----------------------> sub2 1

System.out.print("\n\n");

System.out.print(ga.i + " " + ga.getI()); -----------------------------> sub1 2

I m aware that when the ga is the object of class Arabik but has a reference to class Geek.

Since arabik extends geek and both have the method getI which is overridden.

I know method of subclass whos metthod is overridden is called so we have 'sub2' uptill OK.

Then ga.i ------- for this instance variable of superclass is printed i.e '1' because object ga is referenced to Geek.

But

System.out.print(ga.i + " " + ga.getI()); -----------------------------> sub1 2

how sub1 2 ?

how 1 comes in between ?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well good question
i am also having a doubt in that output
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
why? look first the expression inside the print statement will be evalauted...for that first ga.i is evaluated to i....but ti doesnt print anything..becasue first the full expression has be evalauted first....then the method is called...the method is executed....in which sub is printed. ..and then returns 2....after the method returns now the print statemnt has been evaluated....then 1 will be printed and then 2...but before 1 is printed sub as already printed...


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Raju Champaklal wrote:why? look first the expression inside the print statement will be evalauted...for that first ga.i is evaluated to i....but ti doesnt print anything..becasue first the full expression has be evalauted first....then the method is called...the method is executed....in which sub is printed. ..and then returns 2....after the method returns now the print statemnt has been evaluated....then 1 will be printed and then 2...but before 1 is printed sub as already printed...



confusing ?
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Raju Champaklal wrote:why? look first the expression inside the print statement will be evalauted...for that first ga.i is evaluated to i....but ti doesnt print anything..becasue first the full expression has be evalauted first....then the method is called...the method is executed....in which sub is printed. ..and then returns 2....after the method returns now the print statemnt has been evaluated....then 1 will be printed and then 2...but before 1 is printed sub as already printed...


System.out.print(ga.getI()+ " " +ga.i); --------------------------------> sub2 1

System.out.print("\n\n");

System.out.println(ga.getI()); ------------------------------> sub2



System.out.print("\n\n");

System.out.println(ga.i );---------------------------------> 1

System.out.print("\n\n");

System.out.println(ga.i+ " "+ga.getI()); -------------------> sub1 2---------------------------> why this ?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ga.i is 1....not printed till now...print is being evaluted still....getI method called...sub printed...returns 2...ga.getI is evaluted to 2....now the whole print has been evaluted...now its time to print the values....so sub( which was already printed) then 1(ga.i) and then 2(the returned value of g.getI())
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well confusing but finally i got it thanks raju
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Raju Champaklal wrote:ga.i is 1....not printed till now...print is being evaluted still....getI method called...sub printed...returns 2...ga.getI is evaluted to 2....now the whole print has been evaluted...now its time to print the values....so sub( which was already printed) then 1(ga.i) and then 2(the returned value of g.getI())


Thank u.

Got the explaination.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help