• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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(this);
}


When do we exactly use ((Hospital)m).visit(this) or m.visit(this)? What are their differences?.

Your help is kindly appreciated.

Regards

Eugene
0
eugene007
Asked:
eugene007
  • 6
  • 5
1 Solution
 
dorothy2Commented:
You don't need the reference casting here. The appropriate method will be called without it.

See below:

public abstract class MedicalFacility {
    public abstract void visit();
}

public class HealthClinic extends MedicalFacility {

      /* (non-Javadoc)
       * @see code_examples.MedicalFacility#visit()
       */
      public void visit() {
            System.out.println("This is a health-clinic reference.");

      }

}

public class Hospital extends MedicalFacility {

      /* (non-Javadoc)
       * @see code_examples.MedicalFacility#visit()
       */
      public void visit() {
            System.out.println("This is a hospital reference.");

      }

}


Now I have a test program which calls the visit() method from the abstract class, and the two concrete implementations:

public class TestHarness {

      public static void main(String[] args) {
            Hospital hospital = new Hospital();
            hospital.visit();
            
            HealthClinic clinic = new HealthClinic();
            clinic.visit();
            
            MedicalFacility facility = hospital;
            facility.visit();
            
      }
}

Here is the output:

This is a hospital reference.
This is a health-clinic reference.
This is a hospital reference.

BTW - if you are only implementing abstract methods of MedicalFacility, why not make it an interface rather than a base class? That's exactly what an interface does.

Good luck,

Dorothy
0
 
eugene007Author Commented:
suppose if the method is not found in the abstract class (MedicalFacility) but can be found in the subclass (Hospital) of the abstract class (MedicalFacility), then it means I would be required to do as below:

((Hospital)m).method(this);
0
 
dorothy2Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eugene007Author Commented:
How do I explain the reasons for the above in a simple way?.
0
 
eugene007Author Commented:
The main reason why im using abstract for my super class is because the subclasses are related to each other.
0
 
dorothy2Commented:
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
0
 
eugene007Author Commented:
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?
0
 
dorothy2Commented:
Right.
0
 
eugene007Author Commented:
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?.
0
 
dorothy2Commented:
See (1), 2nd sentence.
0
 
eugene007Author Commented:
Now I understand. Thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now