C
C#13mo ago
Dinny

can someone help me with my abstract class

I've created my abstract classes and no we can't make instances of them, but i keep getting errors even after following youtube videos to the t. here's my first file using abstract classes
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
protected double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength(double length) { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
protected double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength(double length) { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
25 Replies
Dinny
DinnyOP13mo ago
in my main program, i know we can't do, for example, " RailroadCar railroadcar = new RailroadCar()," but in the exmple on youtube, i could do something like "RailroadCar railroadcar = new TankCar();" but that doesn't work either. I am still new and trying to understand, but I am so stuck
Angius
Angius13mo ago
Does TankCar inherit from RailroadCar?
Dinny
DinnyOP13mo ago
yes it does one moment
Angius
Angius13mo ago
It has to work, then
MODiX
MODiX13mo ago
Angius
REPL Result: Success
abstract class Foo {}
class Bar : Foo {}

Foo f = new Bar();
abstract class Foo {}
class Bar : Foo {}

Foo f = new Bar();
Compile: 566.461ms | Execution: 36.685ms | React with ❌ to remove this embed.
Dinny
DinnyOP13mo ago
namespace Module_9
{
public abstract class TankCar : RailroadCar
{
//data fields
private double radius;

protected TankCar(double radius, double length) : base(length)
{
SetRadius(radius);
SetLength(length);

}
namespace Module_9
{
public abstract class TankCar : RailroadCar
{
//data fields
private double radius;

protected TankCar(double radius, double length) : base(length)
{
SetRadius(radius);
SetLength(length);

}
here's a tiny bit og tankcar of i don't see what I am doing wrong then
Angius
Angius13mo ago
Your constructor is protected, that might be why you're getting errors
Dinny
DinnyOP13mo ago
bc i keep getting the same error :///
Angius
Angius13mo ago
But it's hard to tell without seeing the actual error
Dinny
DinnyOP13mo ago
ill take a picture of it one momebt plz
Dinny
DinnyOP13mo ago
No description
Dinny
DinnyOP13mo ago
should they all be public instead of protected ?
Angius
Angius13mo ago
Ah Well Your TankCar itself is abstract And, as you said, can't instantiate abstract classes
Dinny
DinnyOP13mo ago
one moment lemme try fixing that okay i made them now a public class derived from the abstract classes but it’s still not working
Angius
Angius13mo ago
Is the derived class still abstract?
Dinny
DinnyOP13mo ago
yes that's how my prof is doing it too
Angius
Angius13mo ago
It's not It cannot be Because instantiating an abstract class is not possible The base class can be abstract But classes that inherit from it can be not-abstract
Dinny
DinnyOP13mo ago
oh no wait sorry the base class is abstract the derived classes are not they're all public
Angius
Angius13mo ago
And in your example the derived class is still abstract Hence the error
Dinny
DinnyOP13mo ago
oh wait let me show up my updated version
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
private double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength() { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
namespace Module_9
{
public abstract class RailroadCar
{
//data fields
private double length;

//constr
protected RailroadCar(double length = 0.00)
{
SetLength(length);
}

//GETTERS
/** Returns length of solid.
@return: solid length
*/
public double GetLength() { return length; }

/** Updates length of solid.
@return: updated length.
*/
//SETTERS
public void SetLength(double length) { this.length = length;}

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public abstract double Volume();
}
}
here's the base abstract class
namespace Module_9
{
public class TankCar : RailroadCar
{
//data fields
private double radius;

public TankCar(double radius = 0.0, double length = 0.0) : base(length)
{
SetRadius(radius);
SetLength(length);

}

//GETTERS
/** Returns radius of solid.
@return: solid radius.
*/
public double GetRadius() { return radius; }



//SETTERS
/** Updates radius of solid.
@return: updated radius
*/
public void SetRadius(double radius) { this.radius = radius; }

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public override double Volume() { return Math.PI * Math.Pow(radius, 2) * GetLength(); }
}
}
namespace Module_9
{
public class TankCar : RailroadCar
{
//data fields
private double radius;

public TankCar(double radius = 0.0, double length = 0.0) : base(length)
{
SetRadius(radius);
SetLength(length);

}

//GETTERS
/** Returns radius of solid.
@return: solid radius.
*/
public double GetRadius() { return radius; }



//SETTERS
/** Updates radius of solid.
@return: updated radius
*/
public void SetRadius(double radius) { this.radius = radius; }

/** Calculates the volume of the solid.
@return: calculated volume of the solid
*/
public override double Volume() { return Math.PI * Math.Pow(radius, 2) * GetLength(); }
}
}
heres the TankCar class
Angius
Angius13mo ago
Now it should work
Dinny
DinnyOP13mo ago
why is there still a squiggle ugh lemme refrsh plz hold
Angius
Angius13mo ago
What does the squiggle say?
Dinny
DinnyOP13mo ago
okay i just needed to save and refresh VS does that soemetines, it's annoying tysm !! u were a big help
Angius
Angius13mo ago
Anytime Ok
Want results from more Discord servers?
Add your server