0

Suppose I have a parent class A and a daughter class B. Now A has 10 methods in it and B requires only three of those methods. How can this be done?

jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Jun 18, 2015 at 19:15
3
  • 4
    Can't B just ignore the other seven methods? Or could you refactor the shared methods into a superclass that both A and B share, making them siblings? If you want to override inherited methods in a class, just implement them within that class definition. Commented Jun 18, 2015 at 19:42
  • 2
    The Liskov substitution principle requires that a subtype B of A has at least the interface of A. If you don’t want that, then you probably don’t want B to be a subtype of A. Commented Jun 18, 2015 at 19:47
  • Make a parent class that has the 3 methods in it. Inherit the class A and class B from the parent. class A will keep its 7 methods and class B wont know anything about them. Similar to @jonrsharpe suggestion (I +1'd it) , but I advocate this approach to just "ignoring" the other seven methods, since if you are not the only developer then someday someone might forget why they are "ignoring" them. Commented Jun 18, 2015 at 22:33

1 Answer 1

1

There is nothing special to do.

Just inherit class A:

class B(A):
 super(B, self).__init__():

Use/override the methods you need and ignore the rest.

answered Jun 18, 2015 at 19:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.