Wednesday, December 22, 2010

method overriding enhancements - strong type cast needed

C++, Java both don't have full power of node oriented programming.
In Java:
Suppose class B extends A.
A a = new B();
((A)a).foo(); // still calls class B's foo() method.
There is a need to call class A's foo method at runtime preserving data maintained in class B object.

Strong Type Cast is needed in java.
May be something like:
((A strong)a).foo(); should call A's foo() method.
This is case where If a subclass overrides superclass 20-30 methods, and when a subclass instance is passed to a utility method that needs to invoke one specific instance of superclass method.

C++ does allow fully qualify specific superclass method call using A::foo().

What could still be lacking in C++?
strong type cast of an entire object so that the object reference passed to utility method will use all specific type casted super class methods while preserving subclass data.

method invocation could be enhanced like following:

callAnyMethod(&MyObjSub2 arg);
should be enhanceable to
callAnyMethod(&MyObjSub2[MyClassSub1] arg); where [MyClassSub1] represent a strong type cast. or could be mutiple orderings could be given: [MyClassSub1,MyClassSub2]

method definition is
void callAnyMethod(MyClass* p){
p->overridMeth(); // will execute MyClassSub1 method in hierarchy as it is mentioned in the strong type cast in pass by reference

Class Hierarchy is:


java hierarchy:
class A{
void foo(){
System.out.println("hello A");
class B extends A{
void foo(){
System.out.println("hello B");

Computing has two things: 1. storage, 2. processing
What humans wanted to do was stored in ROM/punched cards as assembly language statements or java language statements which get converted to fast electric signals used by processing.


  1. c++ allows more combinations between instructions(functions) and data(variables) than java.
    eg in c++:
    base* b;
    sub* s; // sub extends base and both have method foo()..
    b -> foo(); // will call base foo()
    s -> foo(); // will call sub foo()
    s ->base::foo(); // will call base foo()

    java didn't add this mostly -because functions in super class might access 'private members' of super class which there is no way populating in subclass object and throw Null Exception..
    Yet again - if these specific functions in superclass that could be accessed from subclass were compile time checked as not to access any private variable of super class, this feature may add meaning.

    There could be more better design than just preventing subclass not to access super class methods..

  2. This comment has been removed by the author.