I'm trying to call a derived class method with a base class reference but I do not want to implement it in the base class or other derived classes. Only in that one derived class.
One alternative I looked into was to declare the function and base class as abstract. The only problem is if I make the base class and method abstract, I have to implement the abstract method in all of the derived classes. Is there a way to do this where I don't have to implement the abstract method in all of the derived classes and I can just define it in the class where the method makes sense? Keep in mind that I also don't want to implement method this in the base class and I want to call it through a base class reference of a derived object.
"inputSquareFt" is the function that I'm trying to call in the derived class (Landscaping) from the base class (Service) reference "newS" is main.
Thanks
//**THIS IS MY BASE CLASS**
public class Service extends Utility {
//default constructor
public Service() {
this.name = null;
this.cost = 0;
}
//constructor
public Service(String name, float cost) {
this.name = name;
this.cost = cost;
}
public int inputName() {
}
public int inputCost()
{
}
public void display() {
}
//PRIVATE DATA MEMBERS //
private String name;
private float cost;
}
public class Landscaping extends Service {
public int inputCost() {
}
**THIS IS THE FUNCTION THAT I WANT TO ONLY BE DEFINED IN THIS CLASS **
public int inputSquareFt() {
}
public final void display() {
}
private float sqFt;
private float costPerSqFt;
}
public class Main {
public static void main(String args[])
{
**BASE CLASS REFERENCE TO A DERIVED Landscaping OBJECT**
Service newS = new Landscaping();
newS.inputName();
newS.inputCost();
**inputSquareFt() IS THE FUNCTION IM TRYING TO CALL IN A DERIVED CLASS**
newS.inputSquareFt();
newS.display();
}
}