15,884,388 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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
Show More