1 Replies - 838 Views - Last Post: 15 May 2009 - 09:26 PM Rate Topic: -----

#1 happycamper   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Just a few questions on classes, constructors, and inheritence

Posted 15 May 2009 - 08:41 PM

Is it considered bad object oriented programming to have subclasses that contain only constructors? The parent class has all the methods each class needs and none of the subclasses need to override the methods of the parent class. My instincts tell me to break up functionally when ever possible but having a class that contains only a constructor just seems odd to me. I could easily do that in the parent class, just shove all the constructors into them but then the code seems cluttered.

Sub questions- Is there a point where a constructor gets too big? I have a constructor that is about 43 lines. It only initializes three variables but one of them takes a somewhat large if/ifelse/else statement. But I honestly can't think of a better way to do it.

Is This A Good Question/Topic? 0
  • +

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5223
  • View blog
  • Posts: 14,000
  • Joined: 18-April 07

Re: Just a few questions on classes, constructors, and inheritence

Posted 15 May 2009 - 09:26 PM

Well first of all, a class with only constructors is a useless class. The constructors will allow you to create the object, but then what? If it has no functionality of its own, what advantage does it serve over the parent? So yes, you are right to think that a class with only constructors is a bit odd, because it is.

It should also be a red flag to you that perhaps there is a design consideration perhaps you are missing. The purpose of having the sub class is to make what they call an "is a" relationship. It "is a" special type of its parent. So if the parent is a Melon, the sub class should be a special type of melon like a watermelon. Your melon class should not have everything inside it to make it all types of melons. The parent shouldn't have red flesh and orange flesh and white flesh all at once. The parent should just have "flesh" and your subclass watermelon should specify red flesh. Your subclass cantaloupe should have orange flesh etc.

So if you see your parent getting a bit bloated, find out what is the basics of the class and push the specific details down to subclasses.

As for the length of your constructor, they can get pretty complex and long. But like any function, you can apply what they call "refactoring" and move a lot of those lines into their own functions and call those functions from the constructor. This essentially reduces the complexity of the function by making it smaller and more self contained in individual functions.

If your constructor opens a file, reads it, loads its variables, and closes the file, perhaps you can have the file opening and closing in the constructor and move the file reading to a function where your constructor passes it a file pointer to read from. But do try to keep constructors simple and enough to just instantiate the object. Remember if a constructor is really long it will slow down the creation of the object. Set the state of the object and that is it.

Look up the term "refactoring" for more information and techniques for breaking the code down into manageable parts and into separate functions and patterns.

Good luck to you. :)

This post has been edited by Martyr2: 15 May 2009 - 09:29 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1