eugene007
asked on
abstract
Lets say I have an abtract class called MedicalFacility with the following abtract method:
public abstract class MedicalFacility
{
public abstract boolean visit(Patient p);
}
Bellow is a method that belong to a class that does not extend from the abstract class MedicalFacility. But within this method it invoked the visit method that is in the subclass (Hospital or HealthClinic) of the abstract class (MedicalFacility).
public boolean present(MedicalFacility m)
{
if(m instanceof Hospital)
return ((Hospital)m).visit(this);
else
return ((HealthClinic)m).visit(th is);
}
When do we exactly use ((Hospital)m).visit(this) or m.visit(this)? What are their differences?.
Your help is kindly appreciated.
Regards
Eugene
public abstract class MedicalFacility
{
public abstract boolean visit(Patient p);
}
Bellow is a method that belong to a class that does not extend from the abstract class MedicalFacility. But within this method it invoked the visit method that is in the subclass (Hospital or HealthClinic) of the abstract class (MedicalFacility).
public boolean present(MedicalFacility m)
{
if(m instanceof Hospital)
return ((Hospital)m).visit(this);
else
return ((HealthClinic)m).visit(th
}
When do we exactly use ((Hospital)m).visit(this) or m.visit(this)? What are their differences?.
Your help is kindly appreciated.
Regards
Eugene
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's right. And in that case you'd have to either verify that reference m is actually a Hospital reference before calling method(this), or trap the exception.
Dorothy
Dorothy
ASKER
How do I explain the reasons for the above in a simple way?.
ASKER
The main reason why im using abstract for my super class is because the subclasses are related to each other.
This is definitely a tough OOP concept to understand, particularly if you are new to OOP.
(1) You can use a child reference to call a parent's method without an explicit cast, because the child class has access to all the parent's methods. If the child class has over-ridden a given method, you will get the child method implementation, otherwise you will get the parent method implementation.
(2) You cannot use a parent reference to call a child's method without an explicit cast. Without the explicit cast, if the parent class has the method implementated, you will get the parent's implementation. If the parent class has not implemented the method, you will get an exception. I believe it's a ClassCastException.
Whatever you do, don't use the word "polymorphism" :) Although technically, that's what it is. If you are talking to developers, I'd go through the simple example I gave you. If you are talking to a non-technical audience, I'd stick with the terms "parent" and "child".
Dorothy
(1) You can use a child reference to call a parent's method without an explicit cast, because the child class has access to all the parent's methods. If the child class has over-ridden a given method, you will get the child method implementation, otherwise you will get the parent method implementation.
(2) You cannot use a parent reference to call a child's method without an explicit cast. Without the explicit cast, if the parent class has the method implementated, you will get the parent's implementation. If the parent class has not implemented the method, you will get an exception. I believe it's a ClassCastException.
Whatever you do, don't use the word "polymorphism" :) Although technically, that's what it is. If you are talking to developers, I'd go through the simple example I gave you. If you are talking to a non-technical audience, I'd stick with the terms "parent" and "child".
Dorothy
ASKER
public abstract class MedicalFacility
{
public abstract boolean visit(Patient p);
}
In this case the method has not been implemeted in the abtract class, rather it has only been define in the abstract class, but implemented in the subclass?
{
public abstract boolean visit(Patient p);
}
In this case the method has not been implemeted in the abtract class, rather it has only been define in the abstract class, but implemented in the subclass?
Right.
ASKER
Therefore I do not need to perform reference casting, simple because the subclass has overriden and implemeted the method of the superclass and therefore I will get the childs method implementation?.
See (1), 2nd sentence.
ASKER
Now I understand. Thanks.
ASKER
((Hospital)m).method(this)