Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,i recently studied abstract classes,my sir gave me an wonderful explanation.
the explantion goes in this way
1)implement a abstract person class with fields like age ,name dateofjoin,...and some methods like experience...
2)if employee class inherits from person ,he will inherit the total functionalities
3)we create manager class ,it inherits some features from person ,since role of employee is diiferent from manager he can have new methods like fireemployee ,add new employee...


my question is how to use the abstract members in derived classes .can u give me example in form of code for the above scenario.
thanks
Posted
Comments
Manfred Rudolf Bihy 4-Apr-12 7:38am    
That isn't an explanation, but sounds rather like a homework assignment. If you have studied abstract classes. as you said. there should be no difficulty in this for you.
satishmachineni 4-Apr-12 7:42am    
i studied but iam stuck with how to implement the above scenario in code .

You can access any properties defined in the parent class not defined as private.

C#
abstract class AbstractClass
{
    private int privateVariable { get; set; }
    public int publicVariable { get; set; }
    internal int internalVariable { get; set; }
    protected int protectedVariable { get; set; }
    protected internal int protectedinternalVariable { get; set; }
}

class DerivedClass : AbstractClass
{
    public DerivedClass()
    {
        // Cannot access a variable classified as private:
        //privateVariable = 0;
        // But these are valid:
        publicVariable = 1;
        internalVariable = 2;
        protectedVariable = 3;
        protectedinternalVariable = 4;
    }
}


There is a lot more involved in abstract and base classes than just access. You can define methods in an abstract class as abstract, and then the derived class must define those methods. Also you can override or create new methods in the derived class. Need to look at general article on abstract classes, this has some of the information: All about abstract classes.[^]
 
Share this answer
 
Try:
C#
public abstract class myBase
    {
    public int MyProperty { get; set; }
    public int MyMethod(int i) { return i * MyProperty; }
    }
public class myDerived : myBase
    {
    public myDerived()
        {
        MyProperty = 14;
        Console.WriteLine(MyMethod(2));
        }
    }
 
Share this 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