473,406 Members | 2,710 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,406 software developers and data experts.

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
"methodOfSubclass(...)" which is not defined in "myClass".

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

.......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
.......

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

'...myClass' does not contain a definition for 'methodOfSubclass'

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

Thanks

Q.

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

'...myClass' does not contain a definition for 'methodOfSubclass'

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

Do I have to create a virtual method
"methodOfSubclass(...)" 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).methodOfSubclass(...);

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.methodOfSubclass(...);
......

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

'...myClass' does not contain a definition
for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" 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.methodOfSubclass(...);

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

myclass mc;
mc = new mySubclass();

mySubclass msc = (mySubclass) mc;
mc.methodOfSubclass(...);

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.methodOfSubclass();

--
William Stacey, MVP

"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:uC**************@tk2msftngp13.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
"methodOfSubclass(...)" which is not defined in "myClass".

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

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
......

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

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" 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).methodOfSubclass(...);
}

paul
"Quentin Huo" <q.***@manyworlds.com> wrote in message
news:uC****************@tk2msftngp13.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
"methodOfSubclass(...)" which is not defined in "myClass".

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

......
myClass mc;
mc = new mySubclass();
mc.methodOfSubclass(...);
......

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

'...myClass' does not contain a definition for 'methodOfSubclass'

What is the problem? Do I have to create a virtual method
"methodOfSubclass(...)" 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
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...
4
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
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
1
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...
31
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...
2
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? ...
5
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...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
44
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...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
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.