0

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

    }
}

ndubs18
  • 11
  • 2
  • `but I do not want to implement it in the base class or other derived classes. Only in that one derived class`. Then that function should not be in base class at all since it looks like from your comment/intention that the function is unique to the subclass and so the base class and other sibling classes do not need to know about it. But if you really wanna do it, then look into [this](https://stackoverflow.com/questions/12620365/java-how-to-optional-override-method-in-abstract-class) – denvercoder9 May 24 '20 at 01:10
  • That's not how type-safe languages work. If you make a method on the base class, you can call it on the base class and any subclass. The solution is also simple; in the method that does know that it's actually a Landscaping subclass, address it as a Landscaping subclass: `Landscaping newS = new Landscaping();` – Erwin Bolwidt May 24 '20 at 01:15
  • @sonnet sorry I meant to leave the function out of the base class for this example. Without it in the base class, how can I call the function through the base class reference in main? Since it's not defined in the base class, I get an error saying that when newS.inputSquareFt(); is executed in main: Cannot resolve method 'inputSquareFt' in 'Service – ndubs18 May 24 '20 at 01:24
  • @ErwinBolwidt I'm not trying to call the function through a Landscaping reference. I want to call is through a Service reference because I'm using dynamic binding. Maybe I have to cast it? – ndubs18 May 24 '20 at 01:28

1 Answers1

0

Downcast the Service reference to Landscaping to tell the compiler that you know for sure it's a Landscaping object, then it will let you call the method. If you're wrong you'll get a ClassCastException at runtime, your penalty for lying to the compiler.

((Landscaping) newS).inputSquareFt();
John Kugelman
  • 349,597
  • 67
  • 533
  • 578