❔ Inheritance

hey so i have this inheritance assignment and i dont know how i am supposed to implement the derived class for banking because there is nothing associated with it so im a little confused, also if somoene could check my current code for the account class that would be great because im not sure if im doing this right
21 Replies
Pobiega
Pobiega16mo ago
your Amount property should just be an auto-prop, no reason to give it a backing field
multisportz.hq
multisportz.hq16mo ago
uhh what does that mean sorry im not that familiar with this stuff
Pobiega
Pobiega16mo ago
decimal Amount { get; set; } is all you need
multisportz.hq
multisportz.hq16mo ago
so remoivng public itslef is fine? or should i remove the instance variable as well ohh you mean like just intiialize the variable as that without putting the stuffi n get and set
Pobiega
Pobiega16mo ago
yep and thus remove the backing field just remove private decimal amount; and replace all references to it with Amount instead the Balance method strikes me as weird. I dont think it should add the interest. I dont see a purpose for the method at all tbh, but it is stated in the description that it must exist for some reason
multisportz.hq
multisportz.hq16mo ago
oh alright so how would you implement the derived classes like what should change
Pobiega
Pobiega16mo ago
but it never says what it should do or when it should be called, which is weird
multisportz.hq
multisportz.hq16mo ago
it should just be the overriden interest method right?
Pobiega
Pobiega16mo ago
either that, or have the class specify the interest rate
multisportz.hq
multisportz.hq16mo ago
oh ok so for the banking one, what should i even do because there is no interest rate associated with it and its just branching off to ones associated with interest rate
Pobiega
Pobiega16mo ago
something like this
public abstract class Account
{
public virtual decimal APY { get; }
public decimal Amount { get; set; }

public decimal CalculateInterest()
{
return (APY / 12) * Amount;
}
}

public class Credit : Account
{
public override decimal APY => 0.15m;
}

public abstract class Banking : Account
{
}

public class Checking : Banking
{
public override decimal APY => 0.05m;
}
public abstract class Account
{
public virtual decimal APY { get; }
public decimal Amount { get; set; }

public decimal CalculateInterest()
{
return (APY / 12) * Amount;
}
}

public class Credit : Account
{
public override decimal APY => 0.15m;
}

public abstract class Banking : Account
{
}

public class Checking : Banking
{
public override decimal APY => 0.05m;
}
banking serves absolutely no purpose except to give checking and savings a baseclass, but idk the hierarchy is a bit weird, and idk if this is a good fit for inheritance 😛
multisportz.hq
multisportz.hq16mo ago
oh wait we did learn about abstract classes in the textbook i just realied thanks a lot think i understand now whats the diff ebtween virtual and abstract methods? oh is it that virutal can be overriden?
Pobiega
Pobiega16mo ago
yes
multisportz.hq
multisportz.hq16mo ago
where do you learn the => thing cuz i cant find that anywhere online for override nvm i see why do u put the m in the end tho oh wait i got that error and see lmao
Pobiega
Pobiega16mo ago
m is the decimal literal suffix and the => is the body expression symbol 🙂
multisportz.hq
multisportz.hq16mo ago
woudlnt i need a constructor in each one because i need to make objects out of this
Pobiega
Pobiega16mo ago
implicit constructors 🙂 or add ones, it doesnt matter
multisportz.hq
multisportz.hq16mo ago
yeah but i cant initialize objects oh wait
multisportz.hq
multisportz.hq16mo ago
is this right?
multisportz.hq
multisportz.hq16mo ago
ill rename it to checking but alright actually i finished it @Pobiega but thanks a lot for your help, def wouldnt have figured it out if not for your help 🙏
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts