I want to hide the parent class method for the the child class object.
My Circle object display two methods to me when I going to access that methods:
Here objShape
is the object of the Shape class which is the abstract class.
I want to hide method which is display on 2nd image.
Here Circle class extends the Oval class and Oval class extends the Shape class.
Shape class coding
abstract public class Shape
{
public abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);
public abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);
}
Oval class Coding
class Oval : Shape
{
public override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dAns = Math.PI * dNum1 * dNum2;
return dAns;
}
public override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dTmp1, dTmp2, dAns;
dTmp1 = 2 * (dNum1 * dNum1);
dTmp2 = 2 * (dNum2 * dNum2);
dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);
return dAns;
}
}
Circle Class Code
class Circle : Oval
{
public double Area(double dNum1)
{
double dResult = base.Area(dNum1, dNum1);
return dResult;
}
public double Perimeter(double dNum1)
{
double dAns = base.Perimeter(dNum1, dNum1);
return dAns;
}
}
So, Here how can I hide the direct access of the Oval class method for the Circle class object. In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
Then you shouldn't make Circle
derive from Oval
, basically. Don't forget that it would be perfectly acceptable to have a variable of type Oval
which has a value at execution time referring to an instance of Circle
:
Oval x = new Circle();
Console.WriteLine(x.Area(10, 20));
Fundamentally, any operation that makes sense for the base class should make sense for the derived class as well, in a sensible inheritance hierarchy.
If you want to keep your inheritance hierarchy, you should consider a redesign such that the radius etc are part of the state of the object, instead of being passed in as parameters. For example:
public abstract class Shape
{
public abstract double Area { get; }
public abstract double Perimeter { get; }
}
public class Oval
{
private double Width { get; }
private double Height { get; }
public override double Area { get { ... } };
public override double Perimeter { get { ... } };
public Oval(double width, double height)
{
Width = width;
Height = height;
}
}
public class Circle
{
public double Radius { get { return Height; } }
public Circle(double radius) : base(radius)
{
}
}
See more on this question at Stackoverflow