473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

OOP Failed?

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.
Jul 21 '05 #1
6 1528
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
"Bharat Sharma" <bh****@ten-technologies.com> wrote in message
news:1e*****************************@phx.gbl...
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.

Jul 21 '05 #2
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.
Jul 21 '05 #3
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

"Bharat Sharma" <bh****@ten-technologies.com> wrote in
news:1e*****************************@phx.gbl...
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.

Jul 21 '05 #4
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

"Bharat Sharma" <bh****@ten-technologies.com> wrote in message
news:1e*****************************@phx.gbl...
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.

Jul 21 '05 #5
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
Jul 21 '05 #6
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
Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mo | last post by:
The code I've pasted below is taken directly from Microsoft's site at http://support.microsoft.com/default.aspx?scid=kb;EN-US;308157 As far as I can tell the error is raised on this line: conn...
0
by: Jeff Reed | last post by:
I am experiencing the the problem outlined the below. Unfortunately, I am using WinXP and I not sure if I can apply the solution due to lack of security control Any feed back would be apreciated ...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
9
by: Bijoy Naick | last post by:
I've implemented forms authentication and authorization on my application. In my Web.Config, my authorization section looks like this.. <authorization> <allow roles="admin" /> <deny users="*"...
13
by: Adam | last post by:
Trying to get an asp.net 2.0 app running and am receiving this error. I see a bunch of people with this error on the net, but no solution: Works fine on my local machine, deployed to a server it...
6
by: ruben | last post by:
Hi: I'm running a large database on PostgreSQL 7.1.3. 20 days ago the database failed with a threatening and not too descriptive error like: pg_exec() query failed: server closed the...
1
by: Laurent Lequenne | last post by:
Hello There, I just converted a VS 2003 C++ Project into VS 2005. I already made some changes in my headers files, has I had compilations errors with enums declarations. Now everything compiles...
2
by: news | last post by:
I just upgraded to PHP 4.4.2 on my Slackware 10.2 system. And Apache/mySQL/PHP all work great through a browser. No errors. But when I try to run a PHP script through the command line, which I...
2
by: Dennis | last post by:
I'm getting the following problem occasionally...the application will work perfectly well for a period of time and then this pops up. I have tried turning off the default proxy in the...
1
by: kencana | last post by:
Hi all, I was wondering why I always get "failed to open stream: HTTP request failed!" error in either loading a normal or xml file. i don't understand why i can't get the whole result. the result...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.