473,714 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call the subclass's method?

Hi,

I created a class "myClass" and another class "mySubclass " which inherited
from "myClass". In the "mySubclass ", there is a public method named
"methodOfSubcla ss(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubcla ss(...)", like

.......
myClass mc;
mc = new mySubclass();
mc.methodOfSubc lass(...);
.......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubcla ss'

What is the problem? Do I have to create a virtual method
"methodOfSubcla ss(...)" in "myClass" and override it in "mwSubclass "? Any
other solutions?

Thanks

Q.

Nov 16 '05 #1
4 7413
but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubcla ss'

What is the problem?
Exactly what the error message says.

Do I have to create a virtual method
"methodOfSubcl ass(...)" in "myClass" and override it in "mwSubclass "?
That's one way to do it. If it makes sense for the base class to have
such a method.

Any other solutions?


((mySubclass)mc ).methodOfSubcl ass(...);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

"Quentin Huo" ...
......
myClass mc;
mc = new mySubclass();
mc.methodOfSubc lass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition
for 'methodOfSubcla ss'

What is the problem? Do I have to create a virtual method
"methodOfSubcla ss(...)" in "myClass" and override it in
"mwSubclass "?
That's one way...
Any other solutions?


The type of the variable controls what you can access through it. The *only*
things you can access is what is defined in the variable's type, which can
be a class or an interface.

The simplest is to let the variable be of the subclass type from the
beginning.

mySubclass mc;
mc = new mySubclass();
mc.methodOfSubc lass(...);

Another way is to cast to another variable of the subclass type:

myclass mc;
mc = new mySubclass();

mySubclass msc = (mySubclass) mc;
mc.methodOfSubc lass(...);

Or you can use the "virtual/override" thingy...

// Bjorn A
Nov 16 '05 #3
If the method is unique to the subclass you may not want it virtual as other
subclasses may not need it. Have you tried to cast the object to the
subclass type like this:

mySubclass msc = (mySubclass)mc;
msc.methodOfSub class();

--
William Stacey, MVP

"Quentin Huo" <q.***@manyworl ds.com> wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl...
Hi,

I created a class "myClass" and another class "mySubclass " which inherited
from "myClass". In the "mySubclass ", there is a public method named
"methodOfSubcla ss(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubcla ss(...)", like

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubc lass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubcla ss'

What is the problem? Do I have to create a virtual method
"methodOfSubcla ss(...)" in "myClass" and override it in "mwSubclass "? Any
other solutions?

Thanks

Q.


Nov 16 '05 #4
Try something like

if(mc is mySubclass)
{
(mc as mySubclass).met hodOfSubclass(. ..);
}

paul
"Quentin Huo" <q.***@manyworl ds.com> wrote in message
news:uC******** ********@tk2msf tngp13.phx.gbl. ..
Hi,

I created a class "myClass" and another class "mySubclass " which inherited
from "myClass". In the "mySubclass ", there is a public method named
"methodOfSubcla ss(...)" which is not defined in "myClass".

In a client program I tried to call the "methodOfSubcla ss(...)", like

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubc lass(...);
......

but I got an error message when I tried to compile it:

'...myClass' does not contain a definition for 'methodOfSubcla ss'

What is the problem? Do I have to create a virtual method
"methodOfSubcla ss(...)" in "myClass" and override it in "mwSubclass "? Any
other solutions?

Thanks

Q.

Nov 16 '05 #5

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

Similar topics

1
2419
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using either the list.extend method or even by having the subclass create a whole new list in the variable. The following example illustrates the situation.
4
11997
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
2
4353
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
1
4998
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class Subclass(Superclass): pass
31
3553
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i run my program on , it should be able to find "host names" and "ip addresses" of the other machines; , and ? i have read some threads posted on this group, however, they only work for localhost, not the entire network.
2
1650
by: Ryan Liu | last post by:
Hi, There is a method Test() implemented in base class and override in subclass. In base class itself, how to make sure this.Test() will just call implementation in base class only? Thanks a lot! Ryan Liu
5
3632
by: pixiedustjt | last post by:
Hello, This is my first time to post on the forum, so I hope I'm doing this correctly. I am working on the dreading Java Inventory Program that seems to be legendary. I am currently on part 3 and have most of the program working. I created an additional feature (book author) in a subclass, which is supposed to be called in my main class and populate my array. I've created the subclass and added the new feature, but can't figure out how to...
6
14875
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
44
2938
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
0
8802
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8711
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5948
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4726
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2111
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.