Click here to Skip to main content
14,271,339 members

Comments by FTWO (Top 9 by date)

FTWO 19-Jun-13 4:37am View
   
Ah right…
When instantiated like this (using base class reference in derived class)

ClassBase ObjectBase = new ClassDerived();

The only methods that the object can access in ClassDerived are methods that also exist in ClassBase (unless I’m casting it). Is that correct ?

Apologies for being slow on this !
FTWO 18-Jun-13 17:45pm View
   
It may lead to far. That's funny.

I wanted to see if I was being clear in the question I was asking
ie why would you instantiate like this (using base class reference in derived class:
BaseClass objBaseClass= new DervivedClass()
and not like this
ClassDerived objDerivedClass = new Derived()
If objBaseClass.Show() and objDerivedClass.Show() would always run derived method.
In any case Sagar cleared things up somewhat for me.

Incidentally can you recommend good OOP literature, I have been reading about dynamic dispatch and virtual method tables but mainly from forums or Wikipedia, clearly I need a good recommendation.
FTWO 18-Jun-13 17:32pm View
   
Ah Yes - thank you Sagar, I think that clears it up


1. Without overwrite in DerivedClass show() method

if instantiated like this (using base class reference in derived class
BaseClass objBaseClass= new DervivedClass()

The base class method will be run for command objBaseClass.Show()

if instantiated like this
DerivedClass objBaseClass= new DervivedClass()

The derived class method will be run for command objBaseClass.Show()

2. With overwrite in DerivedClass show() method
The derived class method will run regardless
FTWO 18-Jun-13 14:29pm View
   
just so there's no confusion can you tell me what you think I am asking
FTWO 18-Jun-13 12:17pm View
   
It's a beginners question I suppose. "The use" is to help me understand it.
It's not code that is being used anywhere, it's an example I'm giving so I can understand the principles.
I saw the code here http://www.codeproject.com/Articles/602141/Polymorphism-in-NET
and was surprised that the class was being instantiated as
ClassBase ObjectBase = new Derived() ---I'm calling this case 1
and not
ClassDerived ObjectDerived = new Derived() -- I'm calling this case 2

So I'm asking the question why, so that I can learn from the answer - maybe that's the use.


FTWO 18-Jun-13 10:42am View
   
Thanks Sergey - Are you saying that first case may work, but should use second case ?
FTWO 18-Jun-13 9:30am View
   
Deleted
yes - sorry , that's what I meant.
FTWO 18-Jun-13 9:30am View
   
I have the output in comments that show that I was able to access the derived method
in first example

ClassBase ObjectBase = new Derived();
ObjectBase.Show(); // Output --- This is Derived Class.
FTWO 18-Jun-13 9:29am View
   
Yep - sorry, just copped that