473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 4310
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****@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.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.ShowWithNoVirtual(); // 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 ShowWithNoVirtual()
{
Console.WriteLine("Base - no virtual");
}

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

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

public new void ShowWithNoVirtual()
{
Console.WriteLine("Derv - no virtual");
}

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

"Akhil" wrote:

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

"googou" <go****@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.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
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...
1
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...
5
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...
6
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.