OOPs question: whats the difference between these ways of accessing methods !!!
-
class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
2. I can take an instance of class P in some class and I can expose the object of class P using some property ....class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();
My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???Thanks, Arindam D Tewary
-
class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
2. I can take an instance of class P in some class and I can expose the object of class P using some property ....class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();
My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???Thanks, Arindam D Tewary
Hi I think there's no much difference. It is a type of organizing the code like parent child relation ship. Both method can access the public members. But using inheritance method will give a clear hierarchy of the program. public class parent { //methods } public class child:parent { //methods } I think this will give a clear picture that child having parent properties.
Cheers Navaneeth!! www.w3hearts.com
-
class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
2. I can take an instance of class P in some class and I can expose the object of class P using some property ....class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();
My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???Thanks, Arindam D Tewary
Well, option 1 means that Q is derived from P. This means that it has access to protected methods in P. Consider the following:
class P
{
protected bool IsValid { get { return ... ; } }
}class Q : P
{
public void Validate()
{
if (IsValid)
...
}
}Now, you can't do the same with the following:
class R
{
private P foo = new P();
public void Validate()
{
// This bit won't compile.
if (foo.IsValid) ...
}
}What you are seeing is explained using IsA and HasA relationships. In the case Q, we say that Q is a P - there is an implicit inheritance chain and Q has access to certain *exposed* portions of P. In the case of R, we say that R has a P - no inheritance chain, and no access to the internals of P.
Deja View - the feeling that you've seen this post before.
-
class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
2. I can take an instance of class P in some class and I can expose the object of class P using some property ....class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();
My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???Thanks, Arindam D Tewary
What you use depends on what is logical in the relation between the classes. Inheritance is used when the objects are basically the same. Like an airline pilot is also a person, so the class AirlinePilot would inherit from the class Person. Class members are used when one object is a part of or a member of another. An airline pilot is not the same thing as an aeroplane, but rather a part of it, so the AirlinePilot object would be a member of the AeroPlane class.
--- single minded; short sighted; long gone;
-
Well, option 1 means that Q is derived from P. This means that it has access to protected methods in P. Consider the following:
class P
{
protected bool IsValid { get { return ... ; } }
}class Q : P
{
public void Validate()
{
if (IsValid)
...
}
}Now, you can't do the same with the following:
class R
{
private P foo = new P();
public void Validate()
{
// This bit won't compile.
if (foo.IsValid) ...
}
}What you are seeing is explained using IsA and HasA relationships. In the case Q, we say that Q is a P - there is an implicit inheritance chain and Q has access to certain *exposed* portions of P. In the case of R, we say that R has a P - no inheritance chain, and no access to the internals of P.
Deja View - the feeling that you've seen this post before.
Dear Pete O'Hanlon Excellent !! I am sure this is what I was looking for .... and the way you have presented is awesome. Thanks a lot !!!! :-D:-D
Thanks, Arindam D Tewary
-
What you use depends on what is logical in the relation between the classes. Inheritance is used when the objects are basically the same. Like an airline pilot is also a person, so the class AirlinePilot would inherit from the class Person. Class members are used when one object is a part of or a member of another. An airline pilot is not the same thing as an aeroplane, but rather a part of it, so the AirlinePilot object would be a member of the AeroPlane class.
--- single minded; short sighted; long gone;
Dear Guffa, Thanks for a nice reply. Indeed it was very much helpful.
Thanks, Arindam D Tewary
-
Hi I think there's no much difference. It is a type of organizing the code like parent child relation ship. Both method can access the public members. But using inheritance method will give a clear hierarchy of the program. public class parent { //methods } public class child:parent { //methods } I think this will give a clear picture that child having parent properties.
Cheers Navaneeth!! www.w3hearts.com
Dear Navaneethkn, Thanks for a nice reply. Indeed it was helpful.:-D
Thanks, Arindam D Tewary