child class object in unable to run overridden method instead parent class method is being run both times
Solution 1:
You are trying to override a method called m
which returns a double
which doesn't exist in the parent class, the parent class has a method m
with a return type of float
, these are different. Basically the current @Override
doesn't actually override anything since a method with that name and return type doesn't exist in the parent.
You fulfil the interface contract because a method m
exists which returns a double.
The reason why you get the same output is because you are calling the Parent
classes m
function because of the type of argument you are passing in (10 is being determined as a float). If instead you call the function with a double, i.e 10d then the Child
classes m method will be called.
Change your main method to the following and it will now call the parent and child methods:
public static void main(String[] args) {
Child child = new Child();
child.m(10.0);/*child object running parent method*/
Parent g = new Child();
g.m(10);
}