Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: understanding Abstraction

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

like image 714
Sikander Avatar asked Dec 11 '22 05:12

Sikander


2 Answers

First of all, there are a couple of issues in your code.

  • The abstract class does contain methods without a body and are not abstract, so this isn't valid. You should either make the methods abstract, or provide a body in them. When you want to have a body but you want to be able to override the method, the virtual keyword must be applied. So this are the choices that you have for your abstract class:

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:

  • When your base class does contain abstract members, it should be implemented in your inheriting class.
  • When your base class does contain virtual members, it should not be implemented in your inheriting class but it can be overidden if needed.

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.

like image 148
Complexity Avatar answered Dec 22 '22 17:12

Complexity


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
    }
}
like image 21
David Pilkington Avatar answered Dec 22 '22 17:12

David Pilkington