Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a little problem with C# inheritance. It's easy but I cant figure it out!
Suppose I have a parent class and a child class(child:parent).
I just want to know if we define (child = new parent) can we access to child properties? Because I tried it but I couldn't do that, by the way i think it should work..

Here is a example code:
C#
public class Parent
    {
        private int _Parent1;

        public int Parent1
        {
            get { return _Parent1; }
            set { _Parent1 = value; }
        }   
    }

public class Child1 : Parent
    {
        private int _Child1Prop;

        public int Child1Prop
        {
            get { return _Child1Prop; }
            set { _Child1Prop = value; }
        }
    }


suppose
C#
Parent test = new Child1();

Is this possible:?
C#
test.Child1Prop = some value;
Posted
Updated 23-Dec-11 0:54am
v3
Comments
[no name] 23-Dec-11 6:14am    
alizadeh91--> plz dig deep in CPallini's answer. It makes scene.

You cannot write
C#
Child=new Parent;

The compiler would complain.

[updated]

OK, you rephrased the question, now:
C#
Parent test = new Child1();
test.Child1Prop = some value;

is not possible.

However, for instance:
C#
Parent parent = new Child1();
(parent as Child1).Child1Prop = 5;

would work fine.

[/update]
 
Share this answer
 
v3
Comments
alizadeh91 23-Dec-11 2:51am    
Actually i can!! try it.. that's way of object oriented programming!
CPallini 23-Dec-11 4:07am    
Nope: object oriented programming goes into the opposite direction:
Parent parent = new Child1();
Did you try to compile it?
[no name] 23-Dec-11 6:14am    
5!
CPallini 23-Dec-11 6:17am    
Thanks.
alizadeh91 23-Dec-11 6:38am    
actually i meant that :D Yea that was a mistake suppose: Parent parent = new Child1();
thanks guys :) anyway can i do that???
This isn't possible:

C#
child1 test = new Parent;


You can't cast base to the child object. From my experience this isn't possible. You can implement constructor in Child1 class like this:

C#
public Child1(Parent parent)
{
   _Child1Prop = 2;
}


BUT you can do something like this:

C#
    public class Parent
    {
    }

    public class Child1 : Parent
    {
    }

    public class Child2 : Child1
    {
    }

    public class Child3 : Child2
    {
    }

//-------------- somewhere in Main -------
    Child3 ch3 = new Child3();
    Child1 ch1 = (Child1)ch3;
    Child2 ch2 = (Child2)ch1;


I hope you're asked this.
 
Share this answer
 
v2
Comments
alizadeh91 23-Dec-11 3:26am    
Yeah!!! your solution works but i dont want to use casting!!!
RaisKazi 23-Dec-11 8:44am    
My 5.
Drazen Pupovac 23-Dec-11 12:48pm    
Thank you.
RaviRanjanKr 23-Dec-11 15:43pm    
5+
Drazen Pupovac 23-Dec-11 16:48pm    
Thank you.
C#
child1 test = new Parent;


simply this isn't possible.

"your solution works but i dont want to use casting!!!"

There is no way to do something like that with or without casting, it's not allowed.

You can do this:
C#
public class Child1 : Parent
{
    public static implicit operator Child2(Child1 ch2)
    {
        return new Child2();
    }
}

public class Child2 : Child1
{
    Child2 ch2 = new Child1();
}


but not in this case:
C#
child1 test = new Parent;
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 15:43pm    
My 5+
Drazen Pupovac 23-Dec-11 16:49pm    
Thank you
Suppose you have a class Child2 as well :
public class Child2 : Parent


The following shows what is possible and what is going to happen :
C#
Parent test = new Child1();  // permitted
Child1 test1 = test as Child1; // permitted
Child1 test2 = (Child1)test; // permitted

((Child1)test).Child1Prop = 5; // explicit cast of test
test1.Child1Prop = 5;
test2.Child1Prop = 5;

Parent test3 = new Child2();  // permitted
Child1 test4 = test3 as Child1; // test4 will be null, cannot be cast to Child1
Child1 test5 = (Child1)test3; // runtime exception, test3 cannot be cast to Child1

// if somehow no exception has occurred 
((Child1)test3).Child1Prop = 5; // runtime exception, test3 cannot be cast to Child1
test4.Child1Prop = 5; // null pointer exception on test4
test5.Child1Prop = 5; // if test3 magically had been cast to Child1, no such method or property exception


Cheers
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 15:43pm    
5+
Friends thanks for answering but it's completely possible to write:
child1 test = new Parent;

i just wanna know after this declaration can i access to child1 properties???
 
Share this answer
 
v2
Comments
CPallini 23-Dec-11 4:15am    
C'mon: if you ever instantiate a 'Parent' class (and the compiler won't allow it) how could you access Child1 properties (they simply don't exist)?
alizadeh91 23-Dec-11 11:19am    
Thats right CPallini i'm really sorry that was a mistake i meant Parent test = new Child1();
anyway my problem still exists!!!!
RaviRanjanKr 23-Dec-11 15:44pm    
A suggestion :- you can use Have a question or Comment button to drop your message and to get Immediate response instead of posting as answer.
Come on guys.. :( answer it .. it's really easy!
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 15:44pm    
A suggestion :- you can use Have a question or Comment button to drop your message and to get Immediate response instead of posting as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900