Re: Abstract class or interface?




"ozbear" <ozbear@xxxxxxxxxxx> wrote in message
news:4277546e.81458468@xxxxxxxxxxxxxx
> On Sun, 1 May 2005 19:35:19 -0400, "Brett" <no@xxxxxxxx> wrote:
>
>>I'm still trying to figure out concrete reasons to use one over the other.
>>I understand the abstract class can have implementation in its methods and
>>derived classes can only inherit one abstract class. The interface has
>>implied abstract methods/properties and derived classes can inherit
>>multiple
>>interfaces. The interface properties/methods have no implementation.
>>
>>Besides definitions of the two, what are some conceptual reasons to use
>>one
>>over the other? Perhaps examples of this project uses an abstract classes
>>vs. this one uses an interface...for these reasons.
>>
>>I have one project that uses an interface. I chose the interface over an
>>abstract class because the derived classes all do the same thing, they
>>just
>>go about doing it in slightly different ways. Their results are different
>>but conceptually what they do is the same thing. I don't ever see the
>>need
>>for adding more methods or properties to the interface. If there is every
>>such a need, I can encapsulate this variability into one of the derived
>>classes, since it will be "one" that has such a need rather than all.
>>That
>>make sense?
>>
>>Thanks,
>>Brett
>
> I have a 3-D package that under user control needs to be able to move
> and rotate "things" along all three X, Y, and Z coordinate axis.
>
> The "things" could be sinple points (Vertex class), lines (Line
> class), planar faces (Faces class), or solid things (Solids class).
>
> All these things are defined as supporting the I3D interface whch
> means they must implement methods such as Rotate(x angle, y angle,
> z angle), Translate(some axis), Scale(some percentage), and so forth.
>
> Rotating a Vertex is different than a Line, but the interface doesn't
> know nor care what the differences are. Furthermore, I can place all
> the different things in an ArrayList by their interface and then run
> a simple...
>
> foreach (I3D i3dobj in arraylist)
> i3dobj.Rotate(xangle,yangle,zangle);
>
> to rotate all the objects in the scene.
>
> In this example it would be possible to define an abstract class
> called, say, Ab3D, and have Vertex, Line, Face, etc., derive from
> Ab3D and override the Rotate, Translate, etc. methods in each.
> THis implies some closer coupling between these classes than I
> would like, furthermore...
>
> What if I want to include rotated text or something else where
> the bulk of the work is already defined in an existing class which
> does not derive from Ab3D? If I don't have control of that existing
> class' definition, or if it itself is a derived class I cannot make it
> derive from Ab3D to override those methods.
>
> With interfaces however, I can derive a new class from that already
> existing one, state that it supports the I3D interface, and then
> write the supporting Rotate, Translate, etc., methods for it. Then I
> can include it in the arraylist above and perform the 3D actions on
> it with no changes to manipulation code. I do not have to change
> the interface to make new things 3D manipulatable.
>
> Oz
> --
> A: Because it fouls the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?

Thanks. Great example. Say your interface has three methods - X, Y, Z.
You want to add a forth - Time or T. What now? This is completely
unexpected. I know you could allow your implementation to inherit from
another interface, perhaps called ITime. What is best?

Brett


.



Relevant Pages

  • Re: Abstract class or interface?
    ... > I'm still trying to figure out concrete reasons to use one over the other. ... > in its methods and derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class or interface?
    ... Abstract classes require and imply inheritance whereas interfaces do not. ... An abstract class would be used wherever it was important to enforce some ... aspect of the implementation an interface is used where only the agreement ... > derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Declaring a Constructor in an Interface?
    ... i have read many places that an interface is faster than ... but would You also claim that an abstract class ... Dennis JD Myrén ... > Virtual methods always means performance overhead. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java/J2EE Openings in RTP, NC
    ... JSP, EJB, DAO Developer ... An abstract class is declared with the keyword "class" and is an implementation, i.e., its methods can contain bodies. ... An interface is a declaration of public method signatures which taken together represent a type with a defined contract for interaction with other types. ... A "Type 1" JDBC driver is a bridge to an ODBC driver which in turn interacts with the data store. ...
    (comp.lang.java.programmer)
  • Re: Abstract class or interface?
    ... >derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ... and rotate "things" along all three X, Y, and Z coordinate axis. ...
    (microsoft.public.dotnet.languages.csharp)