Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8111117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:59:26+00:00 2026-06-06T01:59:26+00:00

What are the proper ways of adding a common set of method/properties to subclasses

  • 0

What are the proper ways of adding a common set of method/properties to subclasses derived from the same class?

Let’s say that these are my classes that I need for a videogame based on animals (it’s just an example, I am not really writing a game but the idea is the same)

class Animal():
    def __init(self, numlegs):
        self.numlegs = numlegs

class Dog(Animal):
    def __init(self, numlegs):
        Animal.__init__(self, numlegs)

    def bark(self):
        print "Arf!"

class Cat(Animal):
    def __init(self, numlegs):
        Animal.__init__(self, numlegs)

    def playwithmouse(self):
        print "It's fun!"

class Duck(Animal):
    def __init(self, numlegs):
        Animal.__init__(self, numlegs)

    def fly(self):
        print "Weee!"

At a certain point in the game Dog and Duck can be weaponized, and they need to have the same methods: fire(), reload() and properties: ammocount.

I don’t think it would be correct to add them in the Animal() class, because they will be needed in a completely different part of the game.

What would be the correct approach to add them?

update
I wanted to add that the expected result is some kind of
Animal – Weapon Hybrid Class like

class BurtalDuck(Duck):
    fire(): #<- this is somehow added to Duck
        print "ratatatatat!"

What I get from the answers is that if I need the whole set of “weapons/ammos” I can use multiclassing, otherwise composition is the way to go.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:59:28+00:00Added an answer on June 6, 2026 at 1:59 am

    One possibility: don’t add them to the classes at all. Create a “weapon” class that contains all of the info regarding how weapons work, and add an instance of that class to the objects that can be weapons.

    Of course, this means a bit rethinking of your architecture, but it’s a nice concept to think about. This idea typically goes by the name “component-based”. Google for “component-based game objects” and try to find some blog posts/presentations that describe the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
While searching for a proper way to trim non-breaking space from parsed HTML, I've
I am debating the proper, OO-design to use another object's functionality (methods) from a
Adding support for Unicode passwords it an important feature that should not be ignored
Let's say I have the following list of items: List<Item> items = Item.GetSomeItems(); And
I realize that there are much more elegant ways to do this, and I
I know there are ways to send email from terminal in Linux/MacOS, but I
I want to be able to create a class (in Python) that once initialized
EDIT : proper solution: void add(Student s) { if(root == null) root = new
What is the proper (browser agnostic) way to disable page caching in ASP.NET MVC?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.