473,786 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base Obj = new Derived()?

Hi All,

Can u please explain this.

Base Obj = new Derived(); [Base being the base class and Derived being
the derived the class]

Can Obj access methods both of Base and Derived or what will be the
behaviour?
What will be the behaviour for Overridden Methods?

Cheers,
Akhil

Nov 16 '05 #1
4 4346
Obj only can access methods of base(the Interface of Base ),but the behaviour
of method depend on the Derived(for the Overridden Methods or New
Methods,of couse the behaviour of the other method of Derived is same to
the Base)

hope it helps

"Akhil" wrote:
Hi All,

Can u please explain this.

Base Obj = new Derived(); [Base being the base class and Derived being
the derived the class]

Can Obj access methods both of Base and Derived or what will be the
behaviour?
What will be the behaviour for Overridden Methods?

Cheers,
Akhil

Nov 16 '05 #2

Thanks, But Not Clear though.
--
Cheers,
Akhil

"googou" <go****@discuss ions.microsoft. com> wrote in message
news:D4******** *************** ***********@mic rosoft.com...
Obj only can access methods of base(the Interface of Base ),but the behaviour of method depend on the Derived(for the Overridden Methods or New
Methods,of couse the behaviour of the other method of Derived is same to the Base)

hope it helps

"Akhil" wrote:
Hi All,

Can u please explain this.

Base Obj = new Derived(); [Base being the base class and Derived being the derived the class]

Can Obj access methods both of Base and Derived or what will be the
behaviour?
What will be the behaviour for Overridden Methods?

Cheers,
Akhil


Nov 16 '05 #3
Since the reference type of obj is Base, you will be able to access only
those members of Base, and not that of Derived. Now, whose members get
invoked depends upon how the derived class has overriden the base members
etc. Unless the Derv have overridden the methods, Base's methods keep getting
called.

"Akhil" wrote:
Hi All,

Can u please explain this.

Base Obj = new Derived(); [Base being the base class and Derived being
the derived the class]

Can Obj access methods both of Base and Derived or what will be the
behaviour?
What will be the behaviour for Overridden Methods?

Cheers,
Akhil

Nov 16 '05 #4
This example might help you understand:
class Class1
{
static void Main(string[] args)
{
Base obj = new Base();
obj.Show();

obj = new Derv();
obj.Show(); // This will invoke Derv's method, as it overrides Base's
method
obj.ShowWithNoV irtual(); // This will invoke Base's method, as the Base's
method is not virtual
//obj.DervMethod( ); This won't work as obj refers to a Base type instance
}

}

public class Base
{
public void ShowWithNoVirtu al()
{
Console.WriteLi ne("Base - no virtual");
}

public virtual void Show()
{
Console.WriteLi ne("Base");
}
}

public class Derv : Base
{
public void DervMethod()
{
}

public new void ShowWithNoVirtu al()
{
Console.WriteLi ne("Derv - no virtual");
}

public override void Show()
{
Console.WriteLi ne("Derv");
}
}

"Akhil" wrote:

Thanks, But Not Clear though.
--
Cheers,
Akhil

"googou" <go****@discuss ions.microsoft. com> wrote in message
news:D4******** *************** ***********@mic rosoft.com...
> Obj only can access methods of base(the Interface of Base ),but the

behaviour
> of method depend on the Derived(for the Overridden Methods or New
> Methods,of couse the behaviour of the other method of Derived is same

to
> the Base)
>
> hope it helps
>
> "Akhil" wrote:
>
> > Hi All,
> >
> > Can u please explain this.
> >
> > Base Obj = new Derived(); [Base being the base class and Derived being > > the derived the class]
> >
> > Can Obj access methods both of Base and Derived or what will be the
> > behaviour?
> > What will be the behaviour for Overridden Methods?
> >
> > Cheers,
> > Akhil
> >
> >
> >
> >


Nov 16 '05 #5

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

Similar topics

2
1987
by: Christian Engström | last post by:
When I compile the below program with Microsoft Visual C++ version 6, I get the results I expect, which is that the program should write out base() derived() base() derived(derived) When I try to compile it with gcc, I instead get the compiler error message
1
4345
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
5
3164
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
2625
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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,...
1
10110
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
9964
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
8993
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
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4067
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.