Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!

Meghana Ghanekar

Greenhorn
+ Follow
since Oct 24, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Meghana Ghanekar

Hi Arun!
If the refernce is for current object this means that i can access the things that are not declared in base but are in derived. but this is not possible this still is a base refernce the call to method is a call to virtual method thus it is resolved at runtime. this creates a problem.
It is also said that the call is resolved using method table . Can anyone suggest me some book to refer where i can get more information about this method tables.

Thanx,
Meghana.
15 years ago
Hi!
It is said that by default all methods of a class are virtual & the calls to the methods that are not private static or final are resolved at runtime.but in my Code this condition creates som problem.Please refer the code below:

class Base
{
Base()
{
System.out.println("in Base constructor");
draw();
}
void draw()
{
System.out.println("in base draw");
show(); //want to call the show of base
}

void show()
{
System.out.println("in base show ");
}
}
class Derived extends Base
{
Derived()
{
System.out.println("in derived constructor");
}
void draw()
{
System.out.println("in derived draw");
super.draw();
}
void show()
{
System.out.println("In derived show");
}
}

public class DynamicDemo{
public static void main(String[] args)
{
Derived d = new Derived();
d.draw();
}
}

I expected the output as:
// o/p for the constructor calls while creating object
In Base Constructor.
In Base Draw
in Base show
In derived constructor
// o/p for derived's draw
In derived draw
In Base draw
In Base show

But the output that i have got is as follows
//o/p for the constructor calls while creating object
in Base constructor
in derived draw
in base draw
In derived show
in constructor of derived
//o/p for derived's draw
in derived draw
in base draw
In derived show

I did understand the output it is like this because draw & show are virtual methods of Base & are overridden by Derived. Since the object is of type derived all the calls are resolved dynamically & thus are for Derived class.
But logically when i call any method of base in another method i expect it to be resolved for Base only & not for Derived. But it does not work this way. Is it an Flaw in the Language or it has got any special significance. Or is their any other reason.

Warm Regards,
Meghana.
15 years ago
Thanx Everyone,
I got my answer.

Meghana
15 years ago
Hi!
I agree with u. It will generate an exception. But it is said that when u pass an object to system.out.println() then implicitly tostring() on that object is called. In that case no Exception is generated. but when i try to call it explicitly call it for the object Exception is generated.
i can't understand why it is like this?


Warm Regards,
Meghana
15 years ago
Hi!
While developing the class for Account Holder. I am trying to contain an object of Date class inside the Account Holder object. the Code is as follows:
class Date
{
private int dd,mm,yy;

public Date()
{
dd = mm = yy=1;

}
public Date(int d,int m,int y)
{
dd = d;
mm = m;
yy = y;
}
public String toString()
{
return dd+"/"+mm+"/"+yy;
}

public void disp()
{
System.out.println(dd+"/"+mm+"/"+yy);

}
}


class AccHolder
{
int emp_id;
String name;
Date jng_dt;
float sal;

public AccHolder()
{
}

public AccHolder(int i,String n, Date j,float s)
{
emp_id = i;
name = n;
jng_dt = j;
sal = s;
}

public String toString()
{
return emp_id+"/n"+name+"/n"+jng_dt+"/n"+sal;
}

public void disp()
{
System.out.println(emp_id+"/n"+name+"/n"+jng_dt+"/n"+sal);
}

public void disp1()
{
System.out.println(emp_id+"/n"+name+"/n"+sal);
jng_dt.disp();
}
public static void main(String[] args)
{
AccHolder a1 = new AccHolder();
System.out.println(a1);
/* for a1.jng_dt is displayed as null */
AccHolder a2 = new AccHolder();
a2.disp();
/* for a2.jng_dt is displayed as null */
AccHolder a3 = new AccHolder();
a3.disp1();
/* Runtime exception thrown for jng_dt as it is null*/

}
}

If it is said that when we pass Object directly to the System.out.println(). Then internally toString for that Object is called which is a method call. Then why disp() of date generates Exception @ runtime. also if we explicitly call toString() of Date as (jng_dt.toString()) in disp() of Account Holder runtime exception is generated.
Can u help me out for this ? I cannot understand this problem.
thanx,
meghana
15 years ago
Hi!
I am trying to write code for a Calculator.In its GUI I want the textfield that accepts input as well as displays result to be oriented from right to left. I m not able to do this? setComponenetOrientation method of component class is not working. No error given. But no change in the orientation.Can u help me our?

Warm Regards,
Meghana
15 years ago