473,396 Members | 1,754 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,396 software developers and data experts.

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.WriteLine("I am A");
}
}

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

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

public void Main()
{
B MyB = new B();
}
}
Nov 15 '05 #1
2 2784
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.WriteLine("I am A");
}
}

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

public override void HelloWorld() {
base.HelloWorld();
Console.WriteLine("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.WriteLine("I am A");
}
}

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

override public void HelloWorld()
{
Console.WriteLine("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.com>
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
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...
9
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
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
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...
10
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...
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()
22
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...
6
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...
9
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...
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: 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
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
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
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
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
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.