inheritance - Is it possible to extend Methods in Java? -
i know possible in java:
public class animal {...}
and
public class dog extends animal {...}
then can write whatever dog methods can access animal method.
however, wondering there way extend methods
for example
public void generateanimal() {...}
and
public void generatedog() extends generateanimal() {...}
but not passing compiler. question is:
is possible inherit methods in java?
yes.
public class animal { public void someaction() { system.out.println("from animal class"); } } public class dog extends animal { public void someaction() { super.someaction(); system.out.println("from dog class"); } } public class main { public static void main(string[] args){ animal dog = new dog(); dog.someaction(); } }
output:
from dog class animal class
so extend functionality of method, better use composition instead of inheritance.