user6427267 user6427267 - 8 months ago 38
Java Question

Inheritance in Java - Extend only some methods from parent class

I have a question on Inheritance in Java. Is it possible to inherit only some of methods from base class that a Sub Class extends?

For example, I have class A which has 10 methods, class B extends A but I need to inherit only 5(1,2,3,4,5) methods of A, and Class C which also Extended from A. I need to inherit 3 methods, say (8,9,10), can any one please help me to solve this issue?

If this is possible, please clarify with Code.


If you are talking about extends I'm assuming you're not talking about an interface? If so, you can just override the functions.

EDIT: more info as requested. If you extend a class, say:

public class Dog extends Animal {}

You can just override the methods in Animal. Say Animal has a method called walk:

public void walk() { x += 1; }

You can override this in the class Dog with:

public void walk() { x += 2; }

When you now call new Dog().walk() it will call the walk method from the Dog class, not from the Animal class.