471,327 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 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 1626
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by KK | last post: by
6 posts views Thread by relient | last post: by
3 posts views Thread by RSH | last post: by
1 post views Thread by Oskar Bennet | last post: by
8 posts views Thread by Devon Null | last post: by
3 posts views Thread by bg_ie | last post: by

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.