Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dumbass inheritance question: i just can't get it 2

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
two classes:

Code:
public class ClassA
{
 private string boing = "boing";
 public string boing { get { return boing; };
}

public class ClassB : ClassA
{
 private string boing = "BOING";
}

class TestClass
{
 void Main()
 {
  Debug.Print(new ClassA().Boing);
  Debug.Print(new ClassB().Boing);
 }
}

// output:
// boing
// boing

why doesn't ClassB.Boing see its own copy of the boing private member variable?

mr s. <;)

 
this will cause an error
Code:
public class ClassA
{
   private string boing = "boing";
   public string [COLOR=red]b[/color]oing { get { return boing; };
}
this won't
Code:
public class ClassA
{
   private string boing = "boing";
   public string [COLOR=blue]B[/color]oing { get { return this.boing; };
}
you need to mark the string protected to change the value.
Code:
public class ClassA
{
   protected string boing = "boing";
   public string Boing { get { return this.boing; };
}
public class ClassB
{
   public ClassB() : base() { this.boing = "BOING"; }
}

class TestClass
{
   void Main()
   {
      Debug.Print(new ClassA().Boing);
      Debug.Print(new ClassB().Boing);
   }
}

// output:
// boing
// BOING

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jason,
thanks for that.

my point, though, was that the inherited method from the base class sees the private member on the base class (which should be private, i.e. not visible from the derived class) and not its own redefinition of the variable.

i'm still confused.



mr s. <;)

 
i see your point. i haven't run into this scenario before. i know with protected and private members you can override them with the new keyword.

try this and see if it works
Code:
public class ClassA
{
   private string boing = "boing";
   public string Boing { get { return this.boing; };
}
public class ClassB
{
   private new string boing = "BOING";
   public ClassB() : base() { }

class TestClass
{
   void Main()
   {
      Debug.Print(new ClassA().Boing);
      Debug.Print(new ClassB().Boing);
   }
}

// output:
// boing
// BOING - or - boing

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I think it's normal behaviour. Both classes have private members and Class A has the public property which for which it uses it's private member. ClassB just happens to have the saame private member which is not used because the propertyin classA is not being overwritten. If you want to use the private member of ClassB then you will have to override the public property in classB.

Or you have to make the private member protected in ClassA and use that in classB

Code:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Console.WriteLine(new ClassA().Boing);
            System.Console.WriteLine(new ClassB().Boing2);
            System.Console.WriteLine(new ClassB().Boing);
        }


    }

    public class ClassA
    {
        private string boing = "boing";
        public string Boing { get { return boing; } }
    }

    public class ClassB : ClassA
    {
        private string boing = "BOING";
        public string Boing2 { get { return boing; } }
        public string Boing { get { return boing; } }
    }

this will give the desired result.



Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
misterstick said:
my point, though, was that the inherited method from the base class sees the private member on the base class (which should be private, i.e. not visible from the derived class) and not its own redefinition of the variable.

Remember what you have writte could also be written like this:

Code:
Console.WriteLine( new ClassA().Boing );
Console.WriteLine( ((ClassA)new ClassB()).Boing );

When you declare an object of ClassB what you get is a object of ClassA in memory that is wrapped by the extra bits in ClassB. ClassB does not get a copy of ClassA's method. It mearly passes the call onto the ClassA object that is within it. The property accessor you are calling is on ClassA, so it is not supprising it sees ClassA's variable and not ClassB's?
 
bravo! now i get it.

(i'd have given a star to chrissie1, too, but their code didn't compile)

mr s. <;)

 
just because someone's code doesn't compile doesn't mean it didn't help you understand.

Most of the stuff Chrissie and I write on here is off the top of our head, NOT copied from visual studio.


 
Chrissie's code compiled fine for me (pasted into a new windows application). Just takes a little work to make it show the result ;-)



[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
i understand that, and i'm sorry if i've given offence.

the reason it didn't compile, though, was pretty fundamental, since missing the virtual/override keywords pretty much obscured what chrissie1 was trying to say.

as i said, i would have given...

mr s. <;)

 
Actually it does compile and run. The virtual/override/new keywords don't prevent it from compiling and running and giving the result.

Perhaps it wasn't perfect but you example doesn't really compile either.

but this does.

tried and tested in VS2005.

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new ClassA().Boing);
            Console.WriteLine(new ClassB().Boing2);
            Console.WriteLine(new ClassB().Boing);
            Console.WriteLine(new ClassC().Boing);
            System.Console.ReadLine();
        }

    }

    public class ClassA
    {
        private string boing = "boing";
        public string Boing { get { return boing; } }
    }

    public class ClassB : ClassA
    {
        private string boing = "BOING";
        public string Boing2 { get { return boing; } }
        public string Boing { get { return boing; } }
    }

    public class ClassC : ClassA
    {
        private string boing = "BOING";
    }


}

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top