[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

inherits and interface?

Hi,

Can you explain and when I should use interface verse create a super class?

class Surface {
Property Type() As String
Property Material() As String
Sub Execute()
}

class SoftSurface inherits Surface {
}

vs

 Interface Surface {
Property Type() As String
Property Material() As String
Sub Execute()
}

class SoftSurface inherits Surface{
}
0
dkim18
Asked:
dkim18
6 Solutions
 
LIONKINGCommented:
For starters if you have a super class and you have a "defined" (coded/working) method, this method is automatically available to any class that derives from this class.
When you have an interface, you are responsible of coding each and every one of the methods defined in the interface.

You can give this a read, which I'm pretty sure explains it better than I do.

http://msdn.microsoft.com/en-us/library/ms973861.aspx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can Inherit from only ONE Class, but you can Implement MANY Interfaces.  This if often the deciding factor in a design...
0
 
Julian HansenCommented:
An interface can be thought of as a prototype - it does not do anything but it defines how any class inheriting that interface should be implemented.

So
class IVehicle
{
  // No functionality here only prototypes of methods that must be implemented
  // in class implementing interface

   void Go();
   void Stop();
}

class MotorCar : IVehicle
{
     void Go () 
     {
          put_foot_on_the_gas();
     }

     void Stop()
     {
          put_foot_on_the_brake();
     }
}

class Bicycle : IVehicle 
{
   void Go()
   {
       push_the_pedals();
   }
   void Stop()
   {
       pull_the_brake_handle();
    }
}

Open in new window


Superclass can have functionality defined - allows for inheritance and polymorphosm aspects of Object Orientation
   class ParentClass
   {
      
      void DoSomethingUseful()
      {
          perform_useful_task();
      }    
   }

  class ChildClass : ParentClass
  {
       void run()
       {
           DoSomethingUseful(); // Invoke function in parent class inherited by child class
       }
  }

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LIONKINGCommented:
Superclass can have functionality defined - allows for inheritance and polymorphosm aspects of Object Orientation

Yes, and polymorphism can be obtained through interfaces as well.
0
 
Jacques Bourgeois (James Burger)Commented:
You use inheritance when starting with a class that has already implemented features that you want to use without having to code them, but with the possibility of adding to those features or to change they way they behave. Inheritance is use with a class that already has working code.

An interface is not a class. It is only a list of properties and methods. This is a way to have many classes work in a similar way, by having a set of common properties and methods. Contrary to a class, there is no code in an interface, only the list. It is up to each class that implements the interface to provide the code.

In your example, you do not have any code in the class, so an interface would be a better choice.

Suppose that you are creating an audio-visual application that enables the user to chose a document of any type (tape cassette, audio CD, video CD, video DVD, video file...) and you would like to have a button to Play the selected document. Since the Play method has to work differently depending on the type of document, you could not inherit from a class that has a play method. Having an interface, you are forcing every single class to provide the code for its own play method. You can then have a method that receives the interface type as a parameter, pass it any of the types that implement the interface, and simply call Play. The system will automatically call the method that was coded for the type of document received as a parameter.
0
 
CodeCruiserCommented:
For a brief explanation, I think the two keywords used when using base class and interface describe it very well.

When using a base class and subclass, you INHERIT base class functionality and extend/override it

When using an interface, you IMPLEMENT the functionality declared in interface.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now