A
A
Alexander Krasnov2015-12-10 01:23:46
Java
Alexander Krasnov, 2015-12-10 01:23:46

How to override a method in a child class with different parameters?

Hello!
The essence of the question: in the parent class there is a setSize () method, how to override this method in the child class to accept an argument? So setSize(double r) for example.
If you just declare a class or interface where this method does not accept anything and is abstract:

interface GeometricalFigures {
    void setSize();
}

That in a subclass it is necessary to implement necessarily a variant without arguments.
Is there any way to avoid this?
for example, to write like this:
class Circles implements GeometricalFigures {
    double radius;
    public void setSize(double r) {
    radius = r;
    }

and it was not necessary to shove the setSize () method without arguments into the Cirles class.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aminought, 2015-12-10
@Pompeius_Magnus

Well, you can try something like

interface GeometricalFigures {
    void setSize(int... args);
}

And it is better to create an abstract class Size, inherit CircleSize from it and wait for Size in the arguments. This is called "polymorphism".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question