473,698 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding initializing base class object to derived class object,

Hi all,

I am confuse in concept of inheritance. I am having following 2
classes
class base1
{
public void abc()
{
System.Console. WriteLine("base 1 abc");
}
public void pqr()
{
System.Console. WriteLine("base 1 pqr");
}

public void xyz()
{
System.Console. WriteLine("base 1 xyz");
}
}

class derived1 : base1
{
public void abc()
{
System.Console. WriteLine("deri ved1 abc");
}

public void pqr()
{
System.Console. WriteLine("deri ved1 pqr");
}

public void xyz()
{
System.Console. WriteLine("deri ved1 xyz");
}

}

so when i am having code like following
base1 c = new derived1();
c.abc();c.pqr() ;c.xyz();

Here what exactly happen. Will object of derived class gets created and
its reference gets stored into c? If yes, then if my derived class has
more mothod will those also get initialize and can i access those
methods of derived class through base class object?

Please correct me if i am wrong.

thanks in advance

Jan 9 '07 #1
1 2192
Will object of derived class gets created and
its reference gets stored into c?
Yes; however, c is typed as base1, so only the base1 methods will be
directly callable unless you cast into derived1 [this does not change
the actual object in any way].
If yes, then if my derived class has
more mothod will those also get initialize and can i access those
methods of derived class through base class object?
Methods aren't really initialised; objects are. If you mean
*additional* methods (generally new names), then they will not be
visible without casting. For methods of the same name (and signature)
there are two scenarios:
1 (polymoprphism) : the base method can be marked as "virtual" allowing
the derived method to declare itself as "override"; there is then only
1 method, and "which is called" is determined by the object type -
i.e. it doesn't matter if you have a variable typed as base1 or
derived1: the "most recent" (if you see what I mean) override will be
called. This is the most common variant.

2: the derived method can be marked as "new"; this creates a
*duplicate* method of the same name on the object; "which is called"
is determined by the variable type; if the variable is typed as base1,
then the original version (or most recent override) is invoked. If the
variable is typed as derived1, then the newly declared version is
invoked. This is primarily used to make a method more specific, e.g.
to make a return type more context-appropriate (i.e. "SomeType"
instead of "object") while allowing callers who only know about base1
to work "as was".

Marc
Jan 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1748
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
3
1679
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
1307
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving from base class, and casting, derived classes would already have their variables initialiezed(cause they have already been initialized in the base class) ....
18
1659
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am pasting it here as well... enjoy! (you can skip to the source at the end of the message if you like...) Consider:
3
3490
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ... more protected fields }
3
2064
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
5
4905
by: Joseph Turian | last post by:
I have a global method "update_var(double v);", which updates the value of global variable 'var'. I have a class Foo, which would like to know when 'var' is updated. Let's say I make an ABC: class Notify { virtual void notify_that_var_has_updated() = 0; }; and then I use MI to make Foo inherit from Notify.
9
1490
by: Mike Hofer | last post by:
In a large application I'm working on (ASP.NET 1.1, VS2003), we have a base class that wraps stored procedures. Essentially, this base class (StoredProcedureBase) encapsulates the code to set up the connection, transaction, command and parameters required to invoke a stored procedure on our SQL Server database. It provides helper methods that simplify the process of invoking the stored procedure so that our data access classes can make the...
2
4848
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different signature. A class inherits publicly from this base class and redefines one of the two functions in the derived class. In that case, a derived class object cannot access the other base class function that it hasn't redefined. I'm posting a code snippet...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.