Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help

 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"??
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv did you check your private messages??
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

But i would ask how the print statement goes .

Ok with the methods.

What about instance variables ?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well good question
i am also having a doubt in that output
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well confusing but finally i got it thanks raju
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic