Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface or Abstract Class to fulfill requirement

Tags:

c#

.net

I want to have a abstract view for any type of UI (web or window). In order to do that I must use Interface (IView ) in which I can only apply just rules about view. In fact, I want to set a some basic comple function to provide to its inheritances.

So in this way, I must use abstract class. The problem is

1) Interface only have rules 2) The view (web form or window form) can't inherit any more since that's already inherited from window or web form

How can I do that? Many thanks

like image 1000
mtt Avatar asked Dec 12 '08 17:12

mtt


People also ask

Is it better to use interface or abstract class?

If you are creating functionality that will be useful across a wide range of objects, then you must use an interface. Abstract classes, at the end of the day, should be used for objects that are closely related. But the interfaces are best suited for providing common functionality to unrelated cases.

What is advantage of using interfaces over abstract classes?

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.

Why is abstract class required?

An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.

Is an interface a set of requirements for classes that implement it?

A Java interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior. Definition: An interface is a named collection of method definitions (without implementations).


1 Answers

Will the functions you add change the definition of what the class is, or will you simply be creating functions to manipulate data that is already a part of the class?

If you need to redefine aspects of the base class of these two classes then yes, you will need to change your inheritance structure. But if the functions will simply manipulate data that is already part of the class now then I would suggest you use the interface and create utility functions.

Here is a clumsy example of what I mean:

using System;

abstract class Pet { }

class Dog : Pet, IPet
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

class Cat : Pet, IPet
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

interface IPet
{
    String Name { get; set; }
    Int32 Age { get; set; }
}

static class PetUtils
{
    public static void Print(this IPet pet)
    {
        Console.WriteLine(pet.Name + " is " + pet.Age);
    }
}

Your two UI classes are perhaps related in this way. I would imagine that what you need to do in a cross-cutting fashion would be solved by a utility method like the one I have created. I did create the PetUtils.Print method as an extension method as this will create the expressive illusion of an instance method. If you are not using C# 3 just remove "this" from public static void Print(this IPet pet).

like image 89
Andrew Hare Avatar answered Oct 12 '22 22:10

Andrew Hare