Choosing object type

  • Thread starter Thread starter Phi!
  • Start date Start date
P

Phi!

Hello everyone,

any help with this would be appreciated as I have spent the past
afternoon trying to sort it out. I have two questions based on the
followiung information.

I have two classes: -

Class A {
Method 1() {}
}

Class B: A {
Method 1(){}

Method 2() {}

Method 3() {}
}

From another part of my program I try and call the class as follows: -

A a = new B();

I can now access method 1 from a , however I cannot access method 2 or
3 from A and I can't see why I cannot access them.

My underdtanding of inhertiance, polymorphism and OO design was that I
could do this. Question 1: what am I doing wrong here to make this
work.


Question 2: My actual program consists of 3 dervied classes and
according to an variable I must instantiate one of the three classes
but I wanted teh object to have the same name because subsequent code
in the program. I was going to do it the above way but if there is
something wrong with my thinking what would be the best way of doing
this.

Again any help would be greatfully appreciated.

Kind Regards

Phil
 
Hi,

Phi! said:
Hello everyone,

any help with this would be appreciated as I have spent the past
afternoon trying to sort it out. I have two questions based on the
followiung information.

I have two classes: -

Class A {
Method 1() {}
}

Class B: A {
Method 1(){}

Method 2() {}

Method 3() {}
}

From another part of my program I try and call the class as follows: -

A a = new B();

I can now access method 1 from a , however I cannot access method 2 or
3 from A and I can't see why I cannot access them.

My underdtanding of inhertiance, polymorphism and OO design was that I
could do this. Question 1: what am I doing wrong here to make this
work.

You should cast your a class to B.
((B)a).Method2()
or
B b = (B)a;
b.Method2()
 
hi Phil,
A a = new B();

I can now access method 1 from a , however I cannot access method 2 or
3 from A and I can't see why I cannot access them.

My underdtanding of inhertiance, polymorphism and OO design was that I
could do this. Question 1: what am I doing wrong here to make this
work.

well, no it doesn't quite work like that - "a" refers to the object you've
created as an object of class A. You can only access members of A, even
though the object is actually a B. If you want to call the members specific
to B you must reference it as a B,
ie you'd have to do this

B b = new B();
b.Method_2();

see below for the solution...
Question 2: My actual program consists of 3 dervied classes and
according to an variable I must instantiate one of the three classes
but I wanted teh object to have the same name because subsequent code
in the program. I was going to do it the above way but if there is
something wrong with my thinking what would be the best way of doing
this.

have a look into virtual functions. That way you can refer to the object by
the base class, but use the methods implemented in your subclass. Like
this:

class A
{
internal virtual void Method();
}

class B : A
{
internal override Method(){...}
}

A a = new B();
a.Method(); // calls overidden method implented in class B

hope this helps :)

Rob
 
Hi Phi,

See my comments inlined
any help with this would be appreciated as I have spent the past
afternoon trying to sort it out. I have two questions based on the
followiung information.

I have two classes: -

Class A {
Method 1() {}
}

Class B: A {
Method 1(){}

Method 2() {}

Method 3() {}
}

From another part of my program I try and call the class as follows: -

A a = new B();

I can now access method 1 from a , however I cannot access method 2 or
3 from A and I can't see why I cannot access them.
My underdtanding of inhertiance, polymorphism and OO design was that I
could do this. Question 1: what am I doing wrong here to make this
work.

You cannot call Method2 and Method3 using *a*

Varibale *a* is of type A (even though the actual type of the object it
references is of type B)
and because *a* is of type A it has no Method1 and Method2. If you want to
have it you can declare them as virtual or abstract in class A and override
them in the derived classes.

One more mistake (or maybe it is what you are going for) calling a.Method1
will call A.Method1 not B.Method1. This is because Method1 is not declared
as virtual in class A
Question 2: My actual program consists of 3 dervied classes and
according to an variable I must instantiate one of the three classes
but I wanted teh object to have the same name because subsequent code
in the program. I was going to do it the above way but if there is
something wrong with my thinking what would be the best way of doing
this.
I'm not sure I understand correctly the problem, but you can do that by
declaring the variable that keeps the references to be of the base type. As
you did in Q1. However you can access only methods decalred in this class.
So, refine your design. Maybe some of the methods and properties make sense
to be virtual.
 
Hi,
inline:


Hello everyone,

any help with this would be appreciated as I have spent the past
afternoon trying to sort it out. I have two questions based on the
followiung information.

I have two classes: -

Class A {
Method 1() {}
}

Class B: A {
Method 1(){}

Method 2() {}

Method 3() {}
}

From another part of my program I try and call the class as follows: -

A a = new B();

I can now access method 1 from a , however I cannot access method 2 or
3 from A and I can't see why I cannot access them.

They are not declared in A, and your variable "a" is A class. You have
to cast it to B to use 2 and 3:

B b = (B)a;
b.Method3();
My underdtanding of inhertiance, polymorphism and OO design was that I
could do this. Question 1: what am I doing wrong here to make this
work.

You do not use virtual and override, so the new declaration of Method1
in B just hides the one in A. The right construct will be:

class A{
public virtual void Method1
{
ConsoleWriteLine("A.Method1");
}
}

class B : A
{
public override void Method1{
ConsoleWriteLine("B.Method1");
}
}

A aA = new A();
aA.Method1();
//output: A.Method1

A aB = new B();
aB.Method1();
//output: B.Method1
Question 2: My actual program consists of 3 dervied classes and
according to an variable I must instantiate one of the three classes
but I wanted teh object to have the same name because subsequent code
in the program. I was going to do it the above way but if there is
something wrong with my thinking what would be the best way of doing
this.

Create a base abstract class with with all the public methods/properties
you want to be known. Then derive from it.

This is a simple factory pattern.

Again any help would be greatfully appreciated.

Kind Regards

Phil

Sunny
 
Another possibility would be to use an interface. Define an interface with the definitions of Method1, Method2, and Method3. Have each concrete class implement this interface, call it IMyInterface, and then use the factory pattern mentioned above to return the created object as a IMyInterface.

Take a look here for info about intefaces vs. abstract classes:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vbcon/html/vbconabstractclassesversusinterfaces.asp

Sunny
 
Hello everyone,

I just wanted to thankyou for all of your posts, I think I understand
what I was doing wrong now.

Kind Regards

Philip
 
Back
Top