Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
What is meant by D blocking of mobile?
I like to know details about D blocking of mobile phones. Can we do this ourselves?
3 Answers
- Anonymous1 decade agoFavorite Answer
Here's an idea that I've been aching for in several different
languages: method blocking.
I often create base classes that will have lots of different kinds of
derived classes. To avoid duplicating code, I put all the method
definitions in the base class, so that they can be inherited by the
derived classes that really need them. But not all of the derived
classes should have access to all of the methods in the parent class,
and I'd like to block them.
Take a look at this code:
class baseClass {
public void doSomething() {
...
}
public void doSomethingElse() {
...
}
}
class derivedClassA: baseClass {
public void doSomethingFun() {
}
}
class derivedClassB: baseClass {
block void doSomething();
public void doSomethingSilly() {
}
}
In this example, my base class has two methods: "doSomething()" and
"doSomethingElse()". The class derivedClassA inherits both of these
virtual functions, but the class derivedClassB specifically blocks the
use of the "doSomething()" function. Attempting to call this function
should result in a compiler error, not in a runtime exception.
In the past, I've used exceptions to define class hierarchies like
this. I'll define the methods that I need in the base class, and then
I'll overload those functions in the derived classes, something like
this:
class derivedClassB: baseClass {
public void doSomething() {
throw new Exception("Cannot call doSomething from a
derivedClassB object");
}
public void doSomethingSilly() {
}
}
But throwing exceptions is costly, and besides, I should be able to
know AT COMPILE TIME if I'm calling methods that shouldn't exist
anyhow.
I've been wishing for functionality like this for ages, and I can't
fathom why it isn't available. It seems like it would be easy to use
the "block" keyword to simply remove an entry from the v-table,
wouldn't it?