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

Simple Inheritance question

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

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleProject1
{
public class Base
{
public virtual void SomeMethod( )
{
Console.WriteLine("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.SomeMethod() 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.SomeMethod(),.. 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 1757
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>.SomeMethod( ) 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 "implementation 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.googlegr oups.com...
Hi, I just started the chapter on "Inheritance" and I'm a bit confused
so I have three questions: first, here's the code:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleProject1
{
public class Base
{
public virtual void SomeMethod( )
{
Console.WriteLine("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.SomeMethod() 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.SomeMethod(),.. 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
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...
2
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...
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...
12
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...
6
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
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...
1
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...
8
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...
3
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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,...

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.