*
The moose likes Beginning Java and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors" Watch "Constructors" New topic
Author

Constructors

asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
why the o/p of the follwing is
3
3
rather than
4
3
Can any one please explain ?
class Fruit{
int i=3;
int j=4;
Fruit(){
System.out.println( getI());
}
int getI() {
return j;
}
}
class Mango extends Fruit{
Mango(int x, int y , int z) {
}
int getI() {
return i;
}
}
public class Apple {
public static void main(String argv[]){
try {
Mango m = new Mango(1, 2, 3);
System.out.println(m.getI());
} catch(Exception e) {
}
}
}

Regards
Asheesh
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
It is because i in Mango is shadows the i in Fruit. Put another way - while methods can be overridden, member variables can't. So even though you call the getI in Fruit the i variable that is returned is the one in Mango.
I can't recall the section of the Java Language Spec this is in but it is covered in Chapter 3, Subclasses & Inheritance section of my Java in a Nutshell (1.1 version).
Hope this helps.
John


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
class Fruit{
int i=3;
int j=4;
Fruit(){
System.out.println( getI());
}
int getI() {
return j;
}
}
class Mango extends Fruit{
Mango(int x, int y , int z) {
}
int getI() {
return i;
}
}
public class Apple {
public static void main(String argv[]){
try {
Mango m = new Mango(1, 2, 3);
System.out.println(m.getI());
} catch(Exception e) {
}
}
}
In class mango you are caling the ctor. of threee arguments which call your basr class default ctor. in that ctor there is
Fruit(){
System.out.println( getI());
}
so it will print first time 3 on console and after that you are calling
System.out.println(m.getI());
getI() is Inherited method from base fruit in it will return i again and next time on your console .

Please correct if i am wrong .
asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
Vishwas my question is why the getI method of
class Mango is called in the constructor of class Fruit
ie why it is printing 3 for the first time & not 4 ?
Regards
Asheesh
Harish Babu.N
Greenhorn

Joined: Oct 12, 2000
Posts: 11
That means if i extend a base class,and create a object of sub class will a new object of base class is created?
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Now it's more confusing for me after doing some research on this i find this. just see this

//-----------------------------------------------------------
class Fruit{
int i=3;
int j=4;
Fruit(){
System.out.println(getI() );
System.out.println("From fruit Ctor" );
}
int getI() {
System.out.println("From Fruit getI()");
return j;
}
}

class Mango extends Fruit{

Mango(int x, int y , int z) {
System.out.println("From Class Mango(int , int ,int)");
}
int getI() {
System.out.println("From Class Mango getI()");
return i;
}
}
public class Apple {
public static void main(String argv[]){
try {

Mango m = new Mango(1, 2, 3);
m.getI();
} catch(Exception e) {
}
}
}
//-----------------------Out put--------------------------------
From Class Mango getI()
3
From fruit Ctor
From Class Mango(int , int ,int)
From Class Mango getI()
//-------------------------------------------------------------
Why it is not Displaying the message inside Mango( int , int ,int ) ctor.
and after printing this(--From Class Mango getI()--)
line it's displaying 3 . which is in the ctor of base class
Why now it's going to ctor of base class.and then come to ctor of mango and displaying (--From Class Mango(int , int ,int)--)

and then getI() of mango class ...

Please explain me what hell is going on please

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi Vishwas,
The things that take place while constructing a class instance are as follows
1) class initializers are executed
2) superclass constructor is called
3) body of constructor will be executed
consider following hierarchy.


Now suppose u say
new Mango();
what happens is as follows.
1) first of all the initializers in class Mango will be executed.
2) super class constructor will be called. now since u r not calling any overloaded constructor so call to default constructor of Fruit is made.
3) initializers of Fruit class are executed i.e. i=3 and j=4
gets executed.
4) since Fruit class has got Object as it's super class so constructor of Object class is called.which does nothing and returns simply.
5) Now body of Fruit constructor is executed. Now look here a call to function getI() is made. now since getI() is overriden in class Mango so this call is passed to Mango version of getI().
so u get a print out of
From Class Mango getI()
and getI() returns a value 3 to Fruit constructor.
6) and u get your next print out
3
From fruit Ctor
7) now since body of Fruit Constructor is over so now the body of Mango constructor will be executed.and here u get the printout
From Class Mango(int , int ,int)
8) now the construction of Mango is over.....
9) getI function is called from main method which will give u the print out
From Class Mango getI()

hope all this was of some help
if any confusion do get back
regards
Deekasha


[This message has been edited by deekasha gunwant (edited December 14, 2000).]
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
hi deekasha .
thanx for giving such a nice explanation thanx a lot.
i got one more confu. in java there is two type of initializers
1)static 2)non-static

in which catogry we can put initializatoin and declaration of instance variable like this (in our example)
int i = 3;
int j = 4;
(and why it's is not possible in c++ which is also oop's language and why java allow this thing)
 
 
subject: Constructors
 
Similar Threads
Forward Refrence
initialization of final instance fields
Unable to understand this behavior...wht is happening??
a question..
Generic Binary Search for Ints & Strings