Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

base and derived class

Name: Anonymous 2011-08-09 16:36

What is the difference between the objects 'dc' and 'bcdc', in the following bit of code (C#, may apply to other languages as well):
BaseClass bc = new BaseClass();
DerivedClass dc = new DerivedClass();
BaseClass bcdc = new DerivedClass();

Why would you use one over the other? I found it on a page that explained the difference between new and override&virtual
http://msdn.microsoft.com/en-us/library/ms173153%28v=VS.100%29.aspx

Name: Anonymous 2011-08-09 16:59

From the first paragraph of that page: "The override modifier extends the base class method, and the new modifier hides it."

Read that sentence 10 times then read the whole page 5 times.

Name: Anonymous 2011-08-09 17:06

Yeah I know the difference between new and override+virtual, but what is the difference between using the following:
"dc is of type DerivedClass, and its value is of type DerivedClass.

bcdc is of type BaseClass, and its value is of type DerivedClass."

Why use one over the ohter?

Name: THE_JEWS_ARE_AFTER_ME !!kCq+A64Losi56ze 2011-08-09 17:07

Now it's time for some jewish set theory...

It looks like some fagged up way to emulate the union of two sets.

Name: THE_JEWS_ARE_AFTER_ME !!kCq+A64Losi56ze 2011-08-09 17:13

Pardon me, I'm a bit drunk.....

It looks like some fagged up way to emulate the union of two sets.

That should be the *intersection* of two sets.  Just let one class be a set called A and the other class be called set B. Now using the C## fagged up notion of multiple inheritance, new class C ends up becoming the intersection of sets A and B, where the objects themselves are the elment(s) in the set.

Name: Anonymous 2011-08-09 17:34

I know the difference between new and override+virtual

then why are you racking your brain trying to figure out an MSDN article whose purpose is to teach you the difference?

I'd put together a simple example using something like cars but microsoft already did that. just find a different resource if that one isnt working for you.

Name: Anonymous 2011-08-09 18:26

I read that article to learn the difference between new and override+virtual, and I understand the article and the examples they give. In the first example they use these two objects:
"dc is of type DerivedClass, and its value is of type DerivedClass.

bcdc is of type BaseClass, and its value is of type DerivedClass."
Depending on if new or override is used the bcdc object uses either the method from the base or the derived class. The dc object will us the method derived class in both cases.

But aside from new and override, what is the difference between dc and bcdc? these objects are of different types of classes, but their values are the same type of class. Does this have any significant effect on the objects or the way they are used?

Name: Anonymous 2011-08-10 7:00

bamp

Name: Anonymous 2011-08-10 7:12

C#
Ha. Hahahaha.

Name: Anonymous 2011-08-10 13:00

I don't program in C#, but it looks like the answer is the obvious one: dc is of type DerivedClass, and bcdc is of type BaseClass. Therefore all the caveats with new/override happen in methods where a BaseClass is expected, and a DerivedClass may be passed.

If you pass a DerivedClass with override methods, and the overridden method either does or doesn't do some thing that the caller expects, then you will end up with buggy code. To minimize risk, Microsoft recommends that you use the base keyword to call the base method in the overridden one -- although finally, you're responsible for keeping expectations.

new has less impact than override, and therefore appears to be "safer". However, methods that don't use override cannot expect that process to execute everywhere that method is called, which can have unexpected consequences which would be difficult to track. A LoggedWorker with only new methods will gladly get away with not keeping minutes for a meeting() which only expects that Workers of any kind show up. If the new code in the overridden method is critical to the state or operation of the DerivedClass, then you must use override.

My esteemed conclusion is that you shouldn't base your core design around overrides of either kind. Rather, use them as just the right tool in exceptional cases, when you find that you need a creative solution.

Name: Anonymous 2011-08-10 13:04

>>10
I don't program in C#, but it looks like the answer is the obvious one: dc is of type DerivedClass, and bcdc is of type BaseClass.
Your wrong, morraine.

Name: Anonymous 2011-08-10 13:44

>>11
No I'm not. I can verify that I don't program in C#, and the types are correctly listed1.

___________________
1. See: variable declarations, >>1.

Name: Anonymous 2011-08-10 14:10

>>10
>you shouldn't base your core design around overrides

IHBT

Name: Anonymous 2011-08-10 14:14

         ,           `\
      .' /            ;        ;
     .'         .      .       .'
       .            /     \.'
        :       .-"        \          .-"
         \    .'            ;     .-"`
           './       ,      ._.-"`
             \      ;       ;
              \    `,
:           \ '    ';      '           .-"                '.
'            ' .   ;`      :         .'.
;            : | .-------./         ;'`                        '.
 \           ` |/ _        \      //  _____
      ,. .,__. .'" \        ;-._.---"` __  `'"--.     ,;'         `
                    '       |  \__.--"`  ___     `"-.
                     :      |  /___.---"`            `.
                      .     ;"-'                       ".
                      |  ./'   \                         \
-._             _.-'  |  ;  |  |.". _.--"`"""'._      .   \
   '-.     .-'     __; /_.-' ;`.:  |            `"--:;-._  ;
;-._    :.     _.-:_; /' /   ;/ ': |               '"`.  `":.
-.,_:- -''- --:_,.----'"     ':.   /     .::::::.      '.
"--._     .    _.--"""'--   ;:'  .'   .:::::::::::::.    '.
          |\          _    |__."`"----.__ ':::::::::::::.. '.
      .   |H; .---.    ':  ;'. `"---.         `'::::'`       \
  .-.  `-M"` /     \    ::';  \     ._     __.-----._         .
 .\. `"'----;    :'`    :'/ .  \.-"/  `"--"                    `
 ;'.`'------|      :   ; /  ::. './                       '.
   '\HH".:::|      '   .'  \'::' /           _.-"-
\    / '::::|.'`'. |\.':   _\_.-/        ,-"`
 `;-`  .-'--':--'"_.';.--"`    |        /
  .           `"`  ': \       _|      .'   _.--"'`'"--
"':      .----.   ___.;  __."`/     .' .-"`
:::'  .'               ':__ .'     / .'
::        .'.'      .'     `--._  /.'
'       / /         \  .- .     `\                       ;
'.    .' ;            / .'       ;         _.--""--._  .'
  ;   .::|:.        .| ' .'    _.  '-._.-"`          `".
 .' .:::-"::::. ___:-"`'  \  .'     "`                  \
   '"      _.'         "--" -.                             '.  ;
     __.-'   .-"'`            ".                         '    '
            /_.,.,_.--"'`'"-.  '.
          /     `/            "-.                       `   _."
         :      ;           _.   '-._                  ;     ;
         |     _\          /     _   `._             .'
        _' _.`___'._      _\   _;_.'\   `\       .-"       ;
        "    `    ` `"'-"`  `"/ .-.      /               .'
      _ /   _.--._     _."'"-.\ \ ;  __.'              .'
     ;_  \ .---.  `"'"`   .:.  `"--"      ___    .-"'`        
     |_; ' ;--. ).".---.  ,., -"`";"`""'`
    ;  .:/ '-"  ;  '--. ;/  \.---.|
    '.__.-'\  .: / |'-"  ;  (.-"-.|
            `._.`  '.   /   |'._.';
                     `"'     '._."

Name: Anonymous 2011-08-10 15:24

>>1

The static type of 'dc' is DerivedClass.
The static type of 'bcdc' is BaseClass.

This is the only difference.

I'm not familiar with C#, but _if_ the language differentiates virtual methods from non-virtual ones, then invoking a non-virtual method on 'dc' will yield the invocation of the method inside DerivedClass, while invoking the same method on 'bcdc' will yield the invocation of the method inside BaseClass.

If invoking a virtual method, no difference. If doing dynamic type testing, no difference.

You use the latter declaration (BaseClass) when an instance of BaseClass is enough for your needs. Simple as this.

Name: Anonymous 2011-08-10 16:31

joh joh joh /prog/ is so fresh against /prog/ reddit looks like a rat

Don't change these.
Name: Email:
Entire Thread Thread List