473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Inheritance question

Hi, I just started the chapter on "Inheritanc e" and I'm a bit confused
so I have three questions: first, here's the code:

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleProject1
{
public class Base
{
public virtual void SomeMethod( )
{
Console.WriteLi ne("Inside: Base.SomeMethod .");
}
}

public class Derived : Base
{
public override void SomeMethod( )
{
base.SomeMethod ( );
}
}

public class MainClass
{
public static void Main( )
{
Derived d = new Derived( );
d.SomeMethod( );
}
}
}

1.)
In the above example, shouldn't Derived.SomeMet hod() call itself
(infinite loop) because
I overrided the base method implimentation? meaning, Base.SomeMethod
does not "exist" anymore but only my implimentation of SomeMethod() in
the Derived class?

2.)
If I instead marked the method in the derived class with the "new"
keyword, I would have
access to two implimentations of the same method name:
Base.SomeMethod () and Derived.SomeMet hod(),.. no?

3.)
When you inherit from a base class, the effect is exactly the same as
if you had explicitly declared the inherited members in your derived
class, true or false?
Thanks in advance,
relient.

Jan 18 '06 #1
1 1780
1. No, the function for Base.SomeMethod still exists. When you invoke
base.SomeMethod ( ) you are indeed calling the base class implementation.

2. The new method is a documentary indicator that you are replacing a base
class implementation of a method, variable, class etc with a new one. It
therefore "hides" the item in the base class. Without the modifier you will
get compiler errors. There are two different implementations of
SomeMethod( ), and calling <derived>.SomeM ethod( ) will call the derived
method's implementation. The principal "gotcha" with the new modifier is if
you are using polymorphism you should be using virtual functions, not the
"new" modifier.

3. The answer depends on what perspective you are looking at the object. If
you are using inheritance purely for "implementa tion inheritance" then the
affect of inheritance is virtually the same as if you had manually added all
of the members of the base class to your derived class, with a couple of
small exceptions, like elements redeclared with "new".

But if you are using inheritance with the intent of using polymorphism, then
they are certainly not the same, because polymorphism needs the concept of
virtual functions associated with a base class that have differing
implementations based on the derived class.

Hope this helps.

"relient" <xl************ ***@gmail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Hi, I just started the chapter on "Inheritanc e" and I'm a bit confused
so I have three questions: first, here's the code:

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleProject1
{
public class Base
{
public virtual void SomeMethod( )
{
Console.WriteLi ne("Inside: Base.SomeMethod .");
}
}

public class Derived : Base
{
public override void SomeMethod( )
{
base.SomeMethod ( );
}
}

public class MainClass
{
public static void Main( )
{
Derived d = new Derived( );
d.SomeMethod( );
}
}
}

1.)
In the above example, shouldn't Derived.SomeMet hod() call itself
(infinite loop) because
I overrided the base method implimentation? meaning, Base.SomeMethod
does not "exist" anymore but only my implimentation of SomeMethod() in
the Derived class?

2.)
If I instead marked the method in the derived class with the "new"
keyword, I would have
access to two implimentations of the same method name:
Base.SomeMethod () and Derived.SomeMet hod(),.. no?

3.)
When you inherit from a base class, the effect is exactly the same as
if you had explicitly declared the inherited members in your derived
class, true or false?
Thanks in advance,
relient.

Jan 18 '06 #2

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

Similar topics

1
3749
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2201
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
6
2099
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...
12
2741
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
6
1262
by: relient | last post by:
Hi, I have three classes: Animal, Dog and Cat. Dog inherits from Animal and Cat inherits from Dog. My question is: is Cat now also a Animal even though it didn't inherit directly from Animal?
3
1841
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
1
2458
by: Oskar Bennet | last post by:
Hi everybody, I am supposed to draw a simple UML diagram for a very small project that consists of less than a dozen classes. I have never been working with UML before, I have read some tutorials but I cannot find the connection between UML and my C++ code. What I have found out so far is that inheritance is indicated by an arrow like this |\
8
1281
by: Devon Null | last post by:
I was wondering if there is a container (i.e. a vector) that can hold data structures of differing sizes. I was thinking of something along the lines of a container of classes. Think of a backpack you can take items from and put items into of different sizes. The reason I want to know is I am about to try my hands at learning classes and need to do it in a way I can grasp - ala gaming concepts (see inventory example above.) Thanks in...
3
1267
by: bg_ie | last post by:
Hi, I currently have a class called DefinedTest which relates to a set of tests I perform. But there are two Test states, those that are defined and those that are not. I'd therefore like to have two classes instead, Test and DefinedTest with the former storing either Defined or non-Defined Tests and the latter only defined tests. The distinction between the two classes in terms of members is very simple. A defined test has a file on...
0
9271
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
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
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...
1
9838
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
8709
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...
0
6534
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
3
2665
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.