I have gone through many articles and some Stack Overflow questions for better understanding of abstraction
but am a little confused. This is what I have been reading: here
Abstraction is "To represent the essential feature without representing the background details."Abstraction lets you focus on what the object does instead of how it does it.
Code given:
abstract class MobilePhone
}
public void Calling();
public void SendSMS();
}
public class Nokia1400 : MobilePhone
{
}
public class Nokia2700 : MobilePhone
{
public void FMRadio();
public void MP3();
public void Camera();
}
My question is that when we inherit abstract class
don't we implement details in our sub classes?
calling() and sendSms() dont have any implementation in the super type so when we implement it in our sub class then we should know background details as well. So how is Abstraction actually working in this example?
Edit :if you cam here for some help these guys gave best answers : Complexity Sergey Berezovskiy
First of all, there are a couple of issues in your code.
Methods without a body:
abstract class MobilePhone
{
public abstract void Calling();
public abstract void SendSMS();
}
Methods with a body that can be overriden:
abstract class MobilePhone
{
public virtual void Calling()
{
// Code goes here.
}
public virtual void SendSMS()
{
// Code goes here.
}
}
Methods with a body that cannot be overriden:
abstract class MobilePhone
{
public void Calling()
{
// Code goes here.
}
public void SendSMS()
{
// Code goes here.
}
}
Then there are a couple of more problems. Your abstract class does not provide a constructor which makes the object private by default, so your class Nokia1400 and Nokia2700 cannot inherit from the base class because the base class is less accessible than the abstract class.
Now, depending on how you created your abstracted class, the classes that implementes it must fullify some requirements:
And than, your last class with not compile neither because it does contain methods without a body. Those kinds of methods are only allowed if they are abstract and then they must be in an abstract class.
Here's a sample based on your code:
public abstract class MobilePhone
{
public virtual void Calling()
{
Console.Write("Calling");
}
public abstract void SendSMS();
}
public class Nokia1400 : MobilePhone
{
public override void SendSMS()
{
Console.WriteLine("Sending SMS from Nokia 1400.");
}
}
public class Nokia2700 : MobilePhone
{
public void FMRadio()
{
Console.WriteLine("FM Radio");
}
public void MP3()
{
Console.Write("MP3");
}
public void Camera()
{
Console.WriteLine("Camera");
}
public override void SendSMS()
{
Console.WriteLine("Sending SMS from Nokia 2700.");
}
}
So, a fair long post but I hope it helped.
You can have abstract methods and virtual methods
The abstract methods have to be be implemented by the extending class and do not have a body.
The Virtual methods do have a body but do not need to be overridden. If they are not, then the body of the base class' virtual method is executed.
There are some caveats but that is the main idea
public abstract class MyBase
{
public abstract void MethodMustBeImplemented();
public virtual void DoesNotHaveToBeOverwritten()
{
//Do WORk
}
}
public class Implementor: MyBase
{
}
This will throw a compile time error as MethodMustBeImplemented()
has not been overridden.
But the following is fine
public class Implementor: MyBase
{
public override void MethodMustBeImplemented()
{
//Do WORk
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With