<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-6962294913398429126.post632993672108468401..comments</id><updated>2008-01-15T06:33:23.543-08:00</updated><title type='text'>Comments on wickedcoolthoughts: Abstract class vs interface continued...</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://wickedcoolthoughts.blogspot.com/feeds/632993672108468401/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html'/><author><name>Aslam's Blog</name><uri>http://www.blogger.com/profile/07436503144903480817</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6962294913398429126.post-8337970383481731743</id><published>2008-01-15T06:33:00.000-08:00</published><updated>2008-01-15T06:33:00.000-08:00</updated><title type='text'>In the original post example of Shape there is no ...</title><content type='html'>In the original post example of Shape there is no requirement for combining with another type (atleast I cannot think of any). As there is no multiple inheritance requirement my clear choice would be a class here. &lt;BR/&gt;&lt;BR/&gt;However in cases where multiple inheritance is required we are left with no option than to use an interface. Its a limitation of language and all of us have to live with it.&lt;BR/&gt;&lt;BR/&gt;From a system extensibility point, ProdegeMusician is a Person who changes his/her role as a Student or Musician depending on the context will be a more extensible design.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/8337970383481731743'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/8337970383481731743'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html?showComment=1200407580000#c8337970383481731743' title=''/><author><name>Aslam's Blog</name><uri>http://www.blogger.com/profile/07436503144903480817</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='12679517629867847028'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html' ref='tag:blogger.com,1999:blog-6962294913398429126.post-632993672108468401' source='http://www.blogger.com/feeds/6962294913398429126/posts/default/632993672108468401' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-6962294913398429126.post-5356174578674575579</id><published>2008-01-14T23:44:00.000-08:00</published><updated>2008-01-14T23:44:00.000-08:00</updated><title type='text'>Sure - but your use of an ABC in this case is real...</title><content type='html'>Sure - but your use of an ABC in this case is really just a personal preference.  Syntactically, there really is no difference between this and defining Walker as a Java interface.  Either way, you can use 'Walker' as the type of a variable anywhere in the system.&lt;BR/&gt;&lt;BR/&gt;How would you deal with something that has two base 'types' that you can refer to in different areas of the system?  Say you have a ProdegeMusician - which has the base types of Student and of Musician.  This is the classic multiple inheritance requirement.  In some cases you would want to pass a ProdegeMusician object to parts of the system that are only concerned with Students (eg. billing them for time?) and in other cases to parts of the system that are only concerned with Musicians (eg. getting them to play?).&lt;BR/&gt;&lt;BR/&gt;Since classical multiple inheritance where two base classes containing attributes or default method implementations can lead to ambiguity and confusion (eg. the diamond problem in C++), they introduced the concept of a Java 'interface' to allow you to have two 'base types', whilst avoiding this area of confusion.&lt;BR/&gt;&lt;BR/&gt;But in this example, you couldn't define both Student and Musician as ABC's in Java - at least one would have to be an interface, even though you will use it as a type.&lt;BR/&gt;&lt;BR/&gt;This is one reason why many people strongly encourage the use of Java interfaces for defining all exposed types in a system (eg. the classic Foo and FooImpl scenario).  This is because two types that are not Java interfaces cannot be combined later into class represents both types, which puts a constraint on the extensibility of the system.&lt;BR/&gt;&lt;BR/&gt;(note: I personally prefer this as well, but I hate the Impl part - people should be more creative ;-) )</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/5356174578674575579'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/5356174578674575579'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html?showComment=1200383040000#c5356174578674575579' title=''/><author><name>chris</name><uri>http://www.blogger.com/profile/12295441975024860875</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html' ref='tag:blogger.com,1999:blog-6962294913398429126.post-632993672108468401' source='http://www.blogger.com/feeds/6962294913398429126/posts/default/632993672108468401' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-6962294913398429126.post-7451420388135437771</id><published>2008-01-14T22:44:00.000-08:00</published><updated>2008-01-14T22:44:00.000-08:00</updated><title type='text'>Hi Chris, thank you for explaining your thoughts a...</title><content type='html'>Hi Chris, thank you for explaining your thoughts and giving me an opportunity to express my thoughts more clearly.&lt;BR/&gt;&lt;BR/&gt;As I mentioned in the beginning of the post its only the conceptual difference between the two, I am focusing on.&lt;BR/&gt;Or when to use class and when to use interface. When I mean type/class I mean the existence of a real world type/class.&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;As anything I can draw is a Shape, I prefer to design Shape an abstract class rather than a Drawable interface.&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;In the other example Pet, Robot, Human are real world types. Eventhough all of them have a 'walk' behaviour,&lt;BR/&gt;the behaviour is not sufficient to identify any of the types Pet, Robot or Human. When I need to tie them together based &lt;BR/&gt;on the 'walk' behaviour you may use a Walkable interface. And 'walk' is only a subset of the interfaces(behaviours) exposed&lt;BR/&gt;by the types Pet, Robot or Human and that is why I would say Walkable does not define a real world type.&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;Now let us assume that 'walk' is the central behaviour we are  concerned about in our domain.&lt;BR/&gt;I prefer to design a Walker class as below&lt;BR/&gt;&lt;BR/&gt;abstract class Walker {&lt;BR/&gt;   abstract void walk();&lt;BR/&gt;&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;Pet extends Walker {&lt;BR/&gt;&lt;BR/&gt;  void walk(){....}&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;Robot extends Walker {&lt;BR/&gt;&lt;BR/&gt;  void walk(){....}&lt;BR/&gt;}&lt;BR/&gt;......</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/7451420388135437771'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/7451420388135437771'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html?showComment=1200379440000#c7451420388135437771' title=''/><author><name>Aslam's Blog</name><uri>http://www.blogger.com/profile/07436503144903480817</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='12679517629867847028'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html' ref='tag:blogger.com,1999:blog-6962294913398429126.post-632993672108468401' source='http://www.blogger.com/feeds/6962294913398429126/posts/default/632993672108468401' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-6962294913398429126.post-6769598358748248181</id><published>2008-01-14T10:00:00.000-08:00</published><updated>2008-01-14T10:00:00.000-08:00</updated><title type='text'>I should add - I used "Java interface" in my last ...</title><content type='html'>I should add - I used "Java interface" in my last comment on purpose.  If your talking about the concept of interfaces in general - then I'd agree, they are not a type.  An interface is a description of how you can interact (or interface...duh) with a component/object/whatever.&lt;BR/&gt;&lt;BR/&gt;Which leads to the statement:&lt;BR/&gt;&lt;BR/&gt;Java interfaces are not interfaces.&lt;BR/&gt;&lt;BR/&gt;Simple, huh? ;-)</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/6769598358748248181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/6769598358748248181'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html?showComment=1200333600000#c6769598358748248181' title=''/><author><name>chris</name><uri>http://www.blogger.com/profile/12295441975024860875</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html' ref='tag:blogger.com,1999:blog-6962294913398429126.post-632993672108468401' source='http://www.blogger.com/feeds/6962294913398429126/posts/default/632993672108468401' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-6962294913398429126.post-2584511531564973418</id><published>2008-01-14T09:57:00.000-08:00</published><updated>2008-01-14T09:57:00.000-08:00</updated><title type='text'>I didn't make the original "of course it's a type"...</title><content type='html'>I didn't make the original "of course it's a type" comment, but I totally agree.  A Java interface is still a type.&lt;BR/&gt;&lt;BR/&gt;Why?  Well - what is a type?  According to wikipedia:&lt;BR/&gt;&lt;BR/&gt; "A data type can also be thought of as a constraint placed upon the interpretation of data in a type system, describing representation, interpretation and structure of values or objects stored in computer memory."&lt;BR/&gt;&lt;BR/&gt;Although, I prefer a slightly simpler description - a type is anything you can refer to without explicitly requiring an understanding of it's structure.&lt;BR/&gt;&lt;BR/&gt;So in that case, a Java interface is a type, because you can define a variable that is referred to only by that Java interface name.&lt;BR/&gt;&lt;BR/&gt;So if I had, say:&lt;BR/&gt;&lt;BR/&gt;interface Shape {&lt;BR/&gt;  void draw();&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;I can then refer to a shape somewhere - eg.&lt;BR/&gt;&lt;BR/&gt;class Screen {&lt;BR/&gt;  void render(Shape s) {&lt;BR/&gt;  }&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;In the render method, there is a variable s, which is a Shape (eg. has the type of shape).  It's irrelevant that Shape is a Java interface (or ABC) in this case.&lt;BR/&gt;&lt;BR/&gt;So what is the difference between a Java interface and an ABC?  A Java interface cannot contain attributes or default method implementations.  This is purely for trickery for the Java language, since having multiple inheritance in the presence of attributes or default method implementations is confusing.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/2584511531564973418'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6962294913398429126/632993672108468401/comments/default/2584511531564973418'/><link rel='alternate' type='text/html' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html?showComment=1200333420000#c2584511531564973418' title=''/><author><name>chris</name><uri>http://www.blogger.com/profile/12295441975024860875</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://wickedcoolthoughts.blogspot.com/2008/01/abstract-class-vs-interface-continued_14.html' ref='tag:blogger.com,1999:blog-6962294913398429126.post-632993672108468401' source='http://www.blogger.com/feeds/6962294913398429126/posts/default/632993672108468401' type='text/html'/></entry></feed>