473,625 Members | 3,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

method inheritance question

In the following code, as I understnad it, the resulting
display would be "I am B", because I have overridden the
constructor in B. However, what if what I really want to
do is display "I am A" followed by "I am B', so that A's
constructor is called, followed by B's constructor? Also,
does it work differently for constructors than it does for
other overridden methods?

public class A : System.Object
{
public A()
{
HelloWorld();
}

virtual public void HelloWorld()
{
Console.WriteLi ne("I am A");
}
}

public class B : A
{
public B()
{
}

override public void HelloWorld()
{
Console.WriteLi ne("I am B");
}

public void Main()
{
B MyB = new B();
}
}
Nov 15 '05 #1
2 2795
Not sure what you are looking for, but you can always call your base class
methods from an inherited class (assuming they are marked public or
protected). Adding base.HelloWorld () will make sure the "I am A" actually
gets registered. The inheritance chain has quite a few rules based around
constructors and method inheritance so you'll want to go through the process
of writing a few different examples and run through the C# language
documentation to make sure you understand all of the nuances.

using System;

public class A : System.Object {
public A() {
HelloWorld();
}

virtual public void HelloWorld() {
Console.WriteLi ne("I am A");
}
}

public class B : A {
public B() {
}

public override void HelloWorld() {
base.HelloWorld ();
Console.WriteLi ne("I am B");
}

private static void Main() {
B MyB = new B();
}
}
--
Justin Rogers
DigiTec Web Consultants, LLC.

"Jim Owen" <jo***@saltmine .com> wrote in message
news:04******** *************** *****@phx.gbl.. .
In the following code, as I understnad it, the resulting
display would be "I am B", because I have overridden the
constructor in B. However, what if what I really want to
do is display "I am A" followed by "I am B', so that A's
constructor is called, followed by B's constructor? Also,
does it work differently for constructors than it does for
other overridden methods?

public class A : System.Object
{
public A()
{
HelloWorld();
}

virtual public void HelloWorld()
{
Console.WriteLi ne("I am A");
}
}

public class B : A
{
public B()
{
}

override public void HelloWorld()
{
Console.WriteLi ne("I am B");
}

public void Main()
{
B MyB = new B();
}
}

Nov 15 '05 #2
Jim Owen <jo***@saltmine .com> wrote:
In the following code, as I understnad it, the resulting
display would be "I am B", because I have overridden the
constructor in B.
No you haven't. Constructors aren't inherited in C#, so can't be
overridden. See
http://www.pobox.com/~skeet/csharp/constructors.html
However, what if what I really want to
do is display "I am A" followed by "I am B', so that A's
constructor is called, followed by B's constructor?


In fact A's constructor *is* called, followed by B's constructor,
because B's constructor is implicitly

public B() : base()
{
}

However, you've only got one call to HelloWorld, and that works
polymorphically from the very start. It's basically a very bad idea to
call virtual methods within a constructor in almost all circumstances.
In the very, very rare situation where it's a good idea, it needs to be
very thoroughly documented, so that people overriding the method know
full well that their method will be called before their constructor has
finished executing, and that therefore they may not have complete state
yet.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

8
1509
by: Franz Steinhaeusler | last post by:
Hello, (in wxPython) I want to redirect the SetStatusText to an own method (change the text somewhat) and then call the SetStatusText of wx.Frame. But I have a little problem of understanding, but I think, it should be achieved somehow.
9
1705
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
2
8024
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
8
1572
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the class of "obj"?
10
3552
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base class *Shared* methods which make use of the class ID. So I gave the base class a classID field, and then I gave the derived classes Shared constructors, which I used to set the classID field to the appropriate values for each derived class. But...
4
11987
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
22
23346
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
6
2091
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
9
1572
by: VK | last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to beta stage.<OT> Besides that I am planning to write an introductory to inheritance schema currently used in Javascript programming. It will not be a manual of a "proper" way to use OOP inheritance but a compilation so a description of _all_ OOP paradigms that had been used or are being used in Javascript commercial solutions: starting from the prototype inheritance of...
0
8189
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,...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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
7184
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
6118
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
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
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.