Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
//this is absract class
abstract class abc
{
    //abstract member
    public string _str ="HELLO";
   }

//this is derived class
class xyz : abc
{
    public void add()
    {
        string _str = "WORLD";
//here i want hello world result in derived class method SO HOW CAN I DO THAT
        Console.WriteLine(?);
        Console.ReadLine();
    }
}
Posted
Updated 21-Sep-11 7:58am
v2

By the way, the answer is this:
C#
string _str = "WORLD";
System.Console.Write(_str);
System.Console.Write(base._str);


Paradoxically, this will have the same effect:
C#
string _str = "WORLD";
System.Console.Write(_str);
System.Console.Write(this._str);

but this is because there is no another _str as a class member; the new _str is a stack variable. Consider this:
C#
class xyz : abc {
    new string _str = "HIDES BASE _str";
    public void add()
{
    string _str = "WORLD";
    System.Console.Write(_str); //will print "WORLD"
    System.Console.Write(this._str); //will print "HIDES BASE _str"
    System.Console.Write(base._str); //will print "HELLO";
}


The keyword new above is only used to prevent warning about hiding of base _str, and such hiding is indeed bad style.

Please understand these simple techniques and be considerate to people helping you. Yes, that was a lecture — thank you for attention. :-)

—SA
 
Share this answer
 
Go here [^]

For what it's worth, there are so many things wrong with the code you presented that it's hard to knwo where to start pointing them out. Did your instructor even tell you what an astract class is and why you would want to use one?
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Sep-11 18:10pm    
You're reported for abuse, my friend. How can you be so rude?

You show such ignorance but nevertheless nobody pointed it out before this moment, because it is normal to lack some knowledge and understanding at first; and nobody wanted to hurt your feelings. You are given the lecture because a lecture is what a learning person needs the most. As soon as you demand "solution not lecture", it's clear that you're not learning anything and your participation here is absolutely useless.

However, it's the best if you rethink your attitude. We are happy to help anyone who is trying to learn and behave constructively.

Best wishes,
--SA
#realJSOP 21-Sep-11 18:20pm    
A lecture is what you actually need.
[no name] 21-Sep-11 14:30pm    
this is interview quetions my friend
Sergey Alexandrovich Kryukov 21-Sep-11 18:12pm    
And I don't think we're interested to answer any interview questions. People need to learn science, technology, etc, but not answers to questions which leads nowhere.
--SA
#realJSOP 21-Sep-11 18:20pm    
We answer programming questions, not interview questions. If you're about to conduct an interview, and you're asking such questions here, you're going to end up with less than desirable employees. If you're about to BE interviewed, you may as well not go.

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