Is this a bad programming paradigm?

Started by
9 comments, last by HappyCoder 11 years ago

So, I have a base class(we can call it A) with lots of function.

Then I create a sub class of A(we can call it B) where a lot of the functions in A are disabled.

Example, here is a function from the sub class B:


public override void setSomething(boolean something)
{
      throw new Exception("This function is not supported in the sub class");
}

Is that bad and unprofessional?

Advertisement

I think it requires a more concrete example, but the general answer is yes. You're violating the substitution principle (and it should seem clear that despite what your hierarchy says, B is-not-an A). What are you trying to do, really?

So, I have a base class(we can call it A) with lots of function.

Then I create a sub class of A(we can call it B) where a lot of the functions in A are disabled.

Example, here is a function from the sub class B:


public override void setSomething(boolean something)
{
      throw new Exception("This function is not supported in the sub class");
}

Is that bad and unprofessional?

Normally, yes.

If the subclass only supports a subset of the baseclass then your base class is most likely bloated or the subclass isn't really meant to inherit from it. (As with everything in programming there may be exceptions).

If you show the actual classes i'm sure we can suggest a "better" solution.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

What you probably want to do is define a new class with only the interface you require, and have a private A member that you forward the calls to. Although you probably only want to do that if you don't have access to the source code for A, if you have access to the source just remove the unneeded methods.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

How about letting Class A output those messages (for all functions), and only override (implement) the available functions in Class B?

This makes A more like an interface than a class, and in that case it could actually consist of pure virtual functions as I see it. (Without implementation)

(My virtual is a little rusty, but I think this qualifies, please tell me if it doesn't)

In OOP we have something called the Liskov Substitution Principle. In short:


public class BaseClass { /* stuff */ }

public class DerivedClass : BaseClass { /* stuff */ }

void someFunction(BaseClass arg) { /* do stuff */ }

//somewhere in main:

DerivedClass foo = new DerivedClass(/*stuff*/);

someFunction(foo); //Under LSP, this should NOT be an error.  Throwing exceptions can cause surprises.

Fundamentally, this is what Polymorphism, one of the "tenets" of OOP, is all about. When you define a subclass or implement an interface, not implementing something that already exists is basically telling the compiler "just kidding!" Compilers don't like surprises.

There's almost certainly a better way around your problem, but without any code we can't really say what it is.

Having a method which is not implemented on a derived class might indicate that you should use multiple interfaces instead, and the class that can't implement that method just doesn't provide that interface.

instead of


public override void setSomething(boolean something)
{
      throw new Exception("This function is not supported in the sub class");
}

use this :


protected override void setSomething(boolean something)
{
}

or


private override void setSomething(boolean something)
{
}

so that the user of the B's class object can't call void setSomething(boolean something)

Maybe you can look for information on public, protected and private keywords ?

[EDIT]

So apparently illegal ... in C#

That's not legal in C# (changing the access level of an overridden function)

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

http://msdn.microsoft.com/en-gb/library/ebca9ah3%28v=vs.80%29.aspx

It wouldn't make sense anyway since you could call it with a reference to the base class. That's why it is illegal.

EDIT: Or is this Java code? I thought override was C# only... but then again it uses boolean not bool so I dunno? I expect changing the access level in Java might be illegal as well for the same reasons, but I'm not sure.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I am not sure why you would ever want to do this. It is like building a car with a brake pedal, but then instead of braking when you press it, the car's check engine light comes on as you barrel through a busy intersection.



This topic is closed to new replies.

Advertisement