object oriented - Inheritance

Question posted in Computer Software on 06 2010
Rate question difficulty level 0 Votes
Is it a valid code and what will be the result

public class shape
{
public virtual void printType() {
Console.Write("Im a shape ");
}
}

public class circle:shape
{
public virtual void printType()
{
Console.Write("Im a circle");
}
}

void main ()
{

shape circle = new circle();
circle.printType();
}
 
 
4 Answers
 
the result is : "compile error"

since you can not write virtual in the circle function
you can write override and than it will work

not good

public class circle:shape
{
public virtual void printType()
{
Console.Write("Im a circle");
}
}

good

public class circle:shape
{
public override void printType()
{
Console.Write("Im a circle");
}
}

and than it will write Im a circle

06/17/2010
 
 
another thing you need to know is :
you can not write

circle cir = new shape();

you will get compile error.

you can only write it this way

shape circle=new circle ();



06/17/2010
 
 
There is no error in compilation, the code gets compiled properly with a warning in circle:printType saying circle.printType hides inherited memeber of shape.printType.

06/22/2010
 
 
There is no error, the code is fine and the result will be "Im a shape".
If the circle method was override and not virtual the result would be "I'm a circle"

public class circle:shape
{
public override void printType()
{
Console.Write("Im a circle");
}
}

07/28/2011
 
 
Add an answer*
 
Your name
Email
 
Location: United States
object oriented c# .net java

add a question

arrow_blue


Now hiring!