Object reference

F

frank

I'm new bee to java . In the below program I had created the parent
object in the
reference of child class. When the object creation is inside the
method. I
am not getting any error. If the object creation is in the class
level. I am
getting the error in accessing the method. tel me Why can't
access the the method in side the class..?
Thanks In Advance...


class Employee {

public void ss(){
System.out.println("Testing The Casting");

Employee mn = new Manager();
Manager t = new Manager();

Manager kk = (Manager) new Employee();
kk.manage();
}

{ " Manager kk = (Manager) new Employee();
kk.manage(); " } -------------- Error in this line..

}


class Manager extends Employee {


public void manage() {
System.out.println("Managing ...");
}

}
 
E

Eric Sosman

frank said:
I'm new bee to java . In the below program I had created the parent
object in the
reference of child class. When the object creation is inside the
method. I
am not getting any error. If the object creation is in the class
level. I am
getting the error in accessing the method. tel me Why can't
access the the method in side the class..?
Thanks In Advance...


class Employee {

public void ss(){
System.out.println("Testing The Casting");

Employee mn = new Manager();
Manager t = new Manager();

Manager kk = (Manager) new Employee();

You will get an error here when (if) the code executes.
Every Manager is an Employee, but only some Employees are
Managers. The `new Employee()' piece creates an Employee,
and Java won't let you assign it to `kk'. That's because
`kk' can only point to a Manager, not to an unspecialized
run-of-the-mill Employee. So you insert the `(Manager)' cast,
which means "Java, I happen to know something you don't: this
particular Employee is not just any old Employee, but in fact
a Manager. Trust me." Java believes you and accepts the code.
But at run time when the Employee turns out *not* to be a
Manager, even though you claimed it would be, Java throws a
ClassCastException, whch means (more or less) "You lied to me,
you faithless filthy fibber, you!"
kk.manage();
}

{ " Manager kk = (Manager) new Employee();
kk.manage(); " } -------------- Error in this line..

Get rid of the " quotation marks " -- I don't know what
you thought they would do, but they don't belong here. After
that, I think the code will compile.

... but will fail at run time, for exactly the same reason
as the code inside `ss': You've told Java that the Employee is
in truth a Manager, but it's not. ClassCastException again.

If you change `new Employee()' to `new Manager()' so you'll
have an actual Manager instance to work with, you'll run into
yet another problem. This nameless snippet of code runs as part
of the initialization of every new Employee, and it creates (we
suppose) a Manager. Since a Manager is also an Employee, the
code runs on behalf of that Manager/Employee and creates yet
another Manager. That second Manager is also an Employee, for
whom yet another Manager will be created, and another, and
another, and ... Can you say "OutOfMemoryException"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top