❔ 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
Pobiega2y ago
your Amount property should just be an auto-prop, no reason to give it a backing field
multisportz.hq
multisportz.hqOP2y ago
uhh what does that mean sorry im not that familiar with this stuff
Pobiega
Pobiega2y ago
decimal Amount { get; set; } is all you need
multisportz.hq
multisportz.hqOP2y 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
Pobiega2y 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.hqOP2y ago
oh alright so how would you implement the derived classes like what should change
Pobiega
Pobiega2y ago
but it never says what it should do or when it should be called, which is weird
multisportz.hq
multisportz.hqOP2y ago
it should just be the overriden interest method right?
Pobiega
Pobiega2y ago
either that, or have the class specify the interest rate
multisportz.hq
multisportz.hqOP2y 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
Pobiega2y 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.hqOP2y 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
Pobiega2y ago
yes
multisportz.hq
multisportz.hqOP2y 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
Pobiega2y ago
m is the decimal literal suffix and the => is the body expression symbol 🙂
multisportz.hq
multisportz.hqOP2y ago
woudlnt i need a constructor in each one because i need to make objects out of this
Pobiega
Pobiega2y ago
implicit constructors 🙂 or add ones, it doesnt matter
multisportz.hq
multisportz.hqOP2y ago
yeah but i cant initialize objects oh wait
multisportz.hq
multisportz.hqOP2y ago
is this right?
multisportz.hq
multisportz.hqOP2y 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
Accord2y 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