OOP Failed?

B

Bharat Sharma

Hello All Wise Persons.

I don't know wheather my question is okay or i need to
revisit my OOP fundamentals. AS we have read till now in
OOP that Every Parent Class can see and execute the
Methodas of the object it points to (be it the object of
self or the derived class). However in case of .NET and
Java for that matter, All objects are derived from
System.Object which lets them execute the methods at run-
time i.e. Late Binding. So how come this is happening.
Please clear my doubt or else provide me a good link to re-
visit OOP Fundamentals.

Thank You,
Bharat Sharma.
 
N

Nick Malik

Not sure I understand you.

An object is declared with a type. The caller can use any methods or
properties that are intrinsic to that type. If a derived type is provided,
the calling code cannot see the methods of the derived type without casting
the datatype.

Therefore:
class A
{
void ABC() { // something useful }
}

class B : A
{
void XYZ() { // something else }
}

class C
{
A myA;
B myB = new (B);

myB.XYZ(); // this is valid
myA = myB; // this is valid
myA.XYZ(); // this is not valid, since type A has no knowledge of the
XYZ method.

}

Many design patterns rely on the ability of an interface to represent a
contract that can be extended, but that the calling application may not need
to know about the derived classes. This is called the Liskov Substitution
Principle.

See http://www.objectmentor.com/resources/articles/lsp.pdf
for details.

Hope this helps.
--- Nick
 
B

Bharat Sharma

I am sorry, I should make it more clear. I just want to
know exactly what is happening behind the scenes in the
following code. (Code is in VB.NET)

Module MyModule

Class SomeClass

Public Sub MethodA()
MsgBox("SomeClass::MethodA()",
MsgBoxStyle.Information)
End Sub

End Class

Public Sub Main()
Dim objTmp As Object = New SomeClass
objTmp.MethodA()
End Sub

End Module

As per my knowledge of OOP Principals Since Object class
is parent of Every class defined in the .NET Framework
based applications.

Now take a look at the following C++ Program which doesn't
even compile.

#include "stdafx.h"
#include "iostream.h"


class base
{
int x;
public:
void basefun()
{
cout<<"Base function in base
class"<<endl;
}
};

class derv : public base
{

public :
void basefun()
{
cout<<"Base function in derived
class"<<endl;
}
void dervfun()
{
cout <<"derived function :"<<endl;
}
};

void main()
{
base *ref;

ref = new base;
ref->basefun();
delete ref;

ref = new derv;
ref->dervfun();
delete ref;

}

while i try to compile this in VC++ i get the following
error.

"error C2039: 'dervfun' : is not a member of 'base'"

Best,
Bharat Sharma.
 
N

Niki Estner

I think you got that wrong...
A derived class has access to all non-private members of its parent class.
The other way round is not generally possible. If a derived class overrides
a virtual member of it's parent, the parent will actually call the
overridden version (without even knowing). That's the only OOP-interaction
from parent to child.

Late binding is a VB thing, not an OOP thing; It doesn't have to do anything
with inheritance, it's possible due to reflection (Have a look at
Object.GetType/Type.InvokeMember).
In strongly typed languages this is not possible, and in VB it's considered
"bad style" (and even forbidden with "option strict")

Niki
 
N

Nick Malik

As Niki pointed out, if you set Option Strict in your VB.NET program, you
would get the same behavior in VB as you are getting in C++.
Note: In C#, the first example wouldn't compile either.

--- Nick
 
C

Cor Ligthert

Hi Bharat,

Maybe it is better to ask this kind of questions in one of the program
language groups, in this case
microsoft.public.dotnet.languages.vb

As said by the others are you using late binding. the VisualBasic Net part
is doing things for you at run time.
The code should have be to be correct (and have a better proces time).

Module MyModule
Class SomeClass
Public Sub MethodA()
MsgBox("SomeClass::MethodA()", MsgBoxStyle.Information)
End Sub
End Class

Public Sub Main()
Dim objTmp As Object = New SomeClass
DirectCast(objTmp, SomeClass).MethodA()

'However to take a better aproach and not just from
'classic program languages, do than direct this.
Dim mySomeClass As New SomeClass
mySomeClass.MethodA()

End Sub

End Module

I hope this helps?

Cor
 
B

Bharat Sharma

Hello Nikki and Nick,

Thanks Buddies.

Some times one needs to re-visit basics. :D

I realised this stuff after 6 years of exp. in
development. however as per ur suggestions i have been
able to call the code this way:

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim objTmp As Object = New Abc
objTmp.GetType.InvokeMember("MethodA",
BindingFlags.DeclaredOnly Or BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Instance Or
BindingFlags.InvokeMethod, Nothing, objTmp, Nothing)
End Sub

Class Abc
Public Sub MethodA()
MsgBox("Abc::MethodA()", MsgBoxStyle.Information)
End Sub
End Class

Thanks and Best Regards,
Bharat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top