1

I had a interview with one of the companies and they asked me this question?

What are the protocols you follow while designing sub-classes. My answer was if the subclasses share all the properties and methods of the parent class and plus it wants to implement its own methods and properties than implement subclass.

Can someone let me know the list of items that needs to be looked into?

4

2 回答 2

0

SOLID and DRY principles are a good place to start.

于 2013-03-11T15:40:23.320 回答
0

Your answer was a good start, but they probably were looking for more:

For example:

  • Is a subclass the appropriate solution in this case?

    How much of the parent class's functionality will be appropriate for the subclass?

    Are you subclassing at the correct level of granularity: is your subclass 'final' - an endpoint which will represent the end of an inheritance chain, or an intermediate class that will be the parent class for other subclasses to follow?

    Do you need only one subclass, or perhaps several that reflect different aspects of the parent class's functionality and add their own.

    What aspects of the new functionality will be implemented in the subclass itself, and how much might be delegated to other classes or subsystems.

    Sounds simple, but following an appropriate NAMING PROTOCOL for your classes and subclasses is also very important: make sure the name of your subclass correctly describes its functionality and its relationship to the parent class.

Some things to research - the GOF book is a tough read but 'Head First Design Patterns' is more accessible, although sometimes it gets into very elementary details. There may be Python specific design books out there too, but offhand I'm not familiar with them.

Relevant subjects might be:

*) Inheritance

*) Object composition/delegation vs inheritance

*) Abstract classes/Interfaces

于 2013-03-11T16:28:37.227 回答